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

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 del bean AbstractDataTransport.
 *
 * @author Jorge Vaca.
 * @version 2.1
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class QueryController extends AbstractController<AbstractDataTransport> {

    @ManagedProperty(value = "#{queryProcessController}")
    private QueryProcessController queryprocess;

    @ManagedProperty(value = "#{transactionQueryController}")
    private TransactionQueryController transactionquery;

    private String tabid = "ttransaction";

    public QueryController() 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() {
        try {
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }

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

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

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

    @Override
    public void previous() {
        try {
            if (this.tabid.equals("queryprocess")) {
                this.queryprocess.previous();
            } else {
                this.transactionquery.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 dtosavetq = this.transactionquery.getDtoSave();
            if (dtosavetq.pendingProcess()) {
                dtosavetq.setReturnpk(true);
                msave.put(this.transactionquery.getBeanalias(), dtosavetq); // adicionar metadata de mantenimiento para cada tabla.
            }
            DtoSave dtosaveqp = this.queryprocess.getDtoSave();
            if (dtosaveqp.pendingProcess()) {
                dtosaveqp.setReturnpk(true);
                msave.put(this.queryprocess.getBeanalias(), dtosaveqp); // 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.transactionquery.postCommit(resp);
                this.queryprocess.postCommit(resp);
                MessageHelper.setMessageInfo(resp);
            } else {
                MessageHelper.setMessageError(resp);
            }

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

    @Override
    protected void querydatabase() {
        // TODO Auto-generated method stub
    }

    /**
     * Entrega el valor de: queryprocess
     *
     * @return QueryProcessController
     */
    public QueryProcessController getQueryprocess() {
        return this.queryprocess;
    }

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

    /**
     * Entrega el valor de: transactionquery
     *
     * @return TransactionQueryController
     */
    public TransactionQueryController getTransactionquery() {
        return this.transactionquery;
    }

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

}
