import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flyinsono/pages/schedule/calendar/controller.dart';
import 'package:flyinsono/pages/schedule/calendar/models.dart';
import 'package:flyinsono/pages/schedule/calendar/calendar_page/mini_calendar/mini_day_item.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MiniCalendar extends FStatefulWidget implements FInteractiveContainer {
  const MiniCalendar({super.key});

  @override
  FState<MiniCalendar> createState() => _MiniCalendarState();

  @override
  String get pageName => "MiniCalendar";
}

class _MiniCalendarState extends FState<MiniCalendar> {
  String currYearMonth = ''; // 当前年月
  List<MiniViewDayModel> daysList = []; // 当前控制器的日期列表
  CalendarController calendarController = Get.find<CalendarController>();
  @override
  void initState() {
    super.initState();
    currYearMonth = calendarController.currentYearMonth;
    daysList = calendarController.miniViewDaysList;
    calendarController.onDaysListChange.addListener(_onDaysListChange);
  }

  @override
  void dispose() {
    super.dispose();
    calendarController.onDaysListChange.removeListener(_onDaysListChange);
  }

  void _onDaysListChange(_, e) {
    setState(() {
      currYearMonth = calendarController.currentYearMonth;
      daysList = calendarController.miniViewDaysList;
    });
  }

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      children: <FWidget>[
        _buildMiniCalendarTop(),
        _buildMiniCalendarBody(),
        const FSizedBox(
          height: 10,
        ),
      ],
    );
  }

  FWidget _buildMiniCalendarTop() {
    /// 按钮样式
    final iconButtonStyle = ButtonStyle(
      padding: MaterialStateProperty.all<EdgeInsetsGeometry>(
          const EdgeInsets.all(0)),
      backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
      shadowColor: MaterialStateProperty.all<Color>(Colors.transparent),
      overlayColor: MaterialStateProperty.all<Color>(
          const Color.fromARGB(255, 224, 226, 228)),
      foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
      surfaceTintColor: MaterialStateProperty.all<Color>(Colors.white),
    );
    return FSizedBox(
      height: 40,
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <FWidget>[
          const FSizedBox(
            width: 10,
          ),
          FText(
            currYearMonth,
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
          FExpanded(child: FContainer()),
          FSizedBox(
            width: 32,
            height: 32,
            child: FElevatedButton(
              businessParent: widget,
              name: 'trun to last month',
              style: iconButtonStyle,
              onPressed: calendarController.handleLastMonth,
              child: const FIcon(
                Icons.keyboard_arrow_left,
                size: 20,
                color: Colors.black38,
              ),
            ),
          ),
          FSizedBox(
            width: 32,
            height: 32,
            child: FElevatedButton(
              businessParent: widget,
              name: 'trun to next month',
              style: iconButtonStyle,
              onPressed: calendarController.handleNextMonth,
              child: const FIcon(
                Icons.keyboard_arrow_right,
                size: 20,
                color: Colors.black38,
              ),
            ),
          ),
        ],
      ),
    );
  }

  FWidget _buildMiniCalendarBody() {
    return FContainer(
      padding: const EdgeInsets.symmetric(horizontal: 5),
      child: FColumn(
        children: <FWidget>[
          _buildMiniCalendarWeekTitle(),
          const FSizedBox(
            height: 5,
          ),
          _buildMiniCalendarDaysBody(),
        ],
      ),
    );
  }

  /// 构建「日、一、二、三、四、五、六」的标题
  FWidget _buildMiniCalendarWeekTitle() {
    return FSizedBox(
      height: 30,
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <FWidget>[
          _buildEachTitle(i18nBook.calendar.singleSunday.t),
          _buildEachTitle(i18nBook.calendar.singleMonday.t),
          _buildEachTitle(i18nBook.calendar.singleTuesday.t),
          _buildEachTitle(i18nBook.calendar.singleWednesday.t),
          _buildEachTitle(i18nBook.calendar.singleThursday.t),
          _buildEachTitle(i18nBook.calendar.singleFriday.t),
          _buildEachTitle(i18nBook.calendar.singleSaturday.t),
        ],
      ),
    );
  }

  FWidget _buildEachTitle(String title) {
    const weekTextStyle = TextStyle(fontSize: 12, color: Colors.black54);
    return FSizedBox(
      width: 25,
      child: FText(
        title,
        style: weekTextStyle,
        textAlign: TextAlign.center,
      ),
    );
  }

  /// 构建日历主体
  FWidget _buildMiniCalendarDaysBody() {
    // 将35天分成5个周
    final weeksInMonth = <List<MiniViewDayModel>>[];
    for (var i = 0; i < daysList.length; i += 7) {
      weeksInMonth.add(daysList.sublist(i, i + 7));
    }
    return FSizedBox(
      child: FColumn(
        children: weeksInMonth.map(_buildMiniCalendarDayRow).toList(),
      ),
    );
  }

  /// 构建每一行（周）
  FWidget _buildMiniCalendarDayRow(List<MiniViewDayModel> sevenDays) {
    return FSizedBox(
      height: 32,
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: sevenDays.map(_buildEachDay).toList(),
      ),
    );
  }

  /// 构建每一天
  FWidget _buildEachDay(MiniViewDayModel day) {
    return MiniDayItem(
      dayData: day,
      onSelect: (v) => calendarController.handleSelectedDayByIndex(v),
    );
  }
}
