var web2LeadlUtil = {
    countryWithChilds: {},
    countryOptions: [],
    country_code: {},
    initialize: function () {
        var self = this;
        self.getOptions();
        self.initCountry();
        self.initState();
        self.initExistingCustomer();
    },
    getOptions: function () {
        var self = this;
        $.ajax({
            method: "GET",
            url: "https://gs-sfdc-countries.herokuapp.com/services/countries",
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Accept', 'application/json');
            },
            success: function (data) {
                try {
                    let response
                    if (typeof data == 'object') {
                        response = data;
                    } else {
                        response = JSON.parse(data);
                    }
                    self.countryOptions = response.controlDependDetails;
                    self.initializeCountry();
                    let country_code = $('#country_code');
                    country_code.removeAttr("disabled");
                } catch (e) {
                    console.log(e);
                    alert('error in getting Country and State');
                }
            }
        });
    },
    initCountry: function () {
        var country_code = $('#country_code');
        country_code.append(new Option("--None--", ""));
        country_code.attr("disabled", "true");
    },
    initState: function () {
        var state_code = $('#state_code');
        state_code.append(new Option("--None--", ""));
        state_code.attr("disabled", "true");
        state_code.attr('required', false);
    },
    initExistingCustomer: function () {
        var existing_customer = $('#existing_customer');
        existing_customer.change(function () {
            let serialNumber = $('#serialNumber');
            let input = $('#serialNumber input');
            let required = $(this).prop('checked');
            if (required) {
                serialNumber.css('display', 'block');
                input.attr('required', true);
                input.removeAttr('disabled');
            } else {
                serialNumber.css('display', 'none');
                input.attr('disabled', true);
                input.removeAttr('required');
            }
        })
    },
    initializeCountry: function () {
        var self = this;
        if (typeof $ == "function") {
            let country_code = $('#country_code');
            self.countryOptions.forEach(function (countryOption) {
                country_code.append(new Option(countryOption.master.label, countryOption.master.value));
            })
            country_code.change(function () {
                self.maintainState($(this).val());
            })
        } else {
            throw "jQuery resource not found!";
        }
    },
    maintainState: function (country) {
        var self = this;
        let state_code = $('#state_code');
        state_code.empty();
        state_code.append(new Option("--None--", ""));
        state_code.attr("disabled", "true");
        state_code.attr('required', false);
        if (!self.countryWithChilds[country]) {
            self.countryWithChilds[country] = [];
            self.countryOptions.find(function (countryOption) {
                if (countryOption.master.value == country) {
                    self.countryWithChilds[country] = countryOption.children;
                }
            })
        }
        let removeAttr = false;
        self.countryWithChilds[country].forEach(function (countryOption) {
            if (countryOption.active) {
                removeAttr = true;
                state_code.append(new Option(countryOption.label, countryOption.value));
            }
        })
        if (removeAttr) {
            state_code.removeAttr("disabled");
            state_code.attr('required', true);
        }
    }
}
