import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';

/// 这边需要迁移到UI库
FCalendarDatePicker _calendarDatePicker(
  DatePickerMode mode,
  BuildContext context,
  String key,
  DateTime startDateTime,
  DateTime endDateTime,
  ValueCallback selectedfn,
) {
  return FCalendarDatePicker(
    initialDate: key == '_startKey' ? startDateTime : endDateTime, // 初始化选中日期
    firstDate: DateTime(2000, 9, 10), // 开始日期
    lastDate: DateTime(2222, 9, 10), // 结束日期
    initialCalendarMode: mode, // 日期选择样式
    // 选中日期改变回调函数
    onDateChanged: (dateTime) {
      selectedfn.call(dateTime);
      Navigator.of(context).pop();
    },
    // 月份改变回调函数
    onDisplayedMonthChanged: (dateTime) {},
    // 筛选日期可不可点回调函数
    selectableDayPredicate: (dayTime) {
      if (dayTime.isAfter(DateTime.now())) {
        return false;
      }
      return true;
    },
  );
}

/// 时间选择器
class TimeSelectDialog extends Dialog implements FPage {
  final List? position;
  final String? globalKey;
  final DateTime startDateTime;
  final DateTime endDateTime;
  final ValueCallback selectedfn;
  @override
  final String pageName;

  TimeSelectDialog({
    Key? key,
    required this.startDateTime,
    required this.endDateTime,
    required this.selectedfn,

    /// 弹框出现位置
    this.position,

    /// 当前key
    this.globalKey,
    this.pageName = 'TimeSelectDialog',
  }) : super(key: key);
  @override
  FWidget build(BuildContext context) {
    return FMaterial(
      type: MaterialType.transparency,
      child: _buildProductListView(context, globalKey),
    );
  }

  FWidget _buildProductListView(context, globalKey) => FSafeArea(
        child: FGestureDetector(
          name: "back",
          businessParent: this,
          child: FStack(
            children: [
              FContainer(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height,
                color: Colors.transparent, // 它的颜色为透明色
              ),
              FPositioned(
                top: position![1],
                left: position![0],
                child: FContainer(
                  constraints: BoxConstraints(
                    maxWidth: 310,
                    maxHeight: 400,
                  ),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    boxShadow: [
                      BoxShadow(
                        blurRadius: 5,
                        color: Color(0xffeeeeee),
                      )
                    ],
                    borderRadius: BorderRadius.circular(5),
                  ),
                  child: _calendarDatePicker(
                    DatePickerMode.day,
                    context,
                    globalKey,
                    startDateTime,
                    endDateTime,
                    selectedfn,
                  ),
                ),
              ),
            ],
          ),
          onTap: () {
            Navigator.of(context).pop();
          },
        ),
      );
}
