package com.fp.frontend.controller.armas.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.frontend.controller.AbstractController;
import com.fp.frontend.controller.pgeneral.gene.CatalogDetailController;
import com.fp.frontend.enums.EnumLovOption;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.pgeneral.gene.TgeneCatalogDetail;

/**
 * Clase controladora del lov asociado al bean TgeneCatalog.
 *
 * @author Jorge Vaca.
 * @version 2.1
 */
@ManagedBean
@ViewScoped
public class CentroControlLovController extends AbstractController<TgeneCatalogDetail> {

    private static final long serialVersionUID = 1L;

    public CentroControlLovController() throws Exception {
        super(TgeneCatalogDetail.class);
    }

    @PostConstruct
    private void postconstruct() {
        this.init();
        try {
            /*			String codeperson = CallerHelper.getLovParameter("personcode");
			if (codeperson != null) {
	            super.addFilter("personcode", codeperson);
	        }*/
            super.startQuery();
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }

    /**
     * Incializa el controlador, cuando se esta utilizando una pagina que utliza
     * el controlador.
     */
    private void init() {
        try {
            this.recperpage = 15; // Cambiar al # reg a mirar.
            this.lrecord = new ArrayList<>();
            this.beanalias = "CENTROCONTROLLOV";
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }

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

            List<TgeneCatalogDetail> listaCatalogoDetail = CatalogDetailController.find("NOMBREINSTITUCION");
            for (TgeneCatalogDetail catalogDetail : listaCatalogoDetail) {
                record = catalogDetail;
                System.out.println("***********" + catalogDetail.getDescription());
            }
            System.out.println("Fin");
            this.lrecord = listaCatalogoDetail;

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

    public void setcatalog(TgeneCatalogDetail catalogD) {
        // se utiliza en la pagina del lov.
        RequestContext.getCurrentInstance().closeDialog(catalogD);
    }

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

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