import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/schedule/calendar/calendar_page/month_calendar/schedule_item.dart';
import 'package:flyinsono/pages/schedule/calendar/controller.dart';
import 'package:flyinsono/pages/schedule/calendar/models.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';
import 'package:get/get.dart';

class MoreSchedulePopup extends FStatefulWidget {
  /// 日程数据
  final MonthViewDayModel scheduleData;

  const MoreSchedulePopup({
    Key? key,
    required this.scheduleData,
  }) : super(key: key);

  @override
  FState createState() => MoreSchedulePopupState();
}

class MoreSchedulePopupState extends FState<MoreSchedulePopup> {
  static const double _itemHeight = 28;
  final ScrollController _scrollController = ScrollController();
  List<ScheduleModel> _scheduleDataList = [];
  CalendarController calendarController = Get.find<CalendarController>();

  @override
  FWidget build(BuildContext context) {
    return FListView(
      shrinkWrap: true,
      children: [
        FContainer(
          padding: const EdgeInsets.all(10),
          child: FColumn(
            mainAxisSize: MainAxisSize.min,
            children: [
              _buildScheduleHead(),
              _buildScheduleList(),
            ],
          ),
        ),
      ],
    );
  }

  @override
  void didUpdateWidget(covariant MoreSchedulePopup oldWidget) {
    super.didUpdateWidget(oldWidget);
    _scheduleDataList =
        calendarController.scheduleListFilter(widget.scheduleData.scheduleList);
  }

  @override
  void initState() {
    super.initState();
    _scheduleDataList =
        calendarController.scheduleListFilter(widget.scheduleData.scheduleList);
  }

  FWidget _buildScheduleHead() {
    return FSizedBox(
      height: 35,
      child: FRow(
        children: [
          FText(
            widget.scheduleData.date.day.toString(),
            style: const TextStyle(
              fontSize: 30,
            ),
          ),
          const FSizedBox(width: 5),
          FText(
            CalendarUtils.getLunarDay(widget.scheduleData.date),
            style: const TextStyle(
              fontSize: 11,
              color: Colors.grey,
            ),
          ),
        ],
      ),
    );
  }

  FWidget _buildScheduleItem(int scheduleIndex) {
    ScheduleModel schedule = _scheduleDataList[scheduleIndex];
    return SchedualItem(
      scheduleData: schedule,
      itemHeight: _itemHeight,
    );
  }

  FWidget _buildScheduleList() {
    return FAlwaysScrollListView(
      child: QuickFWidget(ScrollConfiguration(
        behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
        child: FListView.builder(
          shrinkWrap: true,
          addAutomaticKeepAlives: false,
          addRepaintBoundaries: true,
          itemExtent: _itemHeight,
          itemCount: _scheduleDataList.length,
          controller: _scrollController,
          itemBuilder: (context, index) {
            return _buildScheduleItem(index);
          },
        ),
      )),
      scrollController: _scrollController,
    );
  }
}
