/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.taxiservicios.beans.system;

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.system.SystemPropertyController;
import com.arrebol.taxiservicios.model.core.SystemProperty;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Named
@ViewScoped
public class SystemPropertyBean extends TaxiGenericBean implements Serializable {

    public void updateFee() {
        logger.info("updateFee");
        try {
            getSystemProperties().forEach((property) -> {
                switch (property.getPropertyName()) {
                    case FEE:
                        property.setPropertyValue(getFeeValue().toString());
                        break;
                    case LIMIT_KM:
                        property.setPropertyValue(getLimitKmServices()+"");
                        break;
                    case COMISION:
                        property.setPropertyValue(getComision()+"");
                        break;

                }
            });

            if (getController().updateSystemProperties(getSystemProperties())) {
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("system.property.title"),
                        getBundlePropertyFile().getString("system.property.update.success"));
            } else {
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("system.property.title"),
                        getBundlePropertyFile().getString("system.property.update.error"));
            }
        } catch (Exception e) {
            logger.error("updateFee", e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("system.property.title"),
                    getBundlePropertyFile().getString("system.property.update.exception"));
        }
    }

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setController(new SystemPropertyController());
            setSystemProperties(getController().getAllSystemProperties());

            getSystemProperties().forEach((property) -> {
                switch (property.getPropertyName()) {
                    case FEE:
                        try {
                        setFeeValue(new Float(property.getPropertyValue()));
                    } catch (NumberFormatException e) {
                        setFeeValue(0f);
                    }
                    break;
                    case LIMIT_KM:
                        try {
                        setLimitKmServices(new Integer(property.getPropertyValue()));
                    } catch (NumberFormatException e) {
                        setLimitKmServices(0);
                    }
                    break;
                    case COMISION:
                        try {
                        setComision(new Integer(property.getPropertyValue()));
                    } catch (NumberFormatException e) {
                        setComision(0);
                    }

                }
            });
        } catch (Exception e) {
            logger.error("SystemPropertyBean", e);
        }
    }

    private static final long serialVersionUID = -1L;

    final Logger logger = LogManager.getLogger(getClass());

    private SystemPropertyController controller;
    private List<SystemProperty> systemProperties;
    private Float feeValue;
    private int limitKmServices;
    private int comision;

    public int getLimitKmServices() {
        return limitKmServices;
    }

    public void setLimitKmServices(int limitKmServices) {
        this.limitKmServices = limitKmServices;
    }

    public SystemPropertyController getController() {
        return controller;
    }

    public void setController(SystemPropertyController controller) {
        this.controller = controller;
    }

    public List<SystemProperty> getSystemProperties() {
        return systemProperties;
    }

    public void setSystemProperties(List<SystemProperty> systemProperties) {
        this.systemProperties = systemProperties;
    }

    public Float getFeeValue() {
        return feeValue;
    }

    public void setFeeValue(Float feeValue) {
        this.feeValue = feeValue;
    }

    public int getComision() {
        return comision;
    }

    public void setComision(int comision) {
        this.comision = comision;
    }

}
