
import 'package:cpt_services/components/chooseVisitTimeContent_vm.dart';
import 'package:cpt_services/modules/services/service_order_confirm/service_order_confirm_vm.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/date_time_utils.dart';
import 'package:shared/utils/log_utils.dart';

import 'package:widgets/dialog/dialog_content_wrap.dart';

import '../../../respository/services_respository.dart';
import 'chooseVisitTimeTitle_state.dart';

part 'chooseVisitTimeTitle_vm.g.dart';


List<Map<String, dynamic>> dayInfoList = [
  {
    "id": 1,
    "day": 'Tody',
    "date": '2022-01-01',
    "isSelected": true,
    "isIntrady": true,
  },
  {
    "id": 2,
    "day": 'Tommorow',
    "date": '2022-01-01',
    "isSelected": false,
    "isIntrady": false,
  },
  {
    "id": 3,
    "day": '周三',
    "date": '2022-01-01',
    "isSelected": false,
    "isIntrady": false,
  },
  {
    "id": 4,
    "day": '周四',
    "date": '2022-01-01',
    "isSelected": false,
    "isIntrady": false,
  },
  {
    "id": 5,
    "day": '周五',
    "date": '2022-01-01',
    "isSelected": false,
    "isIntrady": false,
  },
  {
    "id": 6,
    "day": '周六',
    "date": '2022-01-01',
    "isSelected": false,
    "isIntrady": false,
  },
  {
    "id": 7,
    "day": '周天',
    "date": '2022-01-01',
    "isSelected": false,
    "isIntrady": false,
  },
];




@riverpod
class ChooseVisitTimeTitleVm extends _$ChooseVisitTimeTitleVm {
  late ServicesRespository serviceRespositoryInstance;

  int _rencentDays = 7;

  formatWeekday(int index, String day){
    switch (index) {
      case 0:
        return 'Today';
      case 1:
        return 'Tomorrow';
      default:
        return day;
    }
  }

  ChooseVisitTimeTitleState initState(){
    Log.d("--------------------------initState---------------------");
    List<DayInfoItem> dayInfoListNew = [];
    // 从当前天开始 生成一份最近一个星期的 DayInfoItem
    DateTime now = DateTime.now();
    // Log.d("当天  $now");
    for(int i = 0; i < _rencentDays; i++){
      DateTime date = now.add(Duration(days: i));
      String day = DateTimeUtils.getWeekday(date, languageCode: 'en');
      String dateStr = date.toString().substring(0, 10);
      DayInfoItem newItem = DayInfoItem()
        ..id = i
        ..day = day = formatWeekday(i, day)
        ..date = dateStr
        ..isSelected = i == 0
        ..isIntrady = i == 0
        ..holiday = false
        ..enable = true;
      dayInfoListNew.add(newItem);
      // Log.d("i=$i, date=$date, weekday=$day");
    }


    return ChooseVisitTimeTitleState(
      dayInfoList: dayInfoListNew,
    );
  }


  @override
  ChooseVisitTimeTitleState build(){
    // 引入数据仓库
    serviceRespositoryInstance = ref.read(servicesRespositoryProvider);
    ChooseVisitTimeTitleState state = initState();
    Log.d("--------------------------build---------------------");

    return state;
  }


  handlerChangeNum(BuildContext context, int num, int index){
    // state.dayInfoList[index].tu = num;
    // state = state.copyWith(
    //   dayInfoList: state.dayInfoList,
    // );
  }

  // 点击某一天
  handlerClicKSomeDay(BuildContext context, DayInfoItem dayInfoItem, int index){
    state.currentSelectIndex = index;
    // 循环 state.dayInfoList 需要索引值为index 的 设置isSelected 为true 其他设置为false
    state.dayInfoList?.asMap().forEach((idx, value) {
      if(idx == index){
        value.isSelected = true;
      }else{
        value.isSelected = false;
      }
    });

    state = state.copyWith(
      currentSelectIndex: state.currentSelectIndex,
      dayInfoList: state.dayInfoList,
    );

    // 触发 chooseVisitTimeContent 的刷新
    final chooseVisitTimeContentVm = ref.read(chooseVisitTimeContentVmProvider.notifier);
    chooseVisitTimeContentVm?.getVisitTimeList(context, dayInfoItem.date!);
  }

  // 点击某一时间段
  handlerTimeItemClick(BuildContext context, int index, bool isSelected){
    // 修改 状态
    // state.dayInfoList[index].isChecked = isSelected;
    // state = state.copyWith(
    //   dayInfoList: state.dayInfoList,
    // );
    //
    // ref.read(serviceOrderConfirmVmProvider.notifier).setConfirmOrderTotalPrice(context);
  }
}
