import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:location/utils/common_expand.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';
import 'package:location/utils/toast_util.dart';
import '../resource/colors.gen.dart';
import '../resource/string.gen.dart';

typedef DateSureCallback = void Function(DateTime startDate, DateTime endDate);

class SelectGuardTimeDialog {
  static const String _tag = 'SelectGuardTimeDialog';

  static void show({
    DateTime? startDate,
    DateTime? endDate,
    required DateSureCallback sureCallback,
  }) {
    SmartDialog.show(
        tag: _tag,
        animationType: SmartAnimationType.centerFade_otherSlide,
        alignment: Alignment.bottomCenter,
        keepSingle: true,
        builder: (_) {
          return _SelectGuardTimeView(startDate ?? DateTime.now(),
              endDate ?? DateTime.now(), sureCallback);
        });
  }

  static void hide() {
    SmartDialog.dismiss(tag: _tag);
  }
}

class _SelectGuardTimeView extends StatelessWidget {
  DateTime startDate;
  DateTime endDate;
  final DateSureCallback sureCallback;

  _SelectGuardTimeView(this.startDate, this.endDate, this.sureCallback);

  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
      child: Container(
        width: double.infinity,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(16.w),
                topRight: Radius.circular(16.w)),
            gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: ['#E4E4FF'.color, ColorName.white],
                stops: [0.0, 0.3])),
        child: Column(
          children: [
            SizedBox(height: 24.w),
            buildTimeHeaderView(),
            SizedBox(height: 6.w),
            buildSelectDateView(),
            SizedBox(height: 6.w),
            buildSelectBtnView(),
            SizedBox(height: 12.w),
          ],
        ),
      ),
    );
  }

  Widget buildSelectBtnView() {
    return Row(
      children: [
        SizedBox(width: 16.w),
        Expanded(
            child: GestureDetector(
          onTap: () {
            SelectGuardTimeDialog.hide();
          },
          child: Container(
            height: 46.w,
            decoration: BoxDecoration(
                color: ColorName.colorPrimary.withOpacity(0.08),
                borderRadius: BorderRadius.circular(100.w)),
            child: Center(
              child: Text(
                StringName.dialogCancel,
                style: TextStyle(
                    fontSize: 14.sp,
                    color: ColorName.colorPrimary,
                    fontWeight: FontWeight.bold),
              ),
            ),
          ),
        )),
        SizedBox(width: 12.w),
        Expanded(
            child: GestureDetector(
          onTap: onSureClick,
          child: Container(
            height: 46.w,
            decoration: BoxDecoration(
                color: ColorName.colorPrimary,
                borderRadius: BorderRadius.circular(100.w)),
            child: Center(
              child: Text(
                StringName.dialogConfirm,
                style: TextStyle(
                    fontSize: 14.sp,
                    color: ColorName.white,
                    fontWeight: FontWeight.bold),
              ),
            ),
          ),
        )),
        SizedBox(width: 16.w),
      ],
    );
  }

  onSureClick() {
    //endDate 必须要大于 startDate
    if (endDate.isBefore(startDate)) {
      ToastUtil.show(StringName.dialogMustThanStartDate);
      return;
    }
    SelectGuardTimeDialog.hide();
    sureCallback.call(startDate, endDate);
  }

  Widget buildSelectDateView() {
    return Stack(
      alignment: Alignment.center,
      children: [
        Container(
          width: 336.w,
          height: 48.w,
          decoration: BoxDecoration(
              color: '#F6F6F8'.color,
              borderRadius: BorderRadius.all(Radius.circular(14.w))),
          child: Center(
            child: Text('-',
                style: TextStyle(fontSize: 14.sp, color: ColorName.black90)),
          ),
        ),
        Row(
          children: [
            SizedBox(width: 18.w),
            Expanded(
                child: DateTimePickerWidget(
              initDateTime: startDate,
              onChange: (dateTime, _) {
                startDate = dateTime;
              },
              dateFormat: 'HH时  mm分',
              locale: DateTimePickerLocale.zh_cn,
              pickerTheme: DateTimePickerTheme(
                  itemHeight: 48.w,
                  pickerHeight: 250.w,
                  itemTextStyle: TextStyle(
                      fontSize: 16.sp,
                      color: ColorName.black90,
                      fontWeight: FontWeight.normal),
                  backgroundColor: ColorName.transparent,
                  showTitle: false,
                  selectionOverlay: SizedBox.shrink()),
            )),
            SizedBox(width: 18.w),
            Expanded(
                child: DateTimePickerWidget(
              initDateTime: endDate,
              onChange: (dateTime, _) {
                endDate = dateTime;
              },
              dateFormat: 'HH时  mm分',
              locale: DateTimePickerLocale.zh_cn,
              pickerTheme: DateTimePickerTheme(
                  itemHeight: 48.w,
                  pickerHeight: 250.w,
                  itemTextStyle: TextStyle(
                      fontSize: 16.sp,
                      color: ColorName.black90,
                      fontWeight: FontWeight.normal),
                  backgroundColor: ColorName.transparent,
                  showTitle: false,
                  selectionOverlay: SizedBox.shrink()),
            )),
            SizedBox(width: 18.w),
          ],
        ),
      ],
    );
  }

  Widget buildTimeHeaderView() {
    return Row(
      children: [
        Expanded(
          child: Center(
            child: Text(
              StringName.commonPointSelectTimeStart,
              style: TextStyle(fontSize: 14.sp, color: ColorName.black60),
            ),
          ),
        ),
        Text('-', style: TextStyle(fontSize: 14.sp, color: ColorName.black30)),
        Expanded(
          child: Center(
            child: Text(
              StringName.commonPointSelectTimeEnd,
              style: TextStyle(fontSize: 14.sp, color: ColorName.black60),
            ),
          ),
        ),
      ],
    );
  }
}
