import 'package:hmg_patient_app/core/enum/OrderService.dart';
import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/GetCMCAllOrdersResponseModel.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_request_model.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_response_model.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_insert_pres_order_request_model.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/get_cmc_service_response_model.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/HomeHealthCare/get_hhc_all_pres_orders_response_model.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_request_model.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_response_model.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/HomeHealthCare/update_pres_oreder_request_model.dart';
import 'package:hmg_patient_app/core/service/AlHabibMedicalService/cmc_service.dart';
import 'package:hmg_patient_app/core/service/AlHabibMedicalService/customer_addresses_service.dart';
import 'package:hmg_patient_app/core/service/parmacyModule/parmacy_module_service.dart';
import 'package:hmg_patient_app/core/viewModels/base_view_model.dart';
import 'package:hmg_patient_app/main.dart';

import '../../../locator.dart';
import 'add_new_address_Request_Model.dart';

class CMCViewModel extends BaseViewModel {
  CMCService _cMCService = locator<CMCService>();
  CustomerAddressesService _customerAddressesService = locator<CustomerAddressesService>();

  List<AddressInfo> get addressesList => _customerAddressesService.addressesList;

  List<GetCMCServicesResponseModel> get cmcAllServicesList => _cMCService.cmcAllServicesList;

  List<GetCMCAllOrdersResponseModel> get cmcAllPresOrders => _cMCService.cmcAllPresOrdersList;

  List<GetOrderDetailByOrderIDResponseModel> get cmcAllOrderDetail => _cMCService.cmcAllOrderDetail;

  List<CMCGetItemsResponseModel> get checkupItems => _cMCService.checkupItemsList;

  PharmacyModuleService _pharmacyModuleService = locator<PharmacyModuleService>();

  GetCMCAllOrdersResponseModel? pendingOrder;

  bool? get isOrderUpdated => _cMCService.isOrderUpdated;

  Future getCMCAllServices() async {
    await _cMCService.getCMCAllServices();
    if (_cMCService.hasError) {
      error = _cMCService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getCheckupItems({CMCGetItemsRequestModel? cMCGetItemsRequestModel, bool isFirst = false}) async {
    if (!isFirst) setState(ViewState.Busy);
    await _cMCService.getCheckupItems(cMCGetItemsRequestModel!);
    if (_cMCService.hasError) {
      error = _cMCService.error!;
      setState(ViewState.Error);
    }
    if (!isFirst) setState(ViewState.Idle);
  }

  Future getCmcAllPresOrders() async {
    setState(ViewState.Busy);
    // await _cMCService.getCmcAllPresOrders();
    await _cMCService.getCmcAllPresOrdersRC();
    if (_cMCService.hasError) {
      error = _cMCService.error!;
      setState(ViewState.Error);
    } else {
      pendingOrder = _cMCService.cmcAllPresOrdersList.firstWhere((element) => element.statusId == 1 || element.statusId == 2, orElse: () => GetCMCAllOrdersResponseModel());
      if (pendingOrder != null && pendingOrder!.serviceText != null) {
        // await getOrderDetailByOrderID(pendingOrder);
        setState(ViewState.Idle);
      } else {
        getCMCAllServices();
      }
    }
  }

  Future updateCmcPresOrder(UpdatePresOrderRequestModel updatePresOrderRequestModel) async {
    setState(ViewState.BusyLocal);
    // await _cMCService.updateCmcPresOrder(updatePresOrderRequestModel);
    await _cMCService.updateCmcPresOrderRC(updatePresOrderRequestModel);
    if (_cMCService.hasError) {
      error = _cMCService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getOrderDetailByOrderID(GetCMCAllOrdersResponseModel order) async {
    GetOrderDetailByOrderIDRequestModel getOrderDetailByOrderIDRequestModel = GetOrderDetailByOrderIDRequestModel(presOrderID: order.iD);
    await _cMCService.getOrderDetailByOrderID(getOrderDetailByOrderIDRequestModel);
  }

  Future<String> insertPresPresOrder({required CMCInsertPresOrderRequestModel order}) async {
    setState(ViewState.Busy);
    order.createdBy = user.patientID;
    order.orderServiceID = OrderService.Comprehensive_Medical_Checkup.getIdOrderService();
    String requestId = await _cMCService.insertPresPresOrder(order: order);
    if (_cMCService.hasError) {
      error = _cMCService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      getCmcAllPresOrders();
    }
    return requestId;
  }

  Future<String> insertCMCOrderRC({required CMCInsertPresOrderRequestModel order}) async {
    setState(ViewState.Busy);
    order.createdBy = user.patientID;
    order.orderServiceID = OrderService.Comprehensive_Medical_Checkup.getIdOrderService();
    String requestId = await _cMCService.insertCMCOrderRC(order: order);
    if (_cMCService.hasError) {
      error = _cMCService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      getCmcAllPresOrders();
    }
    return requestId;
  }

  Future getCustomerAddresses() async {
    setState(ViewState.Busy);
    await _customerAddressesService.getCustomerAddresses();
    if (_customerAddressesService.hasError) {
      error = _customerAddressesService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getCustomerInfo() async {
    setState(ViewState.Busy);
    await _pharmacyModuleService.generatePharmacyToken();

    if (_customerAddressesService.hasError) {
      error = _customerAddressesService.error!;
      await _pharmacyModuleService.createUser();
      setState(ViewState.ErrorLocal);
    } else {
      await _pharmacyModuleService.createUser();
      await getCustomerAddresses();
    }
  }

  Future addAddressInfo({required AddNewAddressRequestModel addNewAddressRequestModel}) async {
    setState(ViewState.Busy);
    await _customerAddressesService.addAddressInfo(addNewAddressRequestModel: addNewAddressRequestModel);
    if (_customerAddressesService.hasError) {
      error = _customerAddressesService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }
}
