/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.component.selectOneStateProvince;

import com.icesoft.faces.facelets.component.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;

@ApplicationScoped
@ManagedBean
public class LocationLists implements Serializable {

    private static Log log = LogFactory.getLog(LocationLists.class);

    private final static int NUMBER_USSTATES = 59;
    private final static int NUMBER_CANADIANPROVINCES = 13;

    private static SelectItem[] usStateList;
    private static SelectItem[] canadianProvinceList;

    static {
        try {
            usStateList = new SelectItem[NUMBER_USSTATES];
            canadianProvinceList = new SelectItem[NUMBER_CANADIANPROVINCES];

            //read the database from the XML file and map it
            loadXDB("usStates.xml.gz", usStateList, NUMBER_USSTATES);
            loadXDB("canadianProvinces.xml.gz", canadianProvinceList, NUMBER_CANADIANPROVINCES);
        } catch (Exception e) {
            log.error("error loading province and state lists: " + e);
        }
    }

    /**
     * Decodes the stored XML wrapper object representation of the various
     * location databases and maps the entries as XMLData objects.
     *
     * @param gzipFile
     * @param list
     * @param numberOfEntries
     * @see XMLData
     */
    private static void loadXDB(String gzipFile, SelectItem[] list, int numberOfEntries) {
        //each of these contains a location name and Alpha 2 code value
        XMLData xData;

        //for decoding the wrapper objects
        XMLDecoder xDecode = null;

        try {
            //load and unzip the xml file
            GZIPInputStream in = new GZIPInputStream(LocationLists.class.getResourceAsStream(gzipFile));

            //xml decoding mechanism
            xDecode = new XMLDecoder(new BufferedInputStream(in));

        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.debug("Location List Database not found.");
        }
        //loop through every entry in the xml file
        for (int i = 0; i < numberOfEntries; i++) {
            xData = (XMLData) xDecode.readObject();
            list[i] = new SelectItem(StringUtil.fixCapitalization(xData.getName().toLowerCase()));
        }
        //close the XML decoder
        xDecode.close();
    }

    public SelectItem[] getUsStateList() {
        return usStateList;
    }

    public void setUsStateList(SelectItem[] usStateList) {
        LocationLists.usStateList = usStateList;
    }

    public SelectItem[] getCanadianProvinceList() {
        return canadianProvinceList;
    }

    public void setCanadianProvinceList(SelectItem[] canadianProvinceList) {
        LocationLists.canadianProvinceList = canadianProvinceList;
    }

}