package com.fp.frontend.controller.bpm;

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

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

import com.fp.dto.AbstractDataTransport;
import com.fp.dto.Request;
import com.fp.dto.Response;
import com.fp.frontend.controller.AbstractController;
import com.fp.frontend.controller.pgeneral.gene.ServicesController;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.pgeneral.gene.TgeneServices;

/**
 * Clase controladora del TaskServer.
 *
 * @author WPA.
 * @version 2.1
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class TaskServerController extends AbstractController<AbstractDataTransport> {

    /**
     * Constructor
     *
     * @throws Exception
     */
    public TaskServerController() throws Exception {
        super(AbstractDataTransport.class);
    }

    /**
     * Atributo para la url de la p&aacute;gina
     */
    private String page;

    /**
     * Atributo para habilitar el inicio
     */
    private boolean lockStart;

    /**
     * Atributo para habilitar el detener
     */
    private boolean lockStop;

    /**
     * Atributo para la lista de servicios
     */
    private List<TgeneServices> lservices;

    /**
     * Metodo invocado despues de instanciar el controlador
     */
    @PostConstruct
    private void postconstruct() {
        this.init();
        // Inicializa autoconsulta
        super.startQuery();
        //List
        this.lservices = ServicesController.find();
    }

    /**
     * Incializa variables del 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 = "SERVICES";
            //page
            this.page = "/resources/images/blanck.jpg";
            //lock
            this.lockStart = false;
            this.lockStop = false;

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

    /* (non-Javadoc)
	 * @see com.fp.frontend.controller.AbstractController#querydatabase()
     */
    @SuppressWarnings("unchecked")
    @Override
    protected void querydatabase() {
        MessageHelper.setMessageError("msg_notQuery");
    }

    /* (non-Javadoc)
	 * @see com.fp.frontend.controller.AbstractController#save()
     */
    @Override
    public void save() {
        MessageHelper.setMessageError("msg_notSave");
    }

    public void queryTransaction() {
        try {
            if (super.getMfilters().get("servicecode") == null) {
                this.init();
                return;
            }

            Request request = this.callerhelper.getRequest();
            request.put("querytype", "T");
            request.put("code", super.getMfilters().get("servicecode"));

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

            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) {
                MessageHelper.setMessageError(resp);
            } else {
                this.responseService((Boolean) resp.get("TSStatus"));
            }

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

    /**
     * Ejecuta una peticion
     *
     * @param status I/S
     */
    public void processServer(String status) {
        try {
            Request request = this.callerhelper.getRequest();
            request.put("status", status);
            request.put("code", super.getMfilters().get("servicecode"));

            Response resp = this.callerhelper.executeSave(request);
            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) == 0) {
                this.postCommit(resp);
                this.responseService((Boolean) resp.get("TSStatus"));
                MessageHelper.setMessageInfo(resp);
            } else {
                MessageHelper.setMessageError(resp);
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    /**
     * Procesa la respuesta
     *
     * @param response
     */
    public void responseService(boolean response) {
        if (response) {
            this.page = "/resources/images/green.jpg";
            this.lockStart = false;
            this.lockStop = true;
        } else {
            this.page = "/resources/images/red.jpg";
            this.lockStart = true;
            this.lockStop = false;
        }
    }

    /**
     * Entrega una lista de objetos TgeneServices
     *
     * @return lservices
     */
    public List<TgeneServices> getLservices() {
        return lservices;
    }

    /**
     * Fija una lista de objetos TgeneServices
     *
     * @param lservices
     */
    public void setLservices(List<TgeneServices> lservices) {
        this.lservices = lservices;
    }

    /**
     * Entrega el valor de page
     *
     * @return page
     */
    public String getPage() {
        return page;
    }

    /**
     * Fija el valor de page
     *
     * @param page
     */
    public void setPage(String page) {
        this.page = page;
    }

    /**
     * Entrega V/F lockStart
     *
     * @return lockStart
     */
    public boolean isLockStart() {
        return lockStart;
    }

    /**
     * Fija V/F lockStart
     *
     * @param lockStart
     */
    public void setLockStart(boolean lockStart) {
        this.lockStart = lockStart;
    }

    /**
     * Entrega V/F lockStop
     *
     * @return lockStop
     */
    public boolean isLockStop() {
        return lockStop;
    }

    /**
     * Fija V/F lockStop
     *
     * @param lockStop
     */
    public void setLockStop(boolean lockStop) {
        this.lockStop = lockStop;
    }
}
