/*
 * @Author: WuXiangNan
 * @Date: 2021-03-12 10:32:04
 * @LastEditTime : 2022-01-19 16:40:48
 * @LastEditors  : WuWei
 * @Description: 考勤规则store
 */

import 'package:app_package/app_package.dart';
import 'package:attendance_package/src/models/response_model/attendance_rule/rule_data_model.dart';
import 'package:attendance_package/src/models/response_model/attendance_rule/schedule_calendar_model.dart';
import 'package:attendance_package/src/service/impl/attendance_service_impl.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:flutter_calendar_carousel/classes/event.dart';
import 'package:flutter_calendar_carousel/classes/event_list.dart';

class RuleStore with ChangeNotifier {
  final BuildContext context;

  late Map args;
  RuleStore(this.context) {
    args =
        ModalRoute.of(context)!.settings.arguments as Map<dynamic, dynamic>? ??
            {};
  }

  RuleModel ruleData = RuleModel();
  List? legWork;

  DateTime currentDate = DateTime.now();
  DateTime currentDate2 = DateTime.now();
  String currentMonth = DateFormat('yyyy-MM').format(DateTime.now());
  DateTime targetDateTime = DateTime.now();
  EventList<Event> calendarEvent = EventList<Event>(events: {});
  List<ScheduleCalendarModel>? scheduleCalendarData;

  ScheduleCalendarModel? selectedDay;

  //当前员工的信息
  final authGlobalStore = GetIt.I.get<AuthGlobalStore>();
  //当前员工的信息
  EmployeesInfoModel? get userInfo => authGlobalStore.info;

  // 获取考勤规则
  Future<void> onGetAttendanceRule(String? id) async {
    WLoadingUtil.show();
    final res = await AttendanceServiceImpl().getRuleShow({
      'companyId': userInfo!.companyId,
      // 'companyId': '136523944394149888',
      'groupIdentification': id,
    });

    ruleData = res.data!;
    legWork = res.data?.legWorkSetting?.split(',') ?? [];
    notifyListeners();
    WLoadingUtil.hide();
  }

  // 获取考勤规则 排班制日历
  Future<void> onGetScheduleCalendar(String date) async {
    WLoadingUtil.show();
    final res = await AttendanceServiceImpl().getScheduleCalendar({
      'companyId': userInfo!.companyId,
      // 'companyId': '136523944394149888',
      'yearMonth': date,
      'employeeId': userInfo!.id,
      // 排班制
      // 'employeeId': '136884611961634816',
      // 固定班制
      // 'employeeId': '284394842658852864',
    });
    scheduleCalendarData = res.data;

    if (currentMonth == DateFormat('yyyy-MM').format(DateTime.now()))
      selectedDay = scheduleCalendarData![DateTime.now().day - 1];

    initEvent(DateTime.now());
    notifyListeners();
    WLoadingUtil.hide();
  }

  void initEvent(DateTime date) {
    calendarEvent = EventList<Event>(events: {});
    date = DateTime(date.year, date.month, date.day);
    for (ScheduleCalendarModel item in scheduleCalendarData ?? []) {
      var day = DateTime(targetDateTime.year, targetDateTime.month, item.day!);

      late Color color;
      late String text;
      // 1：已出勤；0：正常；3：休息；
      switch (item.status) {
        case 0:
          color = Color(0XFF27CEAC);
          // 取班次第一个字显示
          text = item.name!.substring(0, 1);
          // text = item.name!.substring(0, 1) ?? '无';
          break;
        case 1:
          color = Color(0XFF27CEAC);
          text = '已';
          break;
        case 3:
          color = Colors.grey;
          text = '休';
          break;
      }

      calendarEvent.add(
        day,
        Event(
          date: day,
          title: text,
          dot: WisText(
            text,
            size: 12.pt,
            color: day == date ? Colors.white : color,
          ),
        ),
      );
      notifyListeners();
    }
  }

  void preMonth() {
    targetDateTime = DateTime(targetDateTime.year, targetDateTime.month - 1);
    currentMonth = DateFormat('yyyy-MM').format(targetDateTime);
    notifyListeners();
    onGetScheduleCalendar(currentMonth);
  }

  void nextMonth() {
    targetDateTime = DateTime(targetDateTime.year, targetDateTime.month + 1);
    currentMonth = DateFormat('yyyy-MM').format(targetDateTime);
    notifyListeners();
    onGetScheduleCalendar(currentMonth);
  }

  void calendarOnChange(DateTime date) {
    targetDateTime = date;
    currentMonth = DateFormat('yyyy-MM').format(targetDateTime);
    notifyListeners();
    onGetScheduleCalendar(currentMonth);
  }

  void calendarOnPressed(DateTime date, List<Event> events) {
    currentDate2 = date;
    selectedDay = scheduleCalendarData![date.day - 1];
    initEvent(date);
    notifyListeners();
  }
}
