package com.fp.frontend.controller.armas.solicitud;

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

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

import com.fp.dto.Request;
import com.fp.dto.Response;
import com.fp.dto.query.DtoQuery;
import com.fp.dto.save.DtoSave;
import com.fp.frontend.controller.AbstractController;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.parmas.soli.TarmAgenciaArma;
import com.fp.persistence.parmas.soli.TarmSolicitud;

@ManagedBean
@ViewScoped
public class AgenciaArmaController extends AbstractController<TarmAgenciaArma> {

    private static final long serialVersionUID = -8596957712603749520L;

    public AgenciaArmaController() throws Exception {
        super(TarmAgenciaArma.class);
    }

    @PostConstruct
    private void postconstruct() {
        this.init();
    }

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

    /**
     * Busca las Agencias asociadas a las armas seg&uacute;n los criterios
     * indicados
     */
    @SuppressWarnings("unchecked")
    @Override
    protected void querydatabase() {
        try {
            DtoQuery dto = this.getDtoQuery(Boolean.TRUE);
            HashMap<String, DtoQuery> mtables = new HashMap<String, DtoQuery>();
            mtables.put(beanalias, dto);
            Request request = callerhelper.getRequest();
            request.setQueryTables(mtables);
            Response response = callerhelper.executeQuery(request);
            if (response.getResponseCode().compareTo(Response.RESPONSE_OK) == 0) {
                this.lrecord = (List<TarmAgenciaArma>) response.get(beanalias);
                this.postQuery(lrecord);
            } else {
                this.lrecord = new ArrayList<>();
                MessageHelper.setMessageError(response);
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    @Override
    public void save() {
        try {
            DtoSave dto = this.getDtoSave();
            dto.setReturnpk(Boolean.TRUE);
            HashMap<String, DtoSave> msaves = new HashMap<String, DtoSave>();

            msaves.put(this.beanalias, dto);
            Request request = callerhelper.getRequest();
            request.setSaveTables(msaves);

            Response response = callerhelper.executeSave(request);
            if (response.getResponseCode().compareTo(Response.RESPONSE_OK) == 0) {
                this.postCommit(response);
                MessageHelper.setMessageInfo(response);
            } else {
                MessageHelper.setMessageError(response);
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    public static TarmAgenciaArma findByCArma(String cArma, String cAgencia) {
        try {
            AgenciaArmaController cc = new AgenciaArmaController();
            cc.init();
            cc.recperpage = 10000;
            cc.addFilter("pk.carma", cArma);
            cc.addFilter("pk.cagencia", cAgencia);
            cc.querydatabase();
            if (cc.lrecord.size() > 0) {
                return cc.lrecord.get(0);
            }
            return null;
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
            return null;
        }
    }

}
