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

import com.triz.trizservice.modeles.Etablissement;
import com.triz.trizservice.modeles.ServiceEtablissementUser;
import com.triz.trizservice.modeles.ServiceUser;
import com.triz.trizservice.modeles.ServicehistoriqueConnection;
import com.triz.trizservice.service.TransactionService;
import com.triz.util.UtilContext;
import com.triz.util.UtilGetParametre;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.core.Authentication;
import com.triz.util.UtilURL;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author abderrahim.TOUAIBIA
 */
@Service
public class CostumSuccesHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private TransactionService transactionService;

    @Autowired
    public UtilContext context;

    public CostumSuccesHandler() {

    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws ServletException, IOException {

        setDefaultTargetUrl("/" + multiEtablissement());
        super.onAuthenticationSuccess(request, response, authentication);
    }

    private void saveConnection(ServiceUser user, Etablissement etablissement) {
        ServicehistoriqueConnection his = new ServicehistoriqueConnection();
        his.setFkUser(user);
        his.setDate(new Date());
        his.setHeur(new Date());
        his.setAdresseIp("");
        his.setFkEtablissement(etablissement);
        transactionService.save(his);
    }

    public String multiEtablissement() {

        //  useDistribution = UtilGetParametre.useTrizDistribution();
        ServiceUser user = context.getCurrentUser();

        System.out.println("-------------------- multi etablissement");
        List<Etablissement> listUserEtab = transactionService.getAllStockEtablissementFromUsersEtablissementByUser(user);
        System.out.println("-------------------- listUserEtab  " + listUserEtab);
        UtilURL.showURL();
        if (listUserEtab.size() == 1) {
            Etablissement etablissement = listUserEtab.get(0);
            ServiceEtablissementUser userEtablissement = transactionService.getAllUserEtablissementByEtablissementByUser(etablissement, user);
            UtilURL.setIdEtablissement(etablissement.getIdEtabStock());
            UtilURL.setFonctionnalite(transactionService.getFonctionServerByUsersByEtablissement(context.getCurrentUser(), etablissement));
            saveConnection(user, etablissement);
//            if ("production".equals(etablissement.getType_etablissemnt())) {
//                UtilURL.setIsProuction(true);
//                return UtilURL.URL_PRODUCTION;
//            }
//            if (("true".equals(userEtablissement.getUseSuperette()))) {
//                return UtilURL.STOCK_URL_VENTECOMPTOIRE;
//            }
//            if (context.getUseTB() && context.isshow1("Acceuil")) {
//                return UtilURL.STOCK_URL_TB;
//            }
            return UtilURL.STOCK_URL;

        }

        if (listUserEtab.size() > 1) {
            ServicehistoriqueConnection hist = transactionService.getLastStockHistoriqueConnectionByUsers(user);
            if (hist != null) {
                Etablissement lasteEtablissement = hist.getFkEtablissement();
//                if ("true".equals(hist.getIsTableauBord())) {
//                    if ("true".equals(user.getShowTableauDeBord())) {
//                        UtilURL.setIsTableauDeBord(true);
//                        UtilURL.setIdEtablissement(null);
//                        UtilURL.setFonctionnaliteServer(null);
//                        UtilURL.setIsProuction(false);
//                        saveConnection(user, "true", null);
//                        return UtilURL.STOCK_URL;
//                    }
//                }
                if (lasteEtablissement != null) {
                    if (listUserEtab.contains(lasteEtablissement)) {
                        // saveConnection(user, "false", lasteEtablissement);
                        ServiceEtablissementUser userEtablissement = transactionService.getAllUserEtablissementByEtablissementByUser(lasteEtablissement, user);
                        UtilURL.setIdEtablissement(lasteEtablissement.getIdEtabStock());
                        UtilURL.setFonctionnalite(transactionService.getFonctionServerByUsersByEtablissement(context.getCurrentUser(), lasteEtablissement));
//                        if ("production".equals(lasteEtablissement.getType_etablissemnt())) {
//                            UtilURL.setIsProuction(true);
//                            return UtilURL.URL_PRODUCTION;
//                        }
//                        UtilURL.setIsProuction(false);
//                        if (("true".equals(userEtablissement.getUseSuperette()))) {
//                            return UtilURL.STOCK_URL_VENTECOMPTOIRE;
//                        }
//                        if (context.getUseTB() && context.isshow1("Acceuil")) {
//                            return UtilURL.STOCK_URL_TB;
//                        }
                        return UtilURL.STOCK_URL;
                    }
                }
            }
            return UtilURL.STOCK_URL;
        }
        return UtilURL.STOCK_URL;

    }

}
