
import 'package:domain/entity/paid_service_detail_entity.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/log_utils.dart';

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

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

part 'chooseAirConditionContent_vm.g.dart';



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

  List<PaidServiceDetailProducts> _products = [];

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



  ChooseAirConditionContentState initState(){
    Log.d("--------------------------initState---------------------");
    if(airConditionList.isNotEmpty){
      List<AirConditionContentItem> airConditionListNew = [];
      airConditionList.forEach((item) {
        AirConditionContentItem newItem = AirConditionContentItem()
          ..id = item['id']
          ..price = double.tryParse(item['price'] ?? '')
          ..name = item['name']
          ..num = 0;
        airConditionListNew.add(newItem);
      });
      return ChooseAirConditionContentState(
        airConditionList: airConditionListNew,
      );
    }else {
      return ChooseAirConditionContentState(
        airConditionList: [],
      );
    }

  }

  @override
  ChooseAirConditionContentState build(){
    // 引入数据仓库
    serviceRespositoryInstance = ref.read(servicesRespositoryProvider);
    ChooseAirConditionContentState 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<AirConditionContentItem> airConditionListNew = [];
          products.forEach((item) {
            AirConditionContentItem newItem = AirConditionContentItem()
              ..id = item.id
              ..price = (item.price ?? 0.0).toDouble()
              ..name = item.name
              ..num = 0;
            airConditionListNew.add(newItem);
          });

          state = state.copyWith(
            airConditionList: airConditionListNew,
          );
        }
      }
    });
  }


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