// ignore_for_file: sized_box_for_whitespace

import 'package:flutter/material.dart';
import 'package:tour_travel_agr/helper/components_widget/widget_button.dart';
import 'package:tour_travel_agr/helper/function/replace_date.dart';
import 'package:tour_travel_agr/helper/prefs.dart';
import 'package:tour_travel_agr/resource/assets.dart';
import 'package:tour_travel_agr/resource/colors.dart';
import 'package:tour_travel_agr/resource/constanta_string.dart';
import 'package:tour_travel_agr/resource/font.dart';
import 'package:tour_travel_agr/resource/size.dart';
import 'package:tour_travel_agr/resource/style.dart';

class ModalSelectDate extends StatefulWidget {
  const ModalSelectDate({
    Key? key,
    this.onTapWidget,
    this.startDate,
    this.endDate,
    required this.fromWidget,
    required this.todayActive,
    required this.customDateActive,
  }) : super(key: key);

  final void Function()? onTapWidget;
  final String fromWidget;
  final String? startDate;
  final String? endDate;
  final bool todayActive;
  final bool customDateActive;

  @override
  State<ModalSelectDate> createState() => _ModalSelectDateState();
}

class _ModalSelectDateState extends State<ModalSelectDate> {
  bool? todayActive;

  bool? customDateActive;
  String? fromDate;
  String? toDate;
  @override
  void initState() {
    todayActive = widget.todayActive;
    customDateActive = widget.customDateActive;
    if (widget.startDate != null && widget.endDate != null) {
      fromDate = widget.startDate!;
      toDate = widget.endDate!;
    } else {
      fromDate = dateLocal(DateTime.now().toLocal().toString());
      toDate = dateLocal(DateTime.now().toLocal().toString());
    }
    // if (widget.fromWidget == ConstantString.fromWidgetReimburse) {
    //   setReimburseDate(dateLocal(DateTime.now().toLocal().toString()));
    // }
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print(toInternationFormat(widget.startDate!));
    print(toInternationFormat(widget.endDate!));
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          margin: EdgeInsets.only(
            top: AppMargin.m16,
          ),
          padding: EdgeInsets.symmetric(
            horizontal: AppPadding.p20,
          ),
          child: Row(
            children: [
              GestureDetector(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Container(
                  width: 32,
                  height: 32,
                  child: Center(
                    child: Image(
                      width: 14,
                      height: 14,
                      image: AssetImage(
                        Assets.crossIcon,
                      ),
                    ),
                  ),
                ),
              ),
              const SizedBox(
                width: 17,
              ),
              Text(
                "Pilih Tanggal",
                style: getBoldStyle(
                  color: Colors.black,
                  fontFamily: FontConstants.mulish,
                  fontSize: 17,
                ),
              )
            ],
          ),
        ),
        GestureDetector(
          onTap: () {
            setState(() {
              todayActive = true;
              customDateActive = false;
              fromDate = dateLocal(DateTime.now().toLocal().toString());
              setReimburseDate(fromDate!);
            });
          },
          child: RadioSelectDate(
            active: todayActive!,
            title: "Today",
          ),
        ),
        GestureDetector(
          onTap: () {
            setState(() {
              todayActive = false;
              customDateActive = true;
              if (widget.fromWidget == ConstantString.fromWidgetReimburse) {
                setReimburseDate(
                    dateLocal(DateTime.now().toLocal().toString()));
              }
            });
          },
          child: RadioSelectDate(
            active: customDateActive!,
            title: "Select Date",
          ),
        ),
        GestureDetector(
          onTap: () async {
            setState(() {
              todayActive = false;
              customDateActive = true;
            });
            DateTimeRange? result = await showDateRangePicker(
              context: context,
              firstDate: DateTime.now().subtract(const Duration(
                days: 30,
              )),
              lastDate: DateTime.now(),
              currentDate: DateTime.now(),
              initialDateRange:
                  (widget.startDate != null && widget.endDate != null)
                      ? DateTimeRange(
                          start: toInternationFormat(widget.startDate!),
                          end: toInternationFormat(widget.endDate!),
                        )
                      : null,
              saveText: "Simpan",
              helpText: "Pilih Jangka Waktu",
              fieldStartLabelText: "Tanggal Awal",
              fieldStartHintText: "Awal",
              fieldEndLabelText: "Tanggal Akhir",
              fieldEndHintText: "Akhir",
              builder: (context, child) {
                return Theme(
                  data: Theme.of(context).copyWith(
                    colorScheme: ColorScheme.light(
                      primary: ColorManager.primary, // <-- SEE HERE
                      onPrimary: Colors.white, // <-- SEE HERE
                      onSurface: Colors.blueAccent, // <-- SEE HERE
                    ),
                    textButtonTheme: TextButtonThemeData(
                      style: TextButton.styleFrom(
                        foregroundColor: Colors.black, // button text color
                      ),
                    ),
                  ),
                  child: child!,
                );
              },
            );
            if (result?.start != null && result?.end != null) {
              setState(() {
                fromDate = dateLocal(result!.start.toString());
                toDate = dateLocal(result.end.toString());
                if (widget.fromWidget == ConstantString.fromWidgetReimburse) {
                  if (fromDate != toDate) {
                    setReimburseDate("$fromDate - $toDate");
                  } else {
                    setReimburseDate(fromDate!);
                  }
                }
              });
            }
          },
          child: Container(
            margin: EdgeInsets.only(
              top: AppMargin.m12,
            ),
            padding: EdgeInsets.symmetric(
              horizontal: AppPadding.p20,
            ),
            child: Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "From",
                        style: getRegularStyle(
                          color: Colors.black,
                          fontFamily: FontConstants.openSans,
                          fontSize: FontSize.s10,
                        ),
                      ),
                      Container(
                        margin: const EdgeInsets.only(
                          top: 3,
                        ),
                        width: double.infinity,
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              width: 1,
                              color: ColorManager.grey,
                            ),
                          ),
                        ),
                        child: Padding(
                          padding: const EdgeInsets.only(
                            bottom: 3,
                          ),
                          child: Text(
                            fromDate!,
                            style: getRegularStyle(
                              color: Colors.black,
                              fontSize: 12,
                              fontFamily: FontConstants.openSans,
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                SizedBox(
                  width: AppMargin.m20,
                ),
                Expanded(
                    child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "To",
                      style: getRegularStyle(
                        color: Colors.black,
                        fontFamily: FontConstants.openSans,
                        fontSize: FontSize.s10,
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.only(
                        top: 3,
                      ),
                      width: double.infinity,
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            width: 1,
                            color: ColorManager.grey,
                          ),
                        ),
                      ),
                      child: Padding(
                        padding: const EdgeInsets.only(
                          bottom: 3,
                        ),
                        child: Text(
                          toDate!,
                          style: getRegularStyle(
                            color: Colors.black,
                            fontSize: 12,
                            fontFamily: FontConstants.openSans,
                          ),
                        ),
                      ),
                    )
                  ],
                )),
              ],
            ),
          ),
        ),
        Container(
          width: double.infinity,
          margin: EdgeInsets.only(
            top: AppMargin.m20,
          ),
          color: const Color(0xffF1F3FA),
          child: Container(
            padding: EdgeInsets.symmetric(
              horizontal: AppPadding.p20,
              vertical: AppPadding.p15,
            ),
            child: CustomButton(
              text: "Terapkan",
              onTap: widget.onTapWidget,
            ),
          ),
        )
      ],
    );
  }
}

class RadioSelectDate extends StatelessWidget {
  const RadioSelectDate({
    Key? key,
    this.active = false,
    required this.title,
  }) : super(key: key);

  final bool active;
  final String title;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(
        horizontal: AppPadding.p20,
      ),
      margin: EdgeInsets.only(
        top: AppMargin.m20,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: getRegularStyle(
              color: Colors.black,
              fontFamily: FontConstants.openSans,
            ),
          ),
          Container(
            width: 21,
            height: 21,
            padding: const EdgeInsets.all(2.5),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(100),
              border: Border.all(
                color: (active) ? ColorManager.primary : ColorManager.grey,
                width: 1,
              ),
            ),
            child: (active)
                ? Container(
                    width: double.infinity,
                    height: double.infinity,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(100),
                      color: ColorManager.primary,
                    ),
                  )
                : null,
          ),
        ],
      ),
    );
  }
}
