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

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

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

/**
 * Clase controladora que se encarga de la genracion de calendario de fechas en
 * la tabla TgeneAccountingDate.
 *
 * @author Jorge Vaca.
 * @version 2.1
 */
@ManagedBean
@ViewScoped
public class GenerateAccountingDateController extends AbstractController<AbstractDataTransport> {

    private static final long serialVersionUID = 1L;

    @ManagedProperty(value = "#{tgeneAccountingDateBranchController}")
    private TgeneAccountingDateBranchController accountingdate;

    /**
     * Lista de dias de domingo a sabado a indicar si es contable o no.
     */
    List<Map<String, Object>> ldays;
    /**
     * Fecha desde la cual se genera el calendaio contable.
     */
    private Date initdate;
    /**
     * Fecha hasta la cual se genera el calendario contable.
     */
    private Date enddate;

    public GenerateAccountingDateController() 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 {
            this.recperpage = 10; // Cambiar al # reg a mirar.
            this.lrecord = new ArrayList<>();
            this.beanalias = "LISTACCOUNTINDATE";
            ldays = new ArrayList<>();
            this.consultadias();
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }

    /**
     * Entrega el valor de: accountingdate
     *
     * @return TgeneAccountingDateBranchController
     */
    public TgeneAccountingDateBranchController getAccountingdate() {
        return accountingdate;
    }

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

    /**
     * Entrega el valor de: ldays
     *
     * @return List<Map<String,Object>>
     */
    public List<Map<String, Object>> getLdays() {
        return ldays;
    }

    /**
     * Fija el valor de: ldays
     *
     * @param ldays Valor a fijar en el atributo.
     */
    public void setLdays(List<Map<String, Object>> ldays) {
        this.ldays = ldays;
    }

    /**
     * Entrega el valor de: initdate
     *
     * @return Date
     */
    public Date getInitdate() {
        return initdate;
    }

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

    /**
     * Entrega el valor de: enddate
     *
     * @return Date
     */
    public Date getEnddate() {
        return enddate;
    }

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

    @Override
    protected void querydatabase() {
    }

    @SuppressWarnings("unchecked")
    private void consultadias() throws Exception {
        try {
            Request request = this.callerhelper.getRequest();
            request.modifiedData().put("querytype", "P");
            request.modifiedData().put("queryalias", "LISTACCOUNTINDATE");
            Response resp = this.callerhelper.executeQuery(request);

            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) {
                this.ldays.clear();
                MessageHelper.setMessageError(resp);
            } else {
                this.ldays = (List<Map<String, Object>>) resp.get(this.beanalias);
                super.addField("branchcodemin", resp.get("branchcodemin"));
                super.addField("branchcodemax", resp.get("branchcodemax"));
            }

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

    }

    @Override
    public void save() {
        try {
            Request request = this.callerhelper.getRequest();
            //DtoSave dtosave = super.getDtoSave();
            //HashMap<String, DtoSave> msave = new HashMap<String, DtoSave>();
            //msave.put(this.beanalias, dtosave); // adicionar metadata de mantenimiento para cada tabla.
            //request.setSaveTables(msave);
            request.modifiedData().put("branchcodemin", super.getMfilelds().get("branchcodemin"));
            request.modifiedData().put("branchcodemax", super.getMfilelds().get("branchcodemax"));
            request.modifiedData().put("initdate", this.initdate);
            request.modifiedData().put("enddate", this.enddate);
            request.modifiedData().put("LISTACCOUNTINDATE", ldays);
            Response resp = this.callerhelper.executeSave(request);
            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) == 0) {
                this.postCommit(resp);
                MessageHelper.setMessageInfo(resp);
            } else {
                MessageHelper.setMessageError(resp);
            }

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

    @Override
    public void postCommit(Response response) throws Exception {
        super.postCommitGeneric(response, this.beanalias);
    }

}
