package com.gyee.runeconomy.service;

import cn.hutool.core.bean.BeanUtil;
import com.gyee.common.model.StringUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.model.vo.TokenVo;
import com.gyee.runeconomy.service.auto.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class TokenService {
    @Resource
    private ISysUserService sysUserService;


    public static Map<String, List<ProBasicEquipment>> sessionWtMap = new ConcurrentHashMap<>();
    public static Map<String, List<ProBasicPowerstation>> sessionWpMap = new ConcurrentHashMap<>();
    public static Map<String, List<ProBasicSubStation>> sessionSubMap = new ConcurrentHashMap<>();

    public static Map<String, List<ProBasicWeatherStation>> sessionWsMap = new ConcurrentHashMap<>();
    public static Map<String, List<ProBasicWeatherStation>> sessionWeMap = new ConcurrentHashMap<>();
    public static Map<String, List<ProBasicProject>> sessionPjMap = new ConcurrentHashMap<>();
    public static Map<String, TokenVo> sessionMap = new ConcurrentHashMap<>();


    //    public void  judegeSession() {
    //
    //
    //
    //        for (Map.Entry <String, TokenVo>  entry : sessionMap.entrySet()) {
    //            TokenVo vo=entry.getValue();
    //
    //            Date begin=vo.getTime();
    //            Date end=new Date();
    //
    //            double value=DateUtils.hoursDiff(begin,end);
    //            if(value>24)
    //            {
    //                sessionMap.remove(entry.getKey());
    //
    //                if (sessionWtMap.containsKey(entry.getKey())) {
    //                    sessionWtMap.remove(entry.getKey());
    //                }
    //                if (sessionWpMap.containsKey(entry.getKey())) {
    //                    sessionWpMap.remove(entry.getKey());
    //
    //                }
    //                if (sessionSubMap.containsKey(entry.getKey())) {
    //                    sessionSubMap.remove(entry.getKey());
    //
    //                }
    //                if (sessionWeMap.containsKey(entry.getKey())) {
    //                    sessionWeMap.remove(entry.getKey());
    //
    //                }
    //            }
    //        }
    //
    //    }
    public List<ProBasicEquipment> getWtls(HttpServletRequest request) {

        String token = request.getHeader("token");
        String userId = request.getHeader("userId");

        List<ProBasicEquipment> wtlist = new ArrayList<>();
        if (StringUtils.notEmp(token) && StringUtils.notEmp(userId)) {
            if (sessionMap.containsKey(token) && !sessionWtMap.containsKey(token)) {

                List<String> depls = sysUserService.getUserByuserId(Long.valueOf(userId));
                if (!depls.isEmpty()) {
                    String depId = depls.get(0);

                    if (CacheContext.wpwtmap.containsKey(depId)) {

                        List<ProBasicEquipment> wtls = CacheContext.wpwtmap.get(depId);
                        for (ProBasicEquipment wt : wtls) {
                            ProBasicEquipment newWt = new ProBasicEquipment();
                            BeanUtil.copyProperties(wt, newWt);
                            wtlist.add(newWt);
                        }

                    } else if (CacheContext.cmwtlsmap.containsKey(depId)) {

                        List<ProBasicEquipment> wtls = CacheContext.cmwtlsmap.get(depId);
                        for (ProBasicEquipment wt : wtls) {
                            ProBasicEquipment newWt = new ProBasicEquipment();
                            BeanUtil.copyProperties(wt, newWt);
                            wtlist.add(newWt);
                        }

                    } else if (CacheContext.rgwtlsmap.containsKey(depId)) {

                        List<ProBasicEquipment> wtls = CacheContext.rgwtlsmap.get(depId);
                        for (ProBasicEquipment wt : wtls) {
                            ProBasicEquipment newWt = new ProBasicEquipment();
                            BeanUtil.copyProperties(wt, newWt);
                            wtlist.add(newWt);
                        }

                    } else if (depId.equals("0")) {
                        for (ProBasicEquipment wt : CacheContext.wtls) {
                            ProBasicEquipment newWt = new ProBasicEquipment();
                            BeanUtil.copyProperties(wt, newWt);
                            wtlist.add(newWt);
                        }

                    }
                }
                sessionWtMap.put(token, wtlist);
            } else if (sessionMap.containsKey(token) && sessionWtMap.containsKey(token)) {
                wtlist = sessionWtMap.get(token);
            }
        } else {
            for (ProBasicEquipment wt : CacheContext.wtls) {
                ProBasicEquipment newWt = new ProBasicEquipment();
                BeanUtil.copyProperties(wt, newWt);
                wtlist.add(newWt);
            }

        }
        return wtlist;
    }

    public List<ProBasicPowerstation> getWpls(HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = request.getHeader("userId");

        List<ProBasicPowerstation> wplist = new ArrayList<>();
        if (StringUtils.notEmp(token) && StringUtils.notEmp(token)) {
            if (sessionMap.containsKey(token) && !sessionWpMap.containsKey(token)) {

                List<String> depls = sysUserService.getUserByuserId(Long.valueOf(userId));
                if (!depls.isEmpty()) {
                    String depId = depls.get(0);

                    if (CacheContext.wpmap.containsKey(depId)) {

                        ProBasicPowerstation wp = CacheContext.wpmap.get(depId);
                        ProBasicPowerstation newWp = new ProBasicPowerstation();
                        BeanUtil.copyProperties(wp, newWp);
                        wplist.add(newWp);
                    } else if (CacheContext.cpwpmap.containsKey(depId)) {
                        List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(depId);
                        for (ProBasicPowerstation wp : wpls) {
                            ProBasicPowerstation newWp = new ProBasicPowerstation();
                            BeanUtil.copyProperties(wp, newWp);
                            wplist.add(newWp);
                        }

                    } else if (CacheContext.rgmap.containsKey(depId)) {
                        List<ProBasicCompany> rgcpls = CacheContext.rgcpmap.get(depId);
                        for (ProBasicCompany cp : rgcpls) {
                            List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(cp.getId());
                            for (ProBasicPowerstation wp : wpls) {
                                ProBasicPowerstation newWp = new ProBasicPowerstation();
                                BeanUtil.copyProperties(wp, newWp);
                                wplist.add(newWp);
                            }
                        }

                    } else if (depId.equals("0")) {
                        List<ProBasicPowerstation> wpls = CacheContext.wpls;
                        for (ProBasicPowerstation wp : wpls) {
                            ProBasicPowerstation newWp = new ProBasicPowerstation();
                            BeanUtil.copyProperties(wp, newWp);
                            wplist.add(newWp);
                        }

                    }
                }
                sessionWpMap.put(token, wplist);
            } else if (sessionMap.containsKey(token) && sessionWpMap.containsKey(token)) {
                wplist = sessionWpMap.get(token);
            }

        } else {
            List<ProBasicPowerstation> wpls = CacheContext.wpls;
            for (ProBasicPowerstation wp : wpls) {
                ProBasicPowerstation newWp = new ProBasicPowerstation();
                BeanUtil.copyProperties(wp, newWp);
                wplist.add(newWp);
            }
        }
        return wplist;
    }

    public List<ProBasicSubStation> getSubwpls(HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = request.getHeader("userId");
        List<ProBasicSubStation> wplist = new ArrayList<>();
        if (StringUtils.notEmp(token) && StringUtils.notEmp(userId)) {
            if (!sessionSubMap.containsKey(token)) {

                List<String> depls = sysUserService.getUserByuserId(Long.valueOf(userId));
                if (!depls.isEmpty()) {
                    String depId = depls.get(0);

                    if (CacheContext.wpmap.containsKey(depId)) {
                        for (ProBasicSubStation subStation : CacheContext.subwpls) {
                            if (subStation.getWindpowerstationId().equals(depId)) {
                                ProBasicSubStation newSub = new ProBasicSubStation();
                                BeanUtil.copyProperties(subStation, newSub);
                                wplist.add(newSub);
                            }
                        }
                    } else if (CacheContext.cpwpmap.containsKey(depId)) {
                        List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(depId);

                        for (ProBasicPowerstation wp : wpls) {
                            for (ProBasicSubStation subStation : CacheContext.subwpls) {
                                if (subStation.getWindpowerstationId().equals(wp.getId())) {
                                    ProBasicSubStation newSub = new ProBasicSubStation();
                                    BeanUtil.copyProperties(subStation, newSub);
                                    wplist.add(newSub);
                                    break;
                                }
                            }
                        }
                    } else if (CacheContext.rgmap.containsKey(depId)) {
                        List<ProBasicCompany> rgcpls = CacheContext.rgcpmap.get(depId);
                        for (ProBasicCompany cp : rgcpls) {
                            List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(cp.getId());

                            for (ProBasicPowerstation wp : wpls) {
                                for (ProBasicSubStation subStation : CacheContext.subwpls) {
                                    if (subStation.getWindpowerstationId().equals(wp.getId())) {
                                        ProBasicSubStation newSub = new ProBasicSubStation();
                                        BeanUtil.copyProperties(subStation, newSub);
                                        wplist.add(newSub);
                                        break;
                                    }
                                }

                            }
                        }
                    } else if (depId.equals("0")) {
                        for (ProBasicSubStation subStation : CacheContext.subwpls) {
                            ProBasicSubStation newSub = new ProBasicSubStation();
                            BeanUtil.copyProperties(subStation, newSub);
                            wplist.add(newSub);
                        }

                    }
                }
                sessionSubMap.put(token, wplist);
            } else if (sessionMap.containsKey(token) && sessionSubMap.containsKey(token)) {
                wplist = sessionSubMap.get(token);

            }
        } else {
            for (ProBasicSubStation subStation : CacheContext.subwpls) {
                ProBasicSubStation newSub = new ProBasicSubStation();
                BeanUtil.copyProperties(subStation, newSub);
                wplist.add(newSub);
            }
        }
        return wplist;
    }


    public List<ProBasicWeatherStation> getWeawpls(HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = request.getHeader("userId");

        List<ProBasicWeatherStation> wplist = new ArrayList<>();
        if (StringUtils.notEmp(token) && StringUtils.notEmp(userId)) {
            if (!sessionWeMap.containsKey(token)) {

                List<String> depls = sysUserService.getUserByuserId(Long.valueOf(userId));
                if (!depls.isEmpty()) {
                    String depId = depls.get(0);

                    if (CacheContext.wpmap.containsKey(depId)) {
                        for (ProBasicWeatherStation weatherStation : CacheContext.weawpls) {
                            if (weatherStation.getWindpowerstationId().equals(depId)) {

                                ProBasicWeatherStation newWeb = new ProBasicWeatherStation();
                                BeanUtil.copyProperties(weatherStation, newWeb);
                                wplist.add(newWeb);
                            }
                        }

                    } else if (CacheContext.cpwpmap.containsKey(depId)) {
                        List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(depId);

                        for (ProBasicPowerstation wp : wpls) {
                            for (ProBasicWeatherStation weatherStation : CacheContext.weawpls) {
                                if (weatherStation.getWindpowerstationId().equals(wp.getId())) {
                                    ProBasicWeatherStation newWeb = new ProBasicWeatherStation();
                                    BeanUtil.copyProperties(weatherStation, newWeb);
                                    wplist.add(newWeb);
                                    break;
                                }
                            }

                        }
                    } else if (CacheContext.rgmap.containsKey(depId)) {
                        List<ProBasicCompany> rgcpls = CacheContext.rgcpmap.get(depId);
                        for (ProBasicCompany cp : rgcpls) {
                            List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(cp.getId());

                            for (ProBasicPowerstation wp : wpls) {
                                for (ProBasicWeatherStation weatherStation : CacheContext.weawpls) {
                                    if (weatherStation.getWindpowerstationId().equals(wp.getId())) {
                                        ProBasicWeatherStation newWeb = new ProBasicWeatherStation();
                                        BeanUtil.copyProperties(weatherStation, newWeb);
                                        wplist.add(newWeb);
                                        break;
                                    }
                                }

                            }
                        }

                    } else if (depId.equals("0")) {
                        for (ProBasicWeatherStation weatherStation : CacheContext.weawpls) {
                            ProBasicWeatherStation newWeb = new ProBasicWeatherStation();
                            BeanUtil.copyProperties(weatherStation, newWeb);
                            wplist.add(newWeb);
                        }
                    }
                }
                sessionWeMap.put(token, wplist);
            } else if (sessionMap.containsKey(token) && sessionWeMap.containsKey(token)) {
                wplist = sessionWeMap.get(token);
            }

        } else {
            for (ProBasicWeatherStation weatherStation : CacheContext.weawpls) {
                ProBasicWeatherStation newWeb = new ProBasicWeatherStation();
                BeanUtil.copyProperties(weatherStation, newWeb);
                wplist.add(newWeb);
            }


        }
        return wplist;
    }


    public List<ProBasicProject> gePjls(HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = request.getHeader("userId");

        List<ProBasicProject> pjlist = new ArrayList<>();
        if (StringUtils.notEmp(token) && StringUtils.notEmp(token)) {
            if (sessionMap.containsKey(token) && !sessionPjMap.containsKey(token)) {

                List<String> depls = sysUserService.getUserByuserId(Long.valueOf(userId));
                if (!depls.isEmpty()) {
                    String depId = depls.get(0);

                    if (CacheContext.wppromap.containsKey(depId)) {

                        List<ProBasicProject> pjls = CacheContext.wppromap.get(depId);
                        for (ProBasicProject pj : pjls) {
                            ProBasicProject newpj = new ProBasicProject();
                            BeanUtil.copyProperties(pj, newpj);
                            pjlist.add(newpj);
                        }
                    } else if (CacheContext.cpwpmap.containsKey(depId)) {
                        List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(depId);
                        for (ProBasicPowerstation wp : wpls) {
                            List<ProBasicProject> pjls = CacheContext.wppromap.get(wp.getId());
                            for (ProBasicProject pj : pjls) {
                                ProBasicProject newpj = new ProBasicProject();
                                BeanUtil.copyProperties(pj, newpj);
                                pjlist.add(newpj);
                            }
                        }

                    } else if (CacheContext.rgmap.containsKey(depId)) {
                        List<ProBasicCompany> rgcpls = CacheContext.rgcpmap.get(depId);
                        for (ProBasicCompany cp : rgcpls) {
                            List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(cp.getId());
                            for (ProBasicPowerstation wp : wpls) {
                                List<ProBasicProject> pjls = CacheContext.wppromap.get(wp.getId());
                                for (ProBasicProject pj : pjls) {
                                    ProBasicProject newpj = new ProBasicProject();
                                    BeanUtil.copyProperties(pj, newpj);
                                    pjlist.add(newpj);
                                }

                            }
                        }

                    } else if (depId.equals("0")) {
                        List<ProBasicPowerstation> wpls = CacheContext.wpls;
                        for (ProBasicPowerstation wp : wpls) {
                            List<ProBasicProject> pjls = CacheContext.wppromap.get(wp.getId());
                            for (ProBasicProject pj : pjls) {
                                ProBasicProject newpj = new ProBasicProject();
                                BeanUtil.copyProperties(pj, newpj);
                                pjlist.add(newpj);
                            }
                        }

                    }
                }
                sessionPjMap.put(token, pjlist);
            } else if (sessionPjMap.containsKey(token) && sessionPjMap.containsKey(token)) {
                pjlist = sessionPjMap.get(token);
            }

        } else {
            List<ProBasicProject> pjls = CacheContext.pjls;
            for (ProBasicProject wp : pjls) {
                ProBasicProject newWp = new ProBasicProject();
                BeanUtil.copyProperties(wp, newWp);
                pjlist.add(newWp);
            }
        }
        return pjlist;
    }
}
