package com.fp.frontend.controller.pgeneral.product;

import java.util.HashMap;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.TabChangeEvent;

import com.fp.dto.AbstractDataTransport;
import com.fp.dto.Request;
import com.fp.dto.Response;
import com.fp.dto.save.DtoSave;
import com.fp.frontend.controller.AbstractController;
import com.fp.frontend.helper.MessageHelper;

/**
 * Clase controladora de definicion de productos, subproductos y monedas por
 * subproducto.
 *
 * @author Jorge Vaca.
 * @version 2.1
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class ProductDefinitionController extends AbstractController<AbstractDataTransport> {

    private String tabid = "product";

    @ManagedProperty(value = "#{productController}")
    private ProductController product;

    @ManagedProperty(value = "#{subProductController}")
    private SubProductController subproduct;

    @ManagedProperty(value = "#{subProductCurrencyController}")
    private SubProductCurrencyController subproductcurrency;

    public ProductDefinitionController() throws Exception {
        super(AbstractDataTransport.class);
    }

    @PostConstruct
    private void postconstruct() {
        this.init();
        // Inicializa autoconsulta
        super.startQuery();

    }

    /**
     * Incializa el controlador, cuando se esta utilizando una pagina que utliza
     * el controlador.
     */
    private void init() {
    }

    public void tabChange(TabChangeEvent event) throws Exception {
        this.tabid = event.getTab().getId();
    }

    @Override
    public void query() {
        try {
            if (this.tabid.equals("product")) {
                this.product.query();
            } else if (this.tabid.equals("subproduct")) {
                this.subproduct.query();
            } else {
                this.subproductcurrency.query();
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    @Override
    public void next() {
        try {
            try {
                if (this.tabid.equals("product")) {
                    this.product.next();
                } else if (this.tabid.equals("subproduct")) {
                    this.subproduct.next();
                } else {
                    this.subproductcurrency.next();
                }
            } catch (Throwable e) {
                MessageHelper.setMessageError(e);
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    @Override
    public void previous() {
        try {
            if (this.tabid.equals("product")) {
                this.product.previous();
            } else if (this.tabid.equals("subproduct")) {
                this.subproduct.previous();
            } else {
                this.subproductcurrency.previous();
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    @Override
    public void save() {
        try {
            Request request = this.callerhelper.getRequest();
            HashMap<String, DtoSave> msave = new HashMap<String, DtoSave>();

            DtoSave dtosaveprod = this.product.getDtoSave();
            if (dtosaveprod.pendingProcess()) {
                msave.put(this.product.getBeanalias(), dtosaveprod); // adicionar metadata de mantenimiento para cada tabla.
            }
            DtoSave dtosavesubpro = this.subproduct.getDtoSave();
            if (dtosavesubpro.pendingProcess()) {
                msave.put(this.subproduct.getBeanalias(), dtosavesubpro); // adicionar metadata de mantenimiento para cada tabla.
            }
            DtoSave dtosavesubprocurr = this.subproductcurrency.getDtoSave();
            if (dtosavesubprocurr.pendingProcess()) {
                msave.put(this.subproductcurrency.getBeanalias(), dtosavesubprocurr); // adicionar metadata de mantenimiento para cada tabla.
            }

            request.setSaveTables(msave);
            Response resp = this.callerhelper.executeSave(request);
            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) == 0) {
                this.product.postCommit(resp);
                this.subproduct.postCommit(resp);
                this.subproductcurrency.postCommit(resp);
                MessageHelper.setMessageInfo(resp);
            } else {
                MessageHelper.setMessageError(resp);
            }

        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    @Override
    protected void querydatabase() {

    }

    @Override
    public void postCommit(Response response) throws Exception {

    }

    /**
     * Entrega el valor de: product
     *
     * @return ProductController
     */
    public ProductController getProduct() {
        return this.product;
    }

    /**
     * Fija el valor de: product
     *
     * @param product Valor a fijar en el atributo.
     */
    public void setProduct(ProductController product) {
        this.product = product;
    }

    /**
     * Entrega el valor de: subproduct
     *
     * @return SubProductController
     */
    public SubProductController getSubproduct() {
        return this.subproduct;
    }

    /**
     * Fija el valor de: subproduct
     *
     * @param subproduct Valor a fijar en el atributo.
     */
    public void setSubproduct(SubProductController subproduct) {
        this.subproduct = subproduct;
    }

    /**
     * Entrega el valor de: subproductcurrency
     *
     * @return SubProductCurrencyController
     */
    public SubProductCurrencyController getSubproductcurrency() {
        return this.subproductcurrency;
    }

    /**
     * Fija el valor de: subproductcurrency
     *
     * @param subproductcurrency Valor a fijar en el atributo.
     */
    public void setSubproductcurrency(
            SubProductCurrencyController subproductcurrency) {
        this.subproductcurrency = subproductcurrency;
    }

}
