import 'dart:async';

import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/Addresses.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/Country.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart';

class PharmacyAddressService extends BaseService {
  List<Addresses> addresses = List();
  CountryData country;
  int selectedAddressIndex = 0;

  Future<List<Addresses>> getAddresses() async {
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    Map<String, String> queryParams = {'fields': 'addresses'};
    hasError = false;
    Addresses selectedAddress;
    try {
      var completer = Completer();

      await baseAppClient.getPharmacy("$GET_CUSTOMERS_ADDRESSES$customerId",
          onSuccess: (dynamic response, int statusCode) async {
        addresses.clear();
        var savedAddress =
            await sharedPref.getObject(PHARMACY_SELECTED_ADDRESS);
        if (savedAddress != null) {
          selectedAddress = Addresses.fromJson(savedAddress);
        }
        int index = 0;
        response['customers'][0]['addresses'].forEach((item) {
          Addresses address = Addresses.fromJson(item);
          if (selectedAddress != null && selectedAddress.id == item["id"]) {
            selectedAddressIndex = index;
          }
          addresses.add(address);
          index++;
        });
        completer.complete();
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, queryParams: queryParams);

      await completer.future;

    } catch (error){
      throw error;
    }

    return addresses;
  }

  Future getCountries(String countryName) async {
    hasError = false;
    try {
      await baseAppClient.getPharmacy("$PHARMACY_GET_COUNTRY",
          onSuccess: (dynamic response, int statusCode) {
        // countries.clear();
        response['countries'].forEach((item) {
          if (CountryData.fromJson(item).name == countryName ||
              CountryData.fromJson(item).namen == countryName) {
            country = CountryData.fromJson(item);
          }
          // countries.add(CountryData.fromJson(item));
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      });
    } catch (error) {
      throw error;
    }
  }

  Future addCustomerAddress(Addresses address) async {
    makeCustomerAddress(address, ADD_CUSTOMER_ADDRESS);
  }

  Future editCustomerAddress(Addresses address) async {
    makeCustomerAddress(address, EDIT_CUSTOMER_ADDRESS);
  }

  Future deleteCustomerAddress(Addresses address) async {
    makeCustomerAddress(address, DELETE_CUSTOMER_ADDRESS);
  }

  Future makeCustomerAddress(Addresses address, String url) async {
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    hasError = false;
    super.error = "";

    Map<String, dynamic> customerObject = Map();
    customerObject["addresses"] = [address];
    customerObject["id"] = customerId;
    customerObject["email"] = address.email;
    customerObject["role_ids"] = [3];
    Map<String, dynamic> body = Map();
    body["customer"] = customerObject;

    await baseAppClient.postPharmacy("$url", onSuccess: (response, statusCode) async {
      addresses.clear();
      response['customers'][0]['addresses'].forEach((item) {
        addresses.add(Addresses.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }
}
