import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hoshan/ui/theme/text.dart';
import 'package:hoshan/ui/widgets/components/button/loading_button.dart';
import 'package:hoshan/ui/widgets/components/dropdown/simple_dropdown.dart';
import 'package:shamsi_date/shamsi_date.dart';

class ShamsiYearMonthPicker extends StatefulWidget {
  final Function(Jalali) onDateSelected;
  final Jalali initailDate;
  const ShamsiYearMonthPicker(
      {super.key, required this.onDateSelected, required this.initailDate});

  @override
  State<ShamsiYearMonthPicker> createState() => _ShamsiYearMonthPickerState();
}

class _ShamsiYearMonthPickerState extends State<ShamsiYearMonthPicker> {
  late int selectedYear;
  late String selectedMonth;
  late int selectedMonthNum;

  List<String> years = List.generate(50, (index) => Jalali.now().year - index)
      .map((year) => year.toString())
      .toList();

  List<String> ms = [
    "فروردین",
    "اردیبهشت",
    "خرداد",
    "تیر",
    "مرداد",
    "شهریور",
    "مهر",
    "آبان",
    "آذر",
    "دی",
    "بهمن",
    "اسفند"
  ];

  late List<String> mounths = [...ms];

  @override
  void initState() {
    super.initState();
    final now = widget.initailDate;
    if (now.year == Jalali.now().year) {
      while (mounths.length == Jalali.now().month + 1) {
        mounths.removeLast();
      }
    }
    selectedYear = now.year;
    selectedMonth = now.formatter.mN;
    selectedMonthNum = now.month;

    // Ensure initial item matches one of the items in the dropdown list
    if (!years.contains(selectedYear.toString())) {
      selectedYear = int.parse(years.first);
    }
    if (!mounths.contains(selectedMonth)) {
      selectedMonth = mounths.first;
      selectedMonthNum = mounths.indexOf(selectedMonth) + 1;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        // Year Picker
        Row(
          children: [
            Expanded(
                child: Text(
              'سال',
              textAlign: TextAlign.center,
              style: AppTextStyles.body2.copyWith(
                  color: Theme.of(context).colorScheme.onSurface,
                  fontWeight: FontWeight.bold),
            )),
            Expanded(
                child: Text('ماه',
                    textAlign: TextAlign.center,
                    style: AppTextStyles.body2.copyWith(
                        color: Theme.of(context).colorScheme.onSurface,
                        fontWeight: FontWeight.bold))),
          ],
        ),
        const SizedBox(
          height: 12,
        ),
        Row(
          children: [
            Expanded(
              child: SimpleDropdown(
                initialItem: selectedYear.toString(),
                list: years,
                onSelect: (value) {
                  setState(() {
                    selectedYear = int.parse(years[value]);
                    if (selectedYear == Jalali.now().year) {
                      while (mounths.length == Jalali.now().month + 1) {
                        mounths.removeLast();
                      }
                    } else {
                      mounths = [...ms];
                    }
                  });
                },
              ),
            ),
            const SizedBox(
              width: 24,
            ),
            Expanded(
              child: SimpleDropdown(
                initialItem: selectedMonth.toString(),
                list: mounths,
                onSelect: (value) {
                  setState(() {
                    selectedMonth = mounths[value];
                    selectedMonthNum = value + 1;
                  });
                },
              ),
            ),
          ],
        ),
        const SizedBox(
          height: 12,
        ),

        Row(
          children: [
            Expanded(
              child: LoadingButton(
                width: double.infinity,
                onPressed: () {
                  final selectedDate =
                      Jalali(selectedYear, selectedMonthNum, 1);
                  widget.onDateSelected(selectedDate);
                  context.pop();
                },
                color: Theme.of(context).colorScheme.primary,
                child: Text(
                  'تأیید',
                  style: AppTextStyles.body4.copyWith(
                      color: Colors.white, fontWeight: FontWeight.bold),
                ),
              ),
            ),
            const SizedBox(
              width: 24,
            ),
            Expanded(
              child: LoadingButton(
                  width: double.infinity,
                  onPressed: () {
                    context.pop();
                  },
                  isOutlined: true,
                  color: Theme.of(context).colorScheme.primary,
                  backgroundColor:
                      Theme.of(context).dialogTheme.backgroundColor,
                  child: Text(
                    'لغو',
                    style: AppTextStyles.body4.copyWith(
                        color: Theme.of(context).colorScheme.primary,
                        fontWeight: FontWeight.bold),
                  )),
            ),
          ],
        )
      ],
    );
  }
}
