import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';
import 'package:flyinsono/views/mobile/mobile_calendar/widgets/month_view_calendar_day.dart';

class MonthViewCalendarPage extends FStatelessWidget {
  MonthViewCalendarPage({super.key, required this.dayOfMonth});
  final DateTime dayOfMonth;

  @override
  FWidget build(BuildContext context) {
    return _buildMonthCalendarDaysBody();
  }

  /// 构建日历主体 [为了提高性能直接写死]
  FWidget _buildMonthCalendarDaysBody() {
    final monthViewDaysList = CalendarUtils.get42DaysListByDateTime(dayOfMonth);
    return FColumn(
      mainAxisSize: MainAxisSize.max,
      children: [
        _buildMonthCalendarDayRow(monthViewDaysList.sublist(0, 7), 0),
        _buildMonthCalendarDayRow(monthViewDaysList.sublist(7, 14), 7),
        _buildMonthCalendarDayRow(monthViewDaysList.sublist(14, 21), 14),
        _buildMonthCalendarDayRow(monthViewDaysList.sublist(21, 28), 21),
        _buildMonthCalendarDayRow(monthViewDaysList.sublist(28, 35), 28),
        _buildMonthCalendarDayRow(monthViewDaysList.sublist(35, 42), 35),
      ],
    );
  }

  /// 构建每一行（周）
  FWidget _buildMonthCalendarDayRow(List<DateTime> sevenDays, int startIndex) {
    return FExpanded(
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: sevenDays
            .asMap()
            .map((index, day) => MapEntry(
                index,
                _buildEachDay(
                  day,
                  startIndex + index,
                )))
            .values
            .toList(
              growable: false,
            ),
      ),
    );
  }

  /// 构建每一天
  FWidget _buildEachDay(DateTime day, int index) {
    return FExpanded(
      child: FContainer(
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Colors.black12,
              width: 0.5,
            ),
          ),
        ),
        height: double.infinity,
        child: MonthViewCalendarDay(
          date: day,
          indexInPage: index,
          parentPageDayOfMonth: dayOfMonth,
        ),
      ),
    );
  }
}
