import { Model } from 'front';
import states from '../../data/states';

const statesOptions = [],
    statesOptionsAbbrev = [];

for (const code in states) {
    statesOptions.push({
        value: code,
        title: states[code]
    });

    statesOptionsAbbrev.push({
        value: code,
        title: code
    });
}

/**\

    title: Address
    category: model
    description:
        A model for addresses

\**/
class Address extends Model {
    static statesOptions = statesOptions;
    static statesOptionsAbbrev = statesOptionsAbbrev;

    constructor(data) {
        super({
            street: '',
            apt: '',
            city: '',
            state: '',
            country: 'US',
            zip: '',

            notes: '',

            lat: 0,
            lng: 0
        });

        this.fill(data);
    }

    fill(data) {
        if (data) {
            if (data.hasOwnProperty('street_address_1')) {
                data.street = data.street_address_1;
                delete data.street_address_1;
            }

            if (data.hasOwnProperty('street_address_2')) {
                data.apt = data.street_address_2;
                delete data.street_address_2;
            }

            if (data.hasOwnProperty('zip_code')) {
                data.zip = data.zip_code;
                delete data.zip_code;
            }
        }

        super.fill(data);

        return this;
    }

    out() {
        const out = super.out();

        /* eslint-disable camelcase */
        out.street_address_1 = out.street;
        delete out.street;

        out.street_address_2 = out.apt;
        delete out.apt;

        out.zip_code = out.zip;
        delete out.zip;
        /* eslint-enable camelcase */

        return out;
    }

    singleLine() {
        let out = this.street;

        if (!this.street || !this.city || !this.state || !this.zip) {
            return '';
        }

        if (this.apt) {
            out += ', ' + this.apt;
        }

        return out + ', ' + this.city.trim() +
            ', ' + this.state.trim() +
            ' ' + this.zip.trim();
    }

    static fromGooglePlace(place) {
        let streetNumber, street, city, state, zip,
            i, component, addressType;

        if (!place || !place.address_components) {
            return;
        }

        // Extract address components.
        for (i = 0; i < place.address_components.length; i++) {
            component = place.address_components[i];
            addressType = component.types[0];

            switch (addressType) {
                case 'street_number':
                    streetNumber = component.long_name || component.short_name;
                    break;
                case 'route':
                    street = component.short_name || component.long_name;
                    break;
                case 'locality':
                    city = component.long_name || component.short_name;
                    break;
                case 'administrative_area_level_1':
                    state = component.short_name;
                    break;
                case 'postal_code':
                    zip = component.long_name || component.short_name;
                    break;
                default:
                    break;
            }
        }

        return new Address({
            street: ((streetNumber || '') + ' ' + street).trim(),
            city: city,
            state: state,
            zip: zip,

            lat: place.geometry.location.lat(),
            lng: place.geometry.location.lng()
        });
    }
}

export default Address;
