/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-08-16 17:20:44
 * @LastEditTime : 2023-05-25 15:48:58
 * @Description  : Do not edit
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:intl/intl.dart' show DateFormat;

class WFWeekPicker extends StatelessWidget {
  const WFWeekPicker({
    Key? key,
    required this.title,
    this.isRequired = false,
    this.hintText,
    required this.onChanged,
    this.initialWeek,
    this.type,
    this.minuteInterval,
  }) : super(key: key);
  final Widget title;

  ///是否必须
  final bool? isRequired;
  final String? hintText;
  final dynamic initialWeek;
  final void Function(dynamic datetime, String dateStr, int? weekNum) onChanged;
  final String? type;
  final int? minuteInterval;

  int getWeek(DateTime dateTime) {
    ///当前选中时间 周几
    var currentWeekDay = dateTime.weekday;

    ///周四
    int epochWeekDay = DateTime.thursday;

    DateTime epoch = DateTime.utc(dateTime.year);

    int offset = epochWeekDay - currentWeekDay;

    int delta = -offset;

    int week = (dateTime.difference(epoch).inDays - delta) ~/ 7 + 1;
    return week;
  }

  String intToStr(int v) {
    return (v < 10) ? "0$v" : "$v";
  }

  getWeekStr(datetime) {
    String daystr1 = DateFormat("yyyy-MM-dd").format(datetime.start);
    String daystr2 = DateFormat("yyyy-MM-dd").format(datetime.end);
    List arr1 = daystr1.split('-');
    List arr2 = daystr2.split('-');
    return '${DateFormat("yyyy").format(datetime.start)}年第${getWeek(datetime.start)}周（${arr1[1]}月${arr1[2]}日~${arr2[1]}月${arr2[2]}日）';
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    dynamic _initialDate = initialWeek;

    /// PC 格式: 2023第4周 (01月23日 ~ 01月29日)
    return WisFormLayoutComponent(
      title: title,
      isRequired: isRequired,
      axis: Axis.horizontal,
      child: StatefulBuilder(
        builder: (BuildContext context, setState) {
          return Row(
            children: [
              TextField(
                controller: TextEditingController(
                  text: _initialDate != null ? getWeekStr(_initialDate) : '',
                ),
                readOnly: true,
                onTap: () async {
                  final res = await WPickerUtil.week(
                    context,
                    title: title,
                    initialDate: _initialDate,
                  );

                  if (res != null) {
                    _initialDate = res;
                    setState(() {});
                    onChanged(_initialDate, getWeekStr(_initialDate),
                        getWeek(_initialDate.start));
                  }
                },
                style: TextStyle(
                  fontSize: 14.pt,
                  color: colorScheme.minor,
                ),
                decoration: InputDecoration(
                  fillColor: Colors.transparent,
                  filled: true,
                  hintText: hintText,
                  hintStyle: TextStyle(
                    fontSize: 15.pt,
                    color: colorScheme.placeholder2,
                  ),
                ),
              ).asExpanded(),
              _initialDate != null
                  ? WFClose(
                      onTap: () {
                        _initialDate = null;
                        setState(() {});
                        onChanged(null, '', null);
                      },
                    )
                  : Icon(
                      FaIcons.angle_right,
                      size: 18.pt,
                      color: colorScheme.placeholder,
                    )
            ],
          );
        },
      ),
    );
  }
}
