import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/add_new_address_Request_Model.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/pharmacy_module_view_model.dart';
import 'package:intl/intl.dart';

import '../../../locator.dart';
import '../base_service.dart';

class CustomerAddressesService extends BaseService {

  List<AddressInfo> addressesList = List();
  CustomerInfo customerInfo;


  Future addAddressInfo({ AddNewAddressRequestModel
  addNewAddressRequestModel }) async {

    addNewAddressRequestModel.customer.email = customerInfo.email;
    addNewAddressRequestModel.customer.id = customerInfo.customerId;
    addNewAddressRequestModel.customer.roleIds = [3];
    addNewAddressRequestModel.customer.addresses[0].email = customerInfo.email;
    addNewAddressRequestModel.customer.addresses[0].phoneNumber = customerInfo.mobileNumber;
    addNewAddressRequestModel.customer.addresses[0].firstName = user.firstName;
    addNewAddressRequestModel.customer.addresses[0].lastName = user.lastName;
    addNewAddressRequestModel.customer.addresses[0].countryId = 69;

    var f = DateFormat('E, d MMM yyyy HH:mm:ss');
    var date = f.format(DateTime.now().toUtc()) + " GMT";
    addNewAddressRequestModel.customer.addresses[0].createdOnUtc = date;
    hasError = false;
    await baseAppClient.post(ADD_CUSTOMER_ADDRESS,
        onSuccess: (dynamic response, int statusCode) {
          addressesList.clear();
          response["customers"][0]["addresses"].forEach((data) {
            addressesList
                .add(AddressInfo.fromJson(data));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: addNewAddressRequestModel.toJson(), isAllowAny: true);
  }

  Future getCustomerAddresses() async {
    Map<String, String> queryParams = {
      'fields':'addresses'
    };
    hasError = false;
    await baseAppClient.getPharmacy("$BASE_PHARMACY_URL$GET_CUSTOMER_ADDRESSES${customerInfo.customerId}",
        onSuccess: (dynamic response, int statusCode) {
          addressesList.clear();
          response["customers"][0]["addresses"].forEach((data) {
            addressesList
                .add(AddressInfo.fromJson(data));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, queryParams: queryParams, isExternal: true);
  }


  Future getCustomerInfo() async {
    Map<String, String> queryParams = {
      'FileNumber':'${user.patientID}'
    };

    hasError = false;
    await baseAppClient.getPharmacy(BASE_PHARMACY_URL+GET_CUSTOMER_INFO,
        onSuccess: (dynamic response, int statusCode) {
          customerInfo= CustomerInfo.fromJson(response);
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, queryParams: queryParams, isExternal: true);
  }
}


class CustomerInfo {
  bool isRegistered;
  String userName;
  dynamic password;
  String email;
  dynamic errorMessage;
  String mobileNumber;
  int customerId;

  CustomerInfo(
      {this.isRegistered,
        this.userName,
        this.password,
        this.email,
        this.errorMessage,
        this.mobileNumber,
        this.customerId});

  CustomerInfo.fromJson(Map<String, dynamic> json) {
    isRegistered = json['IsRegistered'];
    userName = json['UserName'];
    password = json['Password'];
    email = json['Email'];
    errorMessage = json['ErrorMessage'];
    mobileNumber = json['MobileNumber'];
    customerId = json['CustomerId'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['IsRegistered'] = this.isRegistered;
    data['UserName'] = this.userName;
    data['Password'] = this.password;
    data['Email'] = this.email;
    data['ErrorMessage'] = this.errorMessage;
    data['MobileNumber'] = this.mobileNumber;
    data['CustomerId'] = this.customerId;
    return data;
  }
}


class AddressInfo {
  String id;
  String firstName;
  String lastName;
  String email;
  dynamic company;
  int countryId;
  String country;
  dynamic stateProvinceId;
  String city;
  String address1;
  String address2;
  String zipPostalCode;
  String phoneNumber;
  dynamic faxNumber;
  String customerAttributes;
  String createdOnUtc;
  dynamic province;
  String latLong;

  AddressInfo(
      {this.id,
        this.firstName,
        this.lastName,
        this.email,
        this.company,
        this.countryId,
        this.country,
        this.stateProvinceId,
        this.city,
        this.address1,
        this.address2,
        this.zipPostalCode,
        this.phoneNumber,
        this.faxNumber,
        this.customerAttributes,
        this.createdOnUtc,
        this.province,
        this.latLong});

  AddressInfo.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    firstName = json['first_name'];
    lastName = json['last_name'];
    email = json['email'];
    company = json['company'];
    countryId = json['country_id'];
    country = json['country'];
    stateProvinceId = json['state_province_id'];
    city = json['city'];
    address1 = json['address1'];
    address2 = json['address2'];
    zipPostalCode = json['zip_postal_code'];
    phoneNumber = json['phone_number'];
    faxNumber = json['fax_number'];
    customerAttributes = json['customer_attributes'];
    createdOnUtc = json['created_on_utc'];
    province = json['province'];
    latLong = json['lat_long'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['first_name'] = this.firstName;
    data['last_name'] = this.lastName;
    data['email'] = this.email;
    data['company'] = this.company;
    data['country_id'] = this.countryId;
    data['country'] = this.country;
    data['state_province_id'] = this.stateProvinceId;
    data['city'] = this.city;
    data['address1'] = this.address1;
    data['address2'] = this.address2;
    data['zip_postal_code'] = this.zipPostalCode;
    data['phone_number'] = this.phoneNumber;
    data['fax_number'] = this.faxNumber;
    data['customer_attributes'] = this.customerAttributes;
    data['created_on_utc'] = this.createdOnUtc;
    data['province'] = this.province;
    data['lat_long'] = this.latLong;
    return data;
  }
}
