package com.fp.frontend.controller.pgeneral.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.pgeneral.gene.TgeneEntity;

/**
 *
 * @author Jacqueline Arias
 *
 * @version 2.1
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class EntityLovController extends AbstractController<TgeneEntity> {

    public EntityLovController() throws Exception {
        super(TgeneEntity.class);
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
    public void postConstruct() {
        this.init();
        try {
            String log;
            log = CallerHelper.getLovParameter("log");
            if (log != null) {
                this.addFilter("log", log);
            }
            this.query();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private void init() {
        this.lrecord = new ArrayList<>();
        this.beanalias = "TGENEENTITY";
        this.recperpage = 10;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void querydatabase() {
        // TODO Auto-generated method stub
        try {
            Request request = this.callerhelper.getRequest();

            DtoQuery dto = super.getDtoQuery(true);
            dto.setOrderby("t.pk");
            HashMap<String, DtoQuery> mtables = new HashMap<String, DtoQuery>();
            mtables.put(this.beanalias, dto);
            request.setQueryTables(mtables);
            Response response = this.callerhelper.executeQuery(request);
            if (response.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) {
                this.lrecord = new ArrayList<TgeneEntity>();
                MessageHelper.setMessageError(response);
            } else {
                this.lrecord = (List<TgeneEntity>) response.get(this.beanalias);
                super.postQuery(this.lrecord);
            }
        } catch (Throwable e) {
            // TODO: handle exception
            MessageHelper.setMessageError(e);
        }

    }

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

    public void setentity(TgeneEntity tgeneEntity) {
        // se utiliza en la pagina del lov.
        RequestContext.getCurrentInstance().closeDialog(tgeneEntity);
    }

    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/general/lov/entityLov.xhtml", options, params);
    }

}
