import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/Addresses.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/Country.dart';
import 'package:diplomaticquarterapp/services/pharmacy_services/pharmacyAddress_service.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyAddressesModel.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:google_maps_place_picker/google_maps_place_picker.dart';
import '../../../locator.dart';
import '../base_view_model.dart';

class PharmacyAddressesViewModel extends BaseViewModel {
  PharmacyAddressService _pharmacyAddressService =
      locator<PharmacyAddressService>();

  List<Addresses> get addresses => _pharmacyAddressService.addresses;

  int get selectedAddressIndex => _pharmacyAddressService.selectedAddressIndex;

  CountryData get country => _pharmacyAddressService.country;

  setSelectedAddressIndex(int index) {
    _pharmacyAddressService.selectedAddressIndex = index;
  }

  Future getAddressesList() async {
    setState(ViewState.Busy);
    await _pharmacyAddressService.getAddresses();
    if (_pharmacyAddressService.hasError) {
      error = _pharmacyAddressService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

/*  Future getCountries(String countryName) async {
    setState(ViewState.Busy);
    await _pharmacyAddressService.getCountries(countryName);
    if (_pharmacyAddressService.hasError) {
      error = _pharmacyAddressService.error;
      // setState(ViewState.Error);
    } else {
      // setState(ViewState.Idle);
    }
  }*/

  Future addEditAddress(PickResult value, Addresses editedAddress) async {
    setState(ViewState.Busy);

    Addresses sendingAddress;

    if (editedAddress == null) {
      sendingAddress = Addresses();
      sendingAddress.id = "0";
      sendingAddress.firstName = user.firstName;
      sendingAddress.lastName = user.lastName;
      sendingAddress.email = user.emailAddress;
      sendingAddress.company = null;
    } else {
      sendingAddress = editedAddress;
    }
    value.addressComponents.forEach((element) {
      if (element.types.contains("country")) {
        sendingAddress.country = element.longName;
      }
      if (element.types.contains("administrative_area_level_1")) {
        sendingAddress.city = element.longName;
      }
      if (element.types.contains("postal_code")) {
        sendingAddress.zipPostalCode = element.longName;
      }
      if (element.types.contains("administrative_area_level_2")) {
        sendingAddress.province = element.longName;
      }
    });
    sendingAddress.latLong = value.geometry.location.toString();

    await _pharmacyAddressService.getCountries(sendingAddress.country);
    sendingAddress.countryId = country.id;
    sendingAddress.stateProvinceId = null;
    sendingAddress.address1 = value.formattedAddress;
    sendingAddress.address2 = "";
    sendingAddress.phoneNumber = user.mobileNumber;
    sendingAddress.faxNumber = user.faxNumber;
    sendingAddress.customerAttributes = "";
    sendingAddress.createdOnUtc = DateTime.now().toString();

    if (editedAddress == null) {
      await _pharmacyAddressService.addCustomerAddress(sendingAddress);
    } else {
      await _pharmacyAddressService.editCustomerAddress(sendingAddress);
    }

    if (_pharmacyAddressService.hasError) {
      error = _pharmacyAddressService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future deleteAddresses(Addresses sendingAddress) async {
    setState(ViewState.Busy);
    await _pharmacyAddressService.deleteCustomerAddress(sendingAddress);
    if (_pharmacyAddressService.hasError) {
      error = _pharmacyAddressService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future saveSelectedAddressLocally(Addresses selectedAddress) async {
    await sharedPref.setObject(PHARMACY_SELECTED_ADDRESS, selectedAddress);
  }
}
