
import 'package:domain/entity/paid_service_detail_entity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/log_utils.dart';

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

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

part 'chooseHouseCleanContent_vm.g.dart';


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


  List<PaidServiceDetailProducts> _products = [];

  List<Map<String, dynamic>> houseCleanList = [];


  ChooseHouseCleanContentState initState(){
    Log.d("--------------------------initState---------------------");
    if(houseCleanList.isNotEmpty){
      List<HouseCleanContentItem> houseCleanListNew = [];
      houseCleanList.forEach((item) {
        HouseCleanContentItem newItem = HouseCleanContentItem()
          ..id = item['id']
          ..price = item['price'] as double
          ..name = item['name']
          ..num = 1
          ..areaSizeRange = item['areaSizeRange']
          ..isChecked = item['isChecked']
          ..isDisable = item['isDisable'];
        houseCleanListNew.add(newItem);
      });
      return ChooseHouseCleanContentState(
        houseCleanList: houseCleanListNew,
      );
    }else {
      return ChooseHouseCleanContentState(
        houseCleanList: null,
      );
    }
  }

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

  setInitPageData(BuildContext context, Map<String, dynamic>? params){
    Log.d("--------------------------setInitPageData----------$params-----------");
    _products = params?['products']??[];

    // 渲染一帧后
    WidgetsBinding.instance.addPostFrameCallback((_){
      // 修改 state 中 HouseCleanList
      if(_products != null){
        if(_products != null && _products.isNotEmpty){
          List<PaidServiceDetailProducts> products = _products;
          List<HouseCleanContentItem> houseCleanListNew = [];
          products.forEach((item) {
            HouseCleanContentItem newItem = HouseCleanContentItem()
              ..id = item.id
              ..price = (item.price ?? 0.0).toDouble()
              ..name = item.name
              ..num = 1
              ..areaSizeRange = item.shortDescription
              ..isChecked = false
              ..isDisable = false;
            houseCleanListNew.add(newItem);
          });

          state = state.copyWith(
            houseCleanList: houseCleanListNew,
          );
        }
      }
    });
  }

  // 修改 item 的选中状态
  handlerChangeItemChecked(BuildContext context, int index){
    bool isMultileSelect = state.isMultileSelect?? false;
    if(isMultileSelect == true){
      // 允许多选
      List<HouseCleanContentItem> newHouseCleanList = state.houseCleanList!;
      bool isChecked = newHouseCleanList?[index]?.isChecked??false;
      newHouseCleanList?[index].isChecked = !isChecked;
      state = state.copyWith(
        houseCleanList: newHouseCleanList,
      );
    }else {
      // 只允许单选  需要重置其他item的选择状态
      List<HouseCleanContentItem> newHouseCleanList = List.from(state.houseCleanList!);
      bool isChecked = newHouseCleanList?[index]?.isChecked??false;
      for (int i = 0; i < newHouseCleanList.length; i++) {
        if(i == index){
          if(isChecked){
            newHouseCleanList[i].isChecked = false;
          }else{
            newHouseCleanList[i].isChecked = true;
          }
        }else {
          newHouseCleanList[i].isChecked = false;
        }
      }
      state = state.copyWith(
        houseCleanList: newHouseCleanList,
      );
      Log.d("3433: ${state.houseCleanList}");
    }
  }
}
