package com.fp.frontend.controller.pbpm.lov;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.primefaces.context.RequestContext;

import com.fp.dto.Request;
import com.fp.dto.Response;
import com.fp.dto.query.DtoQuery;
import com.fp.frontend.controller.AbstractController;
import com.fp.frontend.enums.EnumLovOption;
import com.fp.frontend.helper.CallerHelper;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.pbpm.gene.TbpmRules;

/**
 * Clase controladora del lov asociado al bean TbpmRules.
 *
 * @author Andres E. Carpio.
 * @version 2.1
 */
@ManagedBean
@ViewScoped
public class TbpmRulesLovController extends AbstractController<TbpmRules> {

    private static final long serialVersionUID = 1L;

    public TbpmRulesLovController() throws Exception {
        super(TbpmRules.class);
    }

    @PostConstruct
    private void postconstruct() throws Exception {
        this.init();
        // Inicializa autoconsulta
        super.startQuery();
        //FileType
        String filetype = CallerHelper.getLovParameter("filetype");
        if (filetype == null) {
            MessageHelper.setMessageError("msg_fileTypeNotFound");
            return;
        }
        super.addFilter("filetype", filetype);

    }

    /**
     * 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 = "TBPMRULESLOV";
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void querydatabase() {
        try {
            Request request = this.callerhelper.getRequest();
            super.addFilter("pk.companycode", this.getLoginController().getRequest().getCompany().toString());
            DtoQuery dto = super.getDtoQuery(true);
            dto.setOrderby("t.pk.rulecode");

            HashMap<String, DtoQuery> mtables = new HashMap<String, DtoQuery>();
            mtables.put(this.beanalias, dto); // permite adicionar mas de una
            // tabla.

            request.setQueryTables(mtables);

            Response resp = this.callerhelper.executeQuery(request);

            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) {
                this.lrecord = new ArrayList<TbpmRules>();
                MessageHelper.setMessageError(resp);
            } else {
                this.lrecord = (List<TbpmRules>) resp.get(this.beanalias);
                super.postQuery(this.lrecord);
            }

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

    public void setTbpmRules() {
        // se utiliza en la pagina del lov.
        RequestContext.getCurrentInstance().closeDialog(this.record);
    }

    public void setTbpmRules(TbpmRules tbpmRules) {
        // se utiliza en la pagina del lov.
        RequestContext.getCurrentInstance().closeDialog(tbpmRules);
    }

    public static void openLov(Map<String, List<String>> params) {
        Map<String, Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put(EnumLovOption.HEIGHT.getLabel(), 450);
        options.put(EnumLovOption.WIDTH.getLabel(), 580);
        options.put(EnumLovOption.RESIZABLE.getLabel(), false);

        RequestContext.getCurrentInstance().openDialog("/pages/general/lov/bpmRulesLov.xhtml", options, params);
    }
}
