import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/views/mobile/mobile_calendar/controller.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';

/// 移动端月历的每一格日期
class MonthViewScheduleRows extends FisView<MobileCalendarController> {
  MonthViewScheduleRows(
      {Key? key, required this.date, required this.indexInPage})
      : super(key: key);

  final DateTime date;
  final int indexInPage;

  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      /// 如果 schedules 数量大于 maxLines，则显示 maxLines - 1 行，最后一行显示【显示更多】按钮
      final schedules = controller.state.monthViewScheduleList[indexInPage];
      final maxLines = controller.state.monthViewScheduleMaxLines;
      final showMore = schedules.length >= maxLines;
      final itemCount = showMore ? maxLines - 1 : maxLines;
      return FColumn(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ...schedules.take(itemCount).map((schedule) => FContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                  color: schedule.type.color.withOpacity(0.2),
                  borderRadius: BorderRadius.circular(2),
                ),
                margin: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
                padding: EdgeInsets.symmetric(horizontal: 2),
                height: 12,
                child: FText(
                  "${schedule.title}".breakWord(),
                  style: TextStyle(
                    fontSize: 9,
                    color: schedule.type.color,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.clip,
                ),
              )),
          if (showMore) _buildMoreItemRow(schedules.length - maxLines + 1),
        ],
      );
    });
  }

  /// 构建 [还有x项] 一行
  FWidget _buildMoreItemRow(int moreNum) {
    return FContainer(
      width: double.infinity,
      margin: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
      padding: EdgeInsets.symmetric(horizontal: 2),
      height: 12,
      child: FText(
        i18nBook.calendar.moreItems.translate([moreNum.toString()]).breakWord(),
        style: TextStyle(
          fontSize: 9,
          color: Colors.grey,
        ),
        maxLines: 1,
        overflow: TextOverflow.clip,
      ),
    );
  }
}
