package com.fp.frontend.controller.pcustomer;

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.AbstractDataTransport;
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.MessageHelper;

/**
 * Clase controladora del bean TsafeUserDetail.
 *
 * @author Andres E. Carpio
 * @version 2.1
 */
@ManagedBean
@ViewScoped
public class PersonViewLovController extends AbstractController<AbstractDataTransport> {

    private static final long serialVersionUID = 1L;

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

    /**
     * Atributo para la lista de empleados sbs
     */
    private List<Object[]> lemployee;

    /**
     * Atributo para el objeto empleado seleccionado
     */
    private Object[] employee;

    /**
     * Post construct
     *
     * @throws Exception
     */
    @PostConstruct
    private void postconstruct() throws Exception {
        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 = "EMPLOYEESBS";
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void querydatabase() {
        try {

            Request request = this.callerhelper.getRequest();
            DtoQuery dto = super.getDtoQuery(true);

            HashMap<String, DtoQuery> mtables = new HashMap<String, DtoQuery>();
            mtables.put(this.beanalias, dto); // permite adicionar mas de una tabla.
            request.setQueryTables(mtables);
            request.put("querytype", "P");
            request.put("queryalias", "EMPLOYEESBS");

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

            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) {
                this.lemployee = new ArrayList<>();
                MessageHelper.setMessageError(resp);
            } else {
                this.lemployee = (List<Object[]>) resp.get(this.beanalias);
            }

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

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

    public static void openLov(Map<String, List<String>> params) {
        Map<String, Object> options = new HashMap<String, Object>();
        // hint: ver EnumLovOption para las opciones del modal
        options.put(EnumLovOption.MODAL.getLabel(), true);
        options.put(EnumLovOption.HEIGHT.getLabel(), 450);
        options.put(EnumLovOption.WIDTH.getLabel(), 700);
        options.put(EnumLovOption.RESIZABLE.getLabel(), false);

        // lovpersona es el nombre de la pagina
        RequestContext.getCurrentInstance().openDialog("/pages/customer/lov/personViewLov.xhtml", options, params);
    }

    /**
     * Entrega una lista de arreglo de objetos
     *
     * @return lemployee
     */
    public List<Object[]> getLemployee() {
        return lemployee;
    }

    /**
     * Fija una lista de arreglo de objetos
     *
     * @param lemployee
     */
    public void setLemployee(List<Object[]> lemployee) {
        this.lemployee = lemployee;
    }

    /**
     * Entrega un objeto employee
     *
     * @return employee
     */
    public Object[] getEmployee() {
        return employee;
    }

    /**
     * FIna un objeto employee
     *
     * @param employee
     */
    public void setEmployee(Object[] employee) {
        this.employee = employee;
    }

}
