/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-05-15 19:34:32
 * @LastEditTime : 2022-02-24 09:55:16
 * @FilePath     : /app-boost/task_package/lib/src/pages/schedule/components/month_info.dart
 * @Description  : 我的日程-月份列表
 */
part of '../index.dart';

class _BuidlMonthInfo extends StatelessWidget {
  const _BuidlMonthInfo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<ScheduleStore>(context);

    return WisScrollView(
      key: store.scheduleScrViewKey,
      controller: store.viewController,
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: Observer(
              name: 'empty',
              builder: (context) {
                return Column(
                  children: <Widget>[
                    if (store.isListEmpty) ...[
                      WEmptyWidget(text: Text('空空如也~'))
                    ] else ...[
                      _BuildMonthList()
                    ]
                  ],
                );
              }),
        ),
      ],
    ).asExpanded();
  }
}

class _BuildMonthList extends StatelessWidget {
  const _BuildMonthList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<ScheduleStore>(context, listen: false);

    return Wisdom.column(
      children: [
        _BuidlInfoItem(
            key: store.monthItemKey1,
            month: '一月',
            items: _BuildItemList(month: 1)),
        _BuidlInfoItem(
            key: store.monthItemKey2,
            month: '二月',
            items: _BuildItemList(month: 2)),
        _BuidlInfoItem(
            key: store.monthItemKey3,
            month: '三月',
            items: _BuildItemList(month: 3)),
        _BuidlInfoItem(
            key: store.monthItemKey4,
            month: '四月',
            items: _BuildItemList(month: 4)),
        _BuidlInfoItem(
            key: store.monthItemKey5,
            month: '五月',
            items: _BuildItemList(month: 5)),
        _BuidlInfoItem(
            key: store.monthItemKey6,
            month: '六月',
            items: _BuildItemList(month: 6)),
        _BuidlInfoItem(
            key: store.monthItemKey7,
            month: '七月',
            items: _BuildItemList(month: 7)),
        _BuidlInfoItem(
            key: store.monthItemKey8,
            month: '八月',
            items: _BuildItemList(month: 8)),
        _BuidlInfoItem(
            key: store.monthItemKey9,
            month: '九月',
            items: _BuildItemList(month: 9)),
        _BuidlInfoItem(
            key: store.monthItemKey10,
            month: '十月',
            items: _BuildItemList(month: 10)),
        _BuidlInfoItem(
            key: store.monthItemKey11,
            month: '十一月',
            items: _BuildItemList(month: 11)),
        _BuidlInfoItem(
            key: store.monthItemKey12,
            month: '十二月',
            items: _BuildItemList(month: 12)),
      ],
    );
  }
}

/// 月份项
class _BuidlInfoItem extends StatelessWidget {
  const _BuidlInfoItem({Key? key, this.items, this.month}) : super(key: key);
  final Widget? items;
  final month;
  // final GlobalKey key = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return Wisdom.column(
      padding: EdgeInsets.fromLTRB(
        15.pt,
        0,
        15.pt,
        15.pt,
      ),
      color: Colors.white,
      margin: EdgeInsets.only(top: 10.pt),
      children: [
        Wisdom.row(
          margin: EdgeInsets.symmetric(
            vertical: 15.pt,
          ),
          children: [
            Wisdom(
              color: colorScheme.primary,
              width: 3.pt,
              height: 14.pt,
              margin: EdgeInsets.only(
                right: 10.pt,
              ),
            ),
            WisText.c333(
              '$month',
              size: 16.pt,
              weight: FontWeight.bold,
            ),
          ],
        ),
        items!,
      ],
    );
  }
}

/// 月份任务列表
class _BuildItemList extends StatelessWidget {
  const _BuildItemList({
    Key? key,
    this.month,
  }) : super(key: key);
  final month;
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<ScheduleStore>(context, listen: false);

    return Wisdom(
      child: Observer(
        builder: (_) {
          return Wisdom.column(
            children: [
              if (month == 1 && store.myTimeLine1.length > 0) ...[
                for (var item in store.myTimeLine1) _BuildTaskItem(item: item),
              ],
              if (month == 2 && store.myTimeLine2.length > 0) ...[
                for (var item in store.myTimeLine2) _BuildTaskItem(item: item),
              ],
              if (month == 3 && store.myTimeLine3.length > 0) ...[
                for (var item in store.myTimeLine3) _BuildTaskItem(item: item),
              ],
              if (month == 4 && store.myTimeLine4.length > 0) ...[
                for (var item in store.myTimeLine4) _BuildTaskItem(item: item),
              ],
              if (month == 5 && store.myTimeLine5.length > 0) ...[
                for (var item in store.myTimeLine5) _BuildTaskItem(item: item),
              ],
              if (month == 6 && store.myTimeLine6.length > 0) ...[
                for (var item in store.myTimeLine6) _BuildTaskItem(item: item),
              ],
              if (month == 7 && store.myTimeLine7.length > 0) ...[
                for (var item in store.myTimeLine7) _BuildTaskItem(item: item),
              ],
              if (month == 8 && store.myTimeLine8.length > 0) ...[
                for (var item in store.myTimeLine8) _BuildTaskItem(item: item),
              ],
              if (month == 9 && store.myTimeLine9.length > 0) ...[
                for (var item in store.myTimeLine9) _BuildTaskItem(item: item),
              ],
              if (month == 10 && store.myTimeLine10.length > 0) ...[
                for (var item in store.myTimeLine10) _BuildTaskItem(item: item),
              ],
              if (month == 11 && store.myTimeLine11.length > 0) ...[
                for (var item in store.myTimeLine11) _BuildTaskItem(item: item),
              ],
              if (month == 12 && store.myTimeLine12.length > 0) ...[
                for (var item in store.myTimeLine12) _BuildTaskItem(item: item),
              ],
              if (store.isCurrentEmpty(month)!) ...[
                _BuildEmpty(),
              ]
            ],
          );
        },
      ),
    );
  }
}

/// 任务项
class _BuildTaskItem extends StatelessWidget {
  const _BuildTaskItem({
    Key? key,
    this.item,
  }) : super(key: key);
  final item;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final store = Provider.of<ScheduleStore>(context, listen: false);

    ///tag状态颜色
    final statusColorMap = {
      // 1: '未开始',
      1: colorScheme.orange,
      // 2: '进行中',
      2: colorScheme.blue,
      // 3: '已完成',
      3: colorScheme.success,
      // 4: '超时',
      4: colorScheme.danger,
      // 计划
      5: colorScheme.minor,
      // 6: '取消'
      6: colorScheme.grey,
    };
    return Wisdom.row(
      onTap: () => item?.status == 'plan'
          ? store.toPlanTaskDetail(item)
          : store.toTaskDetail(
              item.id,
            ),
      margin: EdgeInsets.only(bottom: 20.pt),
      children: [
        Wisdom.column(
          children: [
            Wisdom(
              margin: EdgeInsets.only(bottom: 6.pt),
              child: WisText(
                '${item?.date}日',
                weight: FontWeight.bold,
                color: colorScheme.mainTitle,
              ),
            ),
            WisText(
              '${item?.weekDay ?? '--'}',
              weight: FontWeight.bold,
              color: colorScheme.mainTitle,
            ),
          ],
        ),
        Wisdom(
          margin: EdgeInsets.symmetric(horizontal: 15.pt),
          height: 38.pt,
          width: 1.pt,
          color: colorScheme.divider,
        ),
        Wisdom.column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Wisdom.row(
              mainAxisAlignment: MainAxisAlignment.start,
              margin: EdgeInsets.only(
                bottom: 8.pt,
              ),
              children: [
                Text.rich(
                  TextSpan(children: [
                    if (item.status == 'finished')
                      WidgetSpan(
                        child: WTag(
                          margin: EdgeInsets.only(right: 5.pt),
                          child: WisText(
                            '已完成',
                            size: 13.pt,
                          ),
                          color: statusColorMap[3]!,
                        ),
                      ),
                    if (item.status == 'no_open')
                      WidgetSpan(
                        child: WTag(
                          margin: EdgeInsets.only(right: 5.pt),
                          child: WisText(
                            '未开始',
                            size: 13.pt,
                          ),
                          color: statusColorMap[1]!,
                        ),
                      ),
                    if (item.status == 'doing')
                      WidgetSpan(
                        child: WTag(
                          margin: EdgeInsets.only(right: 5.pt),
                          child: WisText(
                            '进行中',
                            size: 13.pt,
                          ),
                          color: statusColorMap[2]!,
                        ),
                      ),
                    if (item.status == 'cancel')
                      WidgetSpan(
                        child: WTag(
                          margin: EdgeInsets.only(right: 5.pt),
                          child: WisText(
                            '终止',
                            size: 13.pt,
                          ),
                          color: statusColorMap[6]!,
                        ),
                      ),
                    if (item.status == 'plan')
                      WidgetSpan(
                        child: WTag(
                          margin: EdgeInsets.only(right: 5.pt),
                          child: WisText(
                            '计划',
                            size: 13.pt,
                          ),
                          color: statusColorMap[5]!,
                        ),
                      ),
                    if (item?.taskTimeout == true)
                      WidgetSpan(
                        child: WTag(
                          margin: EdgeInsets.only(right: 5.pt),
                          child: WisText(
                            '超时',
                            size: 13.pt,
                          ),
                          color: statusColorMap[4]!,
                        ),
                      ),
                    TextSpan(
                      text: '${item?.title ?? '--'}',
                      style: TextStyle(
                        fontSize: 15.pt,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ]),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ).asExpanded(),
              ],
            ),
            WisText(
              '${item?.limitedTime ?? '--'}截止',
              color: colorScheme.placeholder,
              size: 14.pt,
            )
          ],
        ).asExpanded()
      ],
    );
  }
}

/// 任务为空
class _BuildEmpty extends StatelessWidget {
  const _BuildEmpty({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        WisText(
          '当月没有日程哦~~',
          color: colorScheme.placeholder,
          size: 14.pt,
        ),
      ],
    );
  }
}
