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.dto.query.Filter;
import com.fp.dto.query.SubQuery;
import com.fp.frontend.controller.AbstractController;
import com.fp.frontend.enums.EnumLovOption;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.pgeneral.safe.TsafeUserDetail;

/**
 * Clase controladora de bean TsafeUserDetail
 *
 * @author Jacqueline Arias
 *
 * @version 2.1
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class UserLovControler extends AbstractController<TsafeUserDetail> {

    private String userName = null;

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

    @PostConstruct
    public void postConstruct() {
        this.init();
    }

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

    @SuppressWarnings("unchecked")
    @Override
    protected void querydatabase() {
        // TODO Auto-generated method stub
        try {
            Request request = this.callerhelper.getRequest();
            super.addFilterDateto();
            super.addFilter("companycode", super.getLoginController().getRequest().getCompany().toString());
            super.addFilter("branchcode", super.getLoginController().getRequest().getBranchCode().toString());
            super.addFilter("officecode", super.getLoginController().getRequest().getOfficeCode().toString());
            DtoQuery dto = super.getDtoQuery(true);
            dto.setOrderby("t.pk");

            if ((this.userName != null) && !this.userName.isEmpty()) {
                Filter f = null;
                f = new Filter();
                f.setSql(" t.pk.personcode in (select p.pk.personcode from com.fp.persistence.pcustomer.gene.TcustPersonDetail  p where   p.name like '"
                        + this.userName
                        + "' and p.pk.dateto = to_date('2999-12-31', 'yyyy-MM-dd') and p.companycode ="
                        + super.getLoginController().getRequest().getCompany().toString() + ")");
                dto.addFiltro(f);

            }
            SubQuery subquery = new SubQuery("TcustPersonDetail", "name", "personname",
                    "i.pk.personcode = t.pk.personcode and i.pk.dateto = to_date('2999-12-31', 'yyyy-MM-dd') and i.companycode="
                    + super.getLoginController().getRequest().getCompany().toString());
            dto.addSubQuery(subquery);

            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<TsafeUserDetail>();
                MessageHelper.setMessageError(response);
            } else {
                this.lrecord = (List<TsafeUserDetail>) response.get(this.beanalias);
                super.postQuery(this.lrecord);
            }
        } catch (Throwable e) {
            // TODO: handle exception
            MessageHelper.setMessageError(e);
        }

    }

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

    public void setuser(TsafeUserDetail tsafeUserDetail) {
        // se utiliza en la pagina del lov.
        RequestContext.getCurrentInstance().closeDialog(tsafeUserDetail);
    }

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

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}
