import 'dart:async';

import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/GetCMCAllOrdersResponseModel.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/HHC_get_all_services_request_modle.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/HHC_get_all_services_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/PatientERHHCInsertServicesList.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/patient_er_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/update_pres_oreder_request_model.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/customer_addresses_service.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/home_health_care_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 HomeHealthCareViewModel extends BaseViewModel {
  HomeHealthCareService _homeHealthCareService = locator<HomeHealthCareService>();

  CustomerAddressesService _customerAddressesService = locator<CustomerAddressesService>();

  List<HHCGetAllServicesResponseModel> get hhcAllServicesList => _homeHealthCareService.hhcAllServicesList;

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

  List<GetCMCAllOrdersResponseModel> get hhcAllPresOrders => _homeHealthCareService.hhcAllPresOrdersList;

  List<GetOrderDetailByOrderIDResponseModel> get hhcAllOrderDetail => _homeHealthCareService.hhcAllOrderDetail;

  PharmacyModuleService _pharmacyModuleService = locator<PharmacyModuleService>();

  bool get isOrderUpdated => _homeHealthCareService.isOrderUpdated;

  int get orderId => _homeHealthCareService.requestNo;
  GetCMCAllOrdersResponseModel pendingOrder;

  List<PatientERHHCInsertServicesList> patientERHHCInsertServicesList = List();

  dynamic get hhcResponse => _homeHealthCareService.hhcResponse;

  Future getHHCAllServices() async {
    HHCGetAllServicesRequestModel hHCGetAllServicesRequestModel = new HHCGetAllServicesRequestModel();
    // await _homeHealthCareService.getHHCAllServices(hHCGetAllServicesRequestModel);
    await _homeHealthCareService.getHHCAllServicesRC(hHCGetAllServicesRequestModel);
    if (_homeHealthCareService.hasError) {
      error = _homeHealthCareService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getHHCAllPresOrders() async {
    pendingOrder = null;
    setState(ViewState.Busy);
    await _homeHealthCareService.getHHCAllPresOrdersRC();
    if (_homeHealthCareService.hasError) {
      error = _homeHealthCareService.error;
      setState(ViewState.Error);
    } else {
      pendingOrder = _homeHealthCareService.hhcAllPresOrdersList.firstWhere((element) => element.statusId == 1 || element.statusId == 2, orElse: () => null);
      if (pendingOrder != null) {
        // await _homeHealthCareService.getOrderDetailByOrderID(pendingOrder.iD);
        setState(ViewState.Idle);
      } else {
        getHHCAllServices();
      }
    }
  }

  Future updateHHCPresOrder(UpdatePresOrderRequestModel updatePresOrderRequestModel) async {
    setState(ViewState.Busy);
    // await _homeHealthCareService.updateHHCPresOrder(updatePresOrderRequestModel);
    await _homeHealthCareService.updateHHCPresOrderRC(updatePresOrderRequestModel);
    if (_homeHealthCareService.hasError) {
      error = _homeHealthCareService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future insertPresPresOrder({PatientERInsertPresOrderRequestModel order}) async {
    setState(ViewState.Busy);
    order.createdBy = user.patientID;
    order.orderServiceID = OrderService.HOME_HEALTH_CARE.getIdOrderService();
    // await _homeHealthCareService.insertPresPresOrder(order: order);
    await _homeHealthCareService.insertHHCOrderRC(order: order);
    if (_homeHealthCareService.hasError) {
      error = _homeHealthCareService.error;
      setState(ViewState.ErrorLocal);
    } else {
      getHHCAllPresOrders();
    }
  }

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

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

    if (_customerAddressesService.hasError) {
      error = _customerAddressesService.error;
      await _pharmacyModuleService.createUser();
      setState(ViewState.ErrorLocal);
    } else {
      await _pharmacyModuleService.createUser().then((value) async {
        if (!_pharmacyModuleService.hasError) await getCustomerAddresses();
        setState(ViewState.Idle);
      }).catchError((err) {});
    }
    return Future.value(1);
  }

  Future addAddressInfo({AddNewAddressRequestModel addNewAddressRequestModel}) async {
    setState(ViewState.Busy);

    // await _pharmacyModuleService.generatePharmacyToken().then((value) async {
    await _customerAddressesService.addAddressInfo(addNewAddressRequestModel: addNewAddressRequestModel);
    // });

    if (_customerAddressesService.hasError) {
      error = _customerAddressesService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }
}
