import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flyinsono/pages/schedule/calendar/calendar_page/month_calendar/schedule_item.dart';
import 'package:flyinsono/pages/schedule/calendar/models.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';

class ScheduleList extends FStatefulWidget {
  const ScheduleList({
    Key? key,
    required this.maxLines,
    required this.scheduleDataList,
    required this.onTapShowMore,
  }) : super(key: key);

  /// 可容纳的最大日程行数
  final int maxLines;

  /// 每行日程的高度
  final double itemHeight = 20;

  /// 日程数据
  final List<ScheduleModel> scheduleDataList;

  /// 点击显示更多
  final VoidCallback onTapShowMore;

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

class ScheduleListState extends FState<ScheduleList> {
  final ScrollController _scrollController = ScrollController();

  /// 容纳的数量
  int _itemCount = 0;
  @override
  void initState() {
    super.initState();
    _itemCount = widget.scheduleDataList.length > widget.maxLines
        ? widget.maxLines
        : widget.scheduleDataList.length;
  }

  @override
  void didUpdateWidget(ScheduleList oldWidget) {
    super.didUpdateWidget(oldWidget);
    _itemCount = widget.scheduleDataList.length > widget.maxLines
        ? widget.maxLines
        : widget.scheduleDataList.length;
  }

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      padding: const EdgeInsets.symmetric(horizontal: 2),
      child: _buildScheduleTypeList(),
    );
  }

  FWidget _buildScheduleTypeList() {
    return FListView.builder(
      addAutomaticKeepAlives: false,
      controller: _scrollController,
      addRepaintBoundaries: true,
      itemExtent: widget.itemHeight,
      itemCount: _itemCount,
      itemBuilder: (context, index) {
        return _buildScheduleTypeItem(index);
      },
    );
  }

  FWidget _buildScheduleTypeItem(int scheduleIndex) {
    if (scheduleIndex == _itemCount - 1) {
      if (_itemCount < widget.scheduleDataList.length) {
        return _buildMoreItemButton(
          widget.scheduleDataList.length - _itemCount + 1,
        );
      }
    }
    ScheduleModel schedule = widget.scheduleDataList[scheduleIndex];
    return SchedualItem(
      scheduleData: schedule,
      itemHeight: widget.itemHeight,
    );
  }

  /// 构建【显示更多】按钮
  FWidget _buildMoreItemButton(int moreNum) {
    return FMaterial(
      color: Colors.transparent,
      child: FInkWell(
        customBorder: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5),
        ),
        onTap: () {
          widget.onTapShowMore.call();
        },
        hoverColor: const Color.fromARGB(255, 227, 228, 228),
        child: Container(
          height: widget.itemHeight,
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          color: Colors.transparent,
          child: Row(
            children: [
              Container(
                width: 6,
                height: 6,
                decoration: BoxDecoration(
                  color: Colors.transparent,
                  borderRadius: BorderRadius.circular(5),
                ),
              ),
              const SizedBox(width: 5),
              Expanded(
                child: Text(
                  "${i18nBook.calendar.moreItems.translate([
                        moreNum.toString()
                      ])}..."
                      .breakWord(),
                  style: const TextStyle(
                    fontSize: 12,
                    color: Colors.black38,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
