package cz.senslog.connector.fetch.fofr;

import cz.senslog.connector.model.config.DefaultConfig;

import java.util.*;

public class FofrConfig {

    private final TimeZone timeZone;
    private final String orderStatesUrl;
    private final String warehouseLocationsUrl;
    private final String parcelTrackingUrl;
    private final String parcelInfoUrl;
    private final String activeOrdersUrl;

    private final int deliveryStatus;
    private final Set<WarehouseInfo> missingWarehouses;

    public FofrConfig(DefaultConfig defaultConfig) {
        this.timeZone = TimeZone.getTimeZone(defaultConfig.getStringProperty("timeZone"));
        this.orderStatesUrl = defaultConfig.getStringProperty("orderStatesUrl");
        this.warehouseLocationsUrl = defaultConfig.getStringProperty("warehouseLocationsUrl");
        this.parcelTrackingUrl = defaultConfig.getStringProperty("parcelTrackingUrl");
        this.parcelInfoUrl = defaultConfig.getStringProperty("parcelInfoUrl");
        this.activeOrdersUrl = defaultConfig.getStringProperty("activeOrdersUrl");
        this.deliveryStatus = defaultConfig.getIntegerProperty("deliveryStatus");
        if (defaultConfig.containsProperty("missingWarehouses")) {
            Object wObject = defaultConfig.getProperty("missingWarehouses");
            if (wObject instanceof List) {
                List<?> wList = (List<?>) wObject;
                this.missingWarehouses = new HashSet<>(wList.size());
                for (Object w : wList) {
                    if (w instanceof Map) {
                        Map<?, ?> wMap = (Map<?, ?>) w;
                        List<?> coord = (List<?>) wMap.get("location");
                        if (coord.size() == 2) {
                            String code = (String) wMap.get("code");
                            Double lat = (Double) coord.get(0);
                            Double lon = (Double) coord.get(1);
                            missingWarehouses.add(new WarehouseInfo(code, lat, lon));
                        }
                    }
                }
            } else {
                this.missingWarehouses = Collections.emptySet();
            }
        } else {
            this.missingWarehouses = Collections.emptySet();
        }
    }

    public TimeZone getTimeZone() {
        return timeZone;
    }

    public int getDeliveryStatus() {
        return deliveryStatus;
    }

    public String getActiveOrdersUrl() {
        return activeOrdersUrl;
    }

    public String getOrderStatesUrl() {
        return orderStatesUrl;
    }

    public String getWarehouseLocationsUrl() {
        return warehouseLocationsUrl;
    }

    public String getParcelTrackingUrl() {
        return parcelTrackingUrl;
    }

    public String getParcelInfoUrl() {
        return parcelInfoUrl;
    }

    public Set<WarehouseInfo> getMissingWarehouses() {
        return missingWarehouses;
    }
}
