import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/Country.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/customer_addresses_service.dart';
import 'package:diplomaticquarterapp/services/pharmacy_services/pharmacyAddress_service.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:geocoding/geocoding.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<AddressInfo> 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 addEditAddress(PickResult value, AddressInfo editedAddress) async {
  Future addEditAddress(Placemark selectedPlace, AddressInfo editedAddress, {String latLong}) async {
    setState(ViewState.Busy);

    AddressInfo sendingAddress;

    if (editedAddress == null) {
      sendingAddress = AddressInfo();
      sendingAddress.id = "0";
      sendingAddress.firstName = user.firstName;
      sendingAddress.lastName = user.lastName;
      sendingAddress.email = user.emailAddress;
      sendingAddress.company = null;
    } else {
      sendingAddress = editedAddress;
    }

    sendingAddress.country = selectedPlace.country;
    sendingAddress.city = selectedPlace.locality;
    sendingAddress.zipPostalCode = selectedPlace.postalCode;
    sendingAddress.province = selectedPlace.locality;
    sendingAddress.latLong = latLong;

    // 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 = selectedPlace.name;
    sendingAddress.address2 = selectedPlace.street;
    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).then((value) async {});
    }

    if (_pharmacyAddressService.hasError) {
      setState(ViewState.Idle);
      await Future.delayed(Duration(milliseconds: 800));
      getAddressesList();
    } else {
      setState(ViewState.Idle);
      await Future.delayed(Duration(milliseconds: 800));
      getAddressesList();
    }
  }

  Future deleteAddresses(AddressInfo address) async {
    setState(ViewState.Busy);
    await _pharmacyAddressService.deleteCustomerAddress(address);
    if (_pharmacyAddressService.hasError) {
      error = _pharmacyAddressService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
      await Future.delayed(Duration(milliseconds: 800));
      getAddressesList();
    }
  }

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