import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/get_cmc_service_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_hhc_all_pres_orders_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/update_pres_oreder_request_model.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/cmc_service.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/customer_addresses_service.dart';
import 'package:diplomaticquarterapp/core/service/parmacyModule/parmacy_module_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.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<GetHHCAllPresOrdersResponseModel> get cmcAllPresOrders =>
      _cMCService.cmcAllPresOrdersList;

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

 PharmacyModuleService _pharmacyModuleService = locator<PharmacyModuleService>();

  bool get isOrderUpdated => _cMCService.isOrderUpdated;

  Future getCMCAllServices() async {
    await _cMCService
        .getCMCAllServices();
    CMCGetItemsRequestModel cMCGetItemsRequestModel = new CMCGetItemsRequestModel(checkupType: cmcAllServicesList[0].iD);

    await getCheckupItems(cMCGetItemsRequestModel: cMCGetItemsRequestModel,isFirst: true);
    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();
    if (_cMCService.hasError) {
      error = _cMCService.error;
      setState(ViewState.Error);
    } else {
      var pendingOrder = _cMCService.cmcAllPresOrdersList.firstWhere(
          (element) => element.status == 1 || element.status == 2,
          orElse: () => null);
      if (pendingOrder != null) {
        await getOrderDetailByOrderID(pendingOrder);
        setState(ViewState.Idle);
      } else {
        getCMCAllServices();
      }
    }
  }

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

  Future getOrderDetailByOrderID(GetHHCAllPresOrdersResponseModel order) async {
    GetOrderDetailByOrderIDRequestModel getOrderDetailByOrderIDRequestModel =
        GetOrderDetailByOrderIDRequestModel(presOrderID: order.iD);
    await _cMCService
        .getOrderDetailByOrderID(getOrderDetailByOrderIDRequestModel);
  }
  //TODO check it in uat
  Future insertPresPresOrder({CMCInsertPresOrderRequestModel order}) async {
    setState(ViewState.Busy);
    order.createdBy = user.patientID;
    order.orderServiceID = OrderService.Comprehensive_Medical_Checkup.getIdOrderService();
    await _cMCService.insertPresPresOrder(order: order);
    if (_cMCService.hasError) {
      error = _cMCService.error;
      setState(ViewState.ErrorLocal);
    } else {
      getCmcAllPresOrders();
    }
  }

  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().then((value) async{
      await _customerAddressesService.getCustomerInfo();
    });
    if (_customerAddressesService.hasError) {
      error = _customerAddressesService.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getCustomerAddresses();
    }
  }

  Future addAddressInfo(
      {AddNewAddressRequestModel addNewAddressRequestModel}) async {


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