/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cosmodas.login;

import com.cosmodas.FacadeCosmos.SucursalesFacadeLocal;
import com.cosmodas.FacadeCosmos.Top20FacadeLocal;
import com.cosmodas.EntCosmos.Sucursales;
import com.cosmodas.EntCosmos.WcOpciones;
import com.cosmodas.EntCosmos.WcUsuarios;
import java.io.IOException;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author obper
 */
@Named
@ViewScoped
public class bInfoSesion implements Serializable {

    @EJB
    SucursalesFacadeLocal sfl;
    @EJB
    Top20FacadeLocal top20fl;

    private WcUsuarios user;
    @Inject
    private Sucursales sucursal;
    private ExternalContext exc;
    private String impresorComanda;

    @PostConstruct
    public void init() {
        this.exc = FacesContext.getCurrentInstance().getExternalContext();
        this.user = (WcUsuarios) exc.getSessionMap().get("usuario");
        this.impresorComanda = "--";

        diccionIp();
        exc.getSessionMap().put("sucursal", getSucursal());
        exc.getSessionMap().put("ompp", getImpresorComanda());

    }

    public String getImpresorComanda() {
        return impresorComanda;
    }

    public void setImpresorComanda(String impresorComanda) {
        this.impresorComanda = impresorComanda;
    }

    public Sucursales getSucursal() {
        return sucursal;
    }

    public void setSucursal(Sucursales sucursal) {
        this.sucursal = sucursal;
    }

    public WcUsuarios getUser() {
        return user;
    }

    public void setUser(WcUsuarios user) {
        this.user = user;
    }

    public bInfoSesion() {
    }

    public void cerrarSesion() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

    }

    public void verificaSesion() throws IOException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        try {
            this.user = (WcUsuarios) ctx.getExternalContext().getSessionMap().get("usuario");
            if (user == null) {
                ctx.getExternalContext().redirect("./../access-denied.jsf");
            } else {
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
                String uri = request.getRequestURI().replaceAll("/cosnet/apps/", "");
                List<WcOpciones> lisOps = user.getGrupoUsuario().getWcOpcionesList();
                boolean tienePermisos = false;

                for (WcOpciones lisOp : lisOps) {
                    if (lisOp.getUrl().equals(uri)) {
                        tienePermisos = true;
                    }
                }

                if (!tienePermisos) {
                    ctx.getExternalContext().redirect("404.jsf");
                }

            }
        } catch (Exception e) {
            ctx.getExternalContext().redirect("./../access-denied.jsf");

        }

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String uri = request.getRequestURI().replaceAll("/cosnet/apps/", "");

    }

    public void verificaSesionIndex() throws IOException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        try {
            this.user = (WcUsuarios) ctx.getExternalContext().getSessionMap().get("usuario");
            if (user != null) {

                ctx.getExternalContext().redirect("/cosnet/apps/" + home() + ".jsf");
            }

        } catch (Exception e) {
            ctx.getExternalContext().redirect("index.jsf");
        }
    }

    public void diccionIp() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        Map<String, Object> sessionMap = externalContext.getSessionMap();
        String redProcedente = "";

        try {
            HttpServletRequest request = (HttpServletRequest) (FacesContext.getCurrentInstance().getExternalContext().getRequest());
            // Get the IP-address of the client.  
            String ip = request.getRemoteAddr();
            String[] parts = ip.split(Pattern.quote("."));
            redProcedente = parts[2];

            if (Integer.valueOf(parts[0]) == 192 && Integer.valueOf(parts[1]) == 168) {

                switch (Integer.parseInt(redProcedente)) {

                    case 0:
                        setSucursal(sfl.find("01"));
                        break;

                    case 1:
                        if (Integer.parseInt(parts[3]) >= 43) {
                            setImpresorComanda("REPARTO");
                        } else {
                            setImpresorComanda("CENTRAL");

                        }
                        setSucursal(sfl.find("02"));

                        break;
                    case 2:
                        setSucursal(sfl.find("03"));
                        break;
                    case 3:
                        setSucursal(sfl.find("05"));
                        break;
                    case 4:
                        setSucursal(sfl.find("08"));
                        break;
                    case 5:
                        setSucursal(sfl.find("07"));
                        break;
                    case 6:
                        setSucursal(sfl.find("06"));
                        break;
                    case 7:
                        setSucursal(sfl.find("04"));
                        break;
                    case 8:
                        setSucursal(sfl.find("11"));
                        break;
                    case 12:
                        setSucursal(sfl.find("03"));
                        break;
                    case 13:
                        setSucursal(sfl.find("05"));
                        break;
                    case 14:
                        setSucursal(sfl.find("08"));
                        break;
                    case 15:
                        setSucursal(sfl.find("07"));
                        break;
                    case 16:
                        setSucursal(sfl.find("06"));
                        break;
                    case 17:
                        setSucursal(sfl.find("04"));
                        break;
                    case 18:
                        setSucursal(sfl.find("11"));
                        break;
                    default:
                        setSucursal(sfl.find("02"));
                        break;

                }
            }else{
                setSucursal(sfl.find(user.getSucursal()));
            }

        } catch (Exception e) {
        }
    }

    public String home() {
        String direccion = "menus";

        if (user.getGrupoUsuario().getTipoGruposUsuarios()) {
            direccion = "dashboard";
        } else {
            direccion = "menus";
        }
        return direccion;
    }

}
