/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.triz.util;

import com.triz.trizservice.modeles.ServiceFonctiongroupe;
import com.triz.trizservice.modeles.ServiceFonctionnaliter;
import com.triz.trizservice.service.TransactionService;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author abderrahim.TOUAIBIA
 */
@Service()
public final class UtilURL {

    private static Logger logger = LoggerFactory.getLogger(UtilURL.class);
    public static String STOCK_URL_CHEMAIN = "favorites/dashboard.xhtml";
    public static String STOCK_URL_CHEMAIN_TB = "favorites/dashboard.xhtml";
    public static String STOCK_URL_VENTECOMPTOIRE_CHEMAIN = "/view/venteSuperette/list.xhtml";
    public static String DISTRIBUTION_URL = "faces/view/telephone/listActif.xhtml";
    public static String TABLEAU_URL = "favorites/dashboard.xhtml";
    public static String TABLEAU_URLTB = "favorites/dashboard.xhtml";
    public static String STOCK_URL = "favorites/dashboard.xhtml";
    public static String STOCK_URL_TB = "favorites/dashboard.xhtml";
    public static String STOCK_URL_VENTECOMPTOIRE = "faces/view/venteSuperette/list.xhtml";
    public static String DISTRIBUTION_URL_Multi_Etablissement = "faces/view/choixEtablissement/list.xhtml";
    public static String DISTRIBUTION_URL_Caisse = "faces/view/venteSuperette/form.xhtml";
    public static String DISTRIBUTION_ACCES_DENIEDED_URL = "faces/accesDenieded.xhtml";
    public static String DISTRIBUTION_ADMIN_URL = "faces/view/admin/list.xhtml";
    public static String URL_PRODUCTION = "faces/view/production/list.xhtml";
    public static String DISTRIBUTION_URL_CARTE = "favorites/dashboard.xhtml";
    public final static String DISTRIBUTION_CHANGER_MP_Url = "view/profile/changerMotDePasse.xhtml";
    public static String idEntreprise;
    private String nomEnreprise;
    private static Boolean isTableauDeBord = null;
    private static Boolean isProuction = null;

    private List<String> userGroup;

    @Autowired
    private TransactionService service;

    @Autowired
    public UtilContext context;

    public boolean showDash(String groupVisibility) {

        if (groupVisibility != null) {

            for (String ele : groupVisibility.split(";")) {
                if (userGroup.contains(ele)) {
                    return true;
                }

            }
        }

        return false;
    }

    public static String getService_CODE_SYSTEM() {
        return getIdEntreprise();
    }

    public UtilURL() {
    }

    public static String decodeParam(String param) {
        if (param == null || param.isEmpty()) {
            return param;
        }
        try {
            param = URLDecoder.decode(param, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            logger.error(null, ex);
        }
        return param;
    }

    public static String encodeParam(String param) {
        if (param == null || param.isEmpty()) {
            return param;
        }
        try {
            param = URLEncoder.encode(param, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            logger.error(null, ex);
        }
        return param;
    }

    public static String getIdEntreprise() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();

        return (String) mySessin.getAttribute("idEntreprise");
    }

    public static void setIdEnreprise(String idEntreprise) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();
        mySessin.setAttribute("idEntreprise", idEntreprise);
    }

    public static boolean isIsTableauDeBord() {
//        if(isTableauDeBord==null){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();
        isTableauDeBord = false;
        if (mySessin.getAttribute("isTableauDeBordStock") != null) {
            isTableauDeBord = (boolean) mySessin.getAttribute("isTableauDeBordStock");
        }

        System.out.println("isTableauDeBord = " + isTableauDeBord);
//        }
        if (isTableauDeBord == null) {
            return false;
        }
        return isTableauDeBord;
    }

    public static void showURL() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();
        System.out.println("request.getContextPath()" + request.getContextPath());
        System.out.println("request.getLocalAddr()" + request.getLocalAddr());
        System.out.println("request.getLocalName()" + request.getLocalName());
        System.out.println("request.getLocalPort()" + request.getLocalPort());
        System.out.println("request.getRemoteAddr()" + request.getRemoteAddr());
        System.out.println("request.getPathInfo()" + request.getPathInfo());
        System.out.println("request.getRequestURI()" + request.getRequestURI());
        System.out.println("request.getServletPath()" + request.getServletPath());
        System.out.println("request.toString()" + request.toString());
        System.out.println("request.getRemoteHost()" + request.getRemoteHost());
        System.out.println("request.getRemotePort()" + request.getRemotePort());
        System.out.println("request.getRemoteUser()" + request.getRemoteUser());
    }

//    request.getContextPath()/TrizStockV2
//    request.getLocalAddr()192.168.1.35
//    request.getLocalName()DESKTOP-SUETGA1
//    request.getLocalPort()8182
//    request.getRemoteAddr()192.168.1.43
//    request.getPathInfo()null
//    request.getRequestURI()/TrizStockV2/j_spring_security_check
//    request.getServletPath()/j_spring_security_check
//    request.toString()SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.context.HttpSessionSecurityContextRepository$Servlet3SaveToSessionRequestWrapper@53e90dfd]
//    request.getRemoteHost()192.168.1.43
//    request.getRemotePort()52085
//    request.getRemoteUser()admin
    public static void resetSession() {
        System.out.println("// ******   resetSession()");
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession session = request.getSession();
        if (session != null && !session.isNew()) {
            session.invalidate();
            request.getSession(true);
        }
    }

    public static String getURLPath(String ip) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        ip = ip + ":" + request.getLocalPort() + request.getContextPath();
        System.out.println("getURLPath:" + ip);
        return ip;
    }

    public static String getContext() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        String ip;
        ip = request.getContextPath();
        System.out.println("getURLPath:" + ip);
        return ip;
    }

    public static void setFonctionnalite(List<ServiceFonctionnaliter> fonction) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();
        mySessin.setAttribute("fonctionStock", fonction);
    }

    public static List<ServiceFonctionnaliter> getFonctionnalite() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();

        return (List<ServiceFonctionnaliter>) mySessin.getAttribute("fonctionStock");
    }

    public static String getStock_CODE_SYSTEM() {
        return getIdEtablissement();
    }

    public static String getIdEtablissement() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();

        return (String) mySessin.getAttribute("idEtablissementStock");
    }

    public static void setIdEtablissement(String idEtablissement) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();
        mySessin.setAttribute("idEtablissementStock", idEtablissement);
    }
    
      public static void setFonctionnaliteServer(List<ServiceFonctiongroupe> fonction) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();
        mySessin.setAttribute("fonctionStock", fonction);
    }

    public static List<ServiceFonctiongroupe> getFonctionnaliteServer() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession mySessin = request.getSession();

        return (List<ServiceFonctiongroupe>) mySessin.getAttribute("fonctionStock");
    }
}
