// ignore_for_file: unnecessary_set_literal

/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-05-15 16:33:28
 * @LastEditTime : 2023-05-11 15:20:41
 * @FilePath     : /wisdom-huiguanjia-app/task_package/lib/src/view_models/schedule_store.dart
 * @Description  : 任务日程逻辑
 */
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import '../../src/impl/task_service_impl.dart';
import '../../src/models/my_time_line.dart';
import '../../src/pages/schedule/index.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
part 'schedule_store.g.dart';

class ScheduleStore = _ScheduleStore with _$ScheduleStore, ChangeNotifier;

abstract class _ScheduleStore with Store {
  ///上下文（请勿修改）
  final BuildContext context1;

  ///路由参数（请勿修改）
  SchedulePageArguments get arguments =>
      SchedulePageArguments.fromRouteArguments(context1);

  _ScheduleStore(this.context1) {
    ///可在此处理进入页面前的操作，比如响应路由的传参进行赋值。
  }

  /// 我的日程
  final scheduleScrViewKey = GlobalKey<WisScrollViewState>();
  ScrollController viewController = new ScrollController();
  final monthItemKey1 = new GlobalKey();
  final monthItemKey2 = new GlobalKey();
  final monthItemKey3 = new GlobalKey();
  final monthItemKey4 = new GlobalKey();
  final monthItemKey5 = new GlobalKey();
  final monthItemKey6 = new GlobalKey();
  final monthItemKey7 = new GlobalKey();
  final monthItemKey8 = new GlobalKey();
  final monthItemKey9 = new GlobalKey();
  final monthItemKey10 = new GlobalKey();
  final monthItemKey11 = new GlobalKey();
  final monthItemKey12 = new GlobalKey();

  ///当前月份
  @observable
  int monthIndex = 0;
  @action
  onSetMonthIndex(index) {
    monthIndex = index;
    onScrollToIndex(index + 1);
  }

  whichKey(month) {
    switch (month) {
      case 1:
        return monthItemKey1;
      case 2:
        return monthItemKey2;
      case 3:
        return monthItemKey3;
      case 4:
        return monthItemKey4;
      case 5:
        return monthItemKey5;
      case 6:
        return monthItemKey6;
      case 7:
        return monthItemKey7;
      case 8:
        return monthItemKey8;
      case 9:
        return monthItemKey9;
      case 10:
        return monthItemKey10;
      case 11:
        return monthItemKey11;
      case 12:
        return monthItemKey12;
    }
  }

  onScrollToIndex(month) {
    var key = whichKey(month);

    ///获取 renderBox
    RenderBox renderBox = key.currentContext.findRenderObject();

    ///获取位置偏移，基于 ancestor: SingleChildScrollView 的 RenderObject()
    double dy = renderBox
        .localToGlobal(Offset.zero,
            ancestor: scheduleScrViewKey.currentContext!.findRenderObject())
        .dy;

    ///计算真实位移
    var offset = dy + viewController.offset;

    viewController.animateTo(offset,
        duration: Duration(milliseconds: 500), curve: Curves.linear);
  }

  @observable
  ObservableMap<String, dynamic> params = ObservableMap.of({
    'year': DateTime.now().year,
  });

  ///年份筛选
  onYearPick(current) {
    params['year'] = current;
    onResetTimeLine();
    onGetMyTimeLine();
    onSetMonthIndex(0);
  }

  Future onGetData() async {
    await onGetMyTimeLine();
  }

  @observable
  ObservableList<MyTimeLineModel> myTimeLine1 = ObservableList.of([]);
  @action
  void addItem1(MyTimeLineModel data) => myTimeLine1.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine2 = ObservableList.of([]);
  @action
  void addItem2(MyTimeLineModel data) => myTimeLine2.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine3 = ObservableList.of([]);
  @action
  void addItem3(MyTimeLineModel data) => myTimeLine3.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine4 = ObservableList.of([]);
  @action
  void addItem4(MyTimeLineModel data) => myTimeLine4.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine5 = ObservableList.of([]);
  @action
  void addItem5(MyTimeLineModel data) => myTimeLine5.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine6 = ObservableList.of([]);
  @action
  void addItem6(MyTimeLineModel data) => myTimeLine6.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine7 = ObservableList.of([]);
  @action
  void addItem7(MyTimeLineModel data) => myTimeLine7.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine8 = ObservableList.of([]);
  @action
  void addItem8(MyTimeLineModel data) => myTimeLine8.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine9 = ObservableList.of([]);
  @action
  void addItem9(MyTimeLineModel data) => myTimeLine9.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine10 = ObservableList.of([]);
  @action
  void addItem10(MyTimeLineModel data) => myTimeLine10.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine11 = ObservableList.of([]);
  @action
  void addItem11(MyTimeLineModel data) => myTimeLine11.add(data);
  @observable
  ObservableList<MyTimeLineModel> myTimeLine12 = ObservableList.of([]);
  @action
  void addItem12(MyTimeLineModel data) => myTimeLine12.add(data);

  @observable
  bool isListEmpty = true;

  /// 获取日程列表
  @action
  Future onGetMyTimeLine() async {
    // WLoadingUtil.show();
    final res = await TaskServiceImpl.instance!.getMyTimeLine(params);
    if (res.data != null && res.data!.length > 0) {
      isListEmpty = false;
      res.data!.forEach((e) => {
            e.month = (e.executionTime!.split('-')[1]).startsWith('0')
                ? (e.executionTime!.split('-')[1]).substring(1)
                : e.executionTime!.split('-')[1],
            e.date = e.executionTime!.split('-')[2],
            if (e.month == '1')
              {
                addItem1(e),
              }
            else if (e.month == '2')
              {
                addItem2(e),
              }
            else if (e.month == '3')
              {
                addItem3(e),
              }
            else if (e.month == '4')
              {
                addItem4(e),
              }
            else if (e.month == '5')
              {
                addItem5(e),
              }
            else if (e.month == '6')
              {
                addItem6(e),
              }
            else if (e.month == '7')
              {
                addItem7(e),
              }
            else if (e.month == '8')
              {
                addItem8(e),
              }
            else if (e.month == '9')
              {
                addItem9(e),
              }
            else if (e.month == '10')
              {
                addItem10(e),
              }
            else if (e.month == '11')
              {
                addItem11(e),
              }
            else if (e.month == '12')
              {
                addItem12(e),
              }
          });

      /// widget加载完毕 回调
      WidgetsBinding.instance.addPostFrameCallback((_) {
        onSetMonthIndex(DateTime.now().month - 1);
        onScrollToIndex(DateTime.now().month);
      });
    } else {
      isListEmpty = true;
    }
  }

  @action
  void onResetTimeLine() {
    myTimeLine1.clear();
    myTimeLine2.clear();
    myTimeLine3.clear();
    myTimeLine4.clear();
    myTimeLine5.clear();
    myTimeLine6.clear();
    myTimeLine7.clear();
    myTimeLine8.clear();
    myTimeLine9.clear();
    myTimeLine10.clear();
    myTimeLine11.clear();
    myTimeLine12.clear();
  }

  ///判断当前月的日程是否为空
  // ignore: missing_return
  bool? isCurrentEmpty(month) {
    switch (month) {
      case 1:
        return myTimeLine1.length == 0;
      case 2:
        return myTimeLine2.length == 0;
      case 3:
        return myTimeLine3.length == 0;
      case 4:
        return myTimeLine4.length == 0;
      case 5:
        return myTimeLine5.length == 0;
      case 6:
        return myTimeLine6.length == 0;
      case 7:
        return myTimeLine7.length == 0;
      case 8:
        return myTimeLine8.length == 0;
      case 9:
        return myTimeLine9.length == 0;
      case 10:
        return myTimeLine10.length == 0;
      case 11:
        return myTimeLine11.length == 0;
      case 12:
        return myTimeLine12.length == 0;
      default:
        return false;
    }
  }

  /// 跳转详情
  toTaskDetail(
    id,
  ) async {
    await Navigator.pushNamed(
      context1,
      'task/detail',
      arguments: {
        'id': id,
      },
    );
  }

  /// 跳转详情
  toPlanTaskDetail(item) async {
    await Navigator.pushNamed(
      context1,
      'task/plan_detail',
      arguments: {
        'id': item.id,
        'limitedTime': item.limitedTime,
        'executionTime': item.executionTime,
      },
    );
  }
}
