package com.gyee.frame.service.monitor;


import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.MatrixVo;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.enumeration.Messagetype;
import com.gyee.frame.netty.websocket.WebSocketServer;
import com.gyee.frame.netty.websocket.util.SocketToolService;
import com.gyee.frame.service.ProjectService;
import com.gyee.frame.service.WindpowerstationService;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

@Service
public class MonitorService extends SocketToolService {

    @Resource
    private WindpowerstationService windpowerstationService;
    @Resource
    private ProjectService projectService;

    private int decimal_digit = 2;
    private IRealTimeDataBaseUtil mongodb = RealTimeDataBaseFactory.createRealTimeDataBase();
    public static int initialstate=0;


    public void monitor_target_matrixMonitor() throws Exception {


        Map<String, Map<String, WindTurbineTestingPointAi2>> wtpAimap = InitialRunner.wtpAimap;
        String pageNumber = "pageNumber_4";
        String functionNumber = "functionNumber_2";
        StringBuilder sb = new StringBuilder();
        sb.append(pageNumber).append("_").append(functionNumber);

        String jobkey = String.valueOf(sb);
        Map<String, CopyOnWriteArraySet<String>> keyidMap = WebSocketServer.keyidMap;

        Map<String, CopyOnWriteArraySet<String>> tempmap=new HashMap<>();

        if(initialstate==0)
        {

            List<Windpowerstation> wpls= InitialRunner.wpls;
            List<Project> pjls=InitialRunner.pjls;

            CopyOnWriteArraySet<String> set=new CopyOnWriteArraySet<String>();
            for(Windpowerstation wp: wpls)
            {
                StringBuilder sb2 = new StringBuilder();
                sb2.append("wp_").append(wp.getId());
                set.add(String.valueOf(sb2));
            }

            for(Project pj: pjls)
            {
                StringBuilder sb2 = new StringBuilder();
                sb2.append("pj_").append(pj.getId());
                set.add(String.valueOf(sb2));
            }
            tempmap.put(jobkey,set);
            keyidMap =tempmap;
        }else {
            keyidMap = WebSocketServer.keyidMap;
        }

//        CopyOnWriteArraySet<String> set=new CopyOnWriteArraySet<String>();
//        set.add("wp_CL_FDC");
//        set.add("wp_DX_FDC");
//        set.add("wp_KB_FDC");
//
//        set.add("pj_CL01_GC");
//        set.add("pj_CL02_GC");
//        set.add("pj_DX01_GC");
//        set.add("pj_KB01_GC");
//        set.add("pj_KB02_GC");
//        set.add("pj_KB03_GC");
//        set.add("pj_KB04_GC");
//
//        keyidMap.put(jobkey,set);

        if(keyidMap.containsKey(jobkey))
        {
            CopyOnWriteArraySet<String> set=keyidMap.get(jobkey);

            for (String str : set) {

                List<MatrixVo> vos = new ArrayList<>();

                if (str.equals("all")) {
                    continue;
                }
                String keyid_title = str.substring(0, 3);
                String keyid = str.substring(3);

                if (keyid_title.equals("wp_")) {

                    if (InitialRunner.wp_wtmap.containsKey(keyid)) {
                        List<Windturbine> ls = InitialRunner.wp_wtmap.get(keyid);
                        if (null != ls && !ls.isEmpty()) {

                            setMatrixValue(wtpAimap, vos, ls);

                        }
                    }

                } else if (keyid_title.equals("pj_")) {

                    if (InitialRunner.pj_wtmap.containsKey(keyid)) {
                        List<Windturbine> ls = InitialRunner.pj_wtmap.get(keyid);
                        if (null != ls && !ls.isEmpty()) {
                            setMatrixValue(wtpAimap, vos, ls);
                        }
                    }

                }


                this.pushToWeb("pageNumber_4", "functionNumber_2", str, vos, Messagetype.MESSAGE);

            }
            initialstate=1;
        }


    }

    private void setMatrixValue(Map<String, Map<String, WindTurbineTestingPointAi2>> wtpAimap, List<MatrixVo> vos, List<Windturbine> ls) throws Exception {
        List<String> fjztls=new ArrayList<>();
        List<String> glls=new ArrayList<>();
        List<String> fsls=new ArrayList<>();
        for (Windturbine wt : ls) {

            if(wtpAimap.containsKey(wt.getId()))
            {
                fjztls.add(wtpAimap.get(wt.getId()).get(Constant.FJZT).getId());
                glls.add(wtpAimap.get(wt.getId()).get(Constant.TPOINT_WT_FJGL).getId());
                fsls.add(wtpAimap.get(wt.getId()).get(Constant.TPOINT_WT_FJSSFS).getId());
            }

        }
        List<PointData> fjztvaluels=mongodb.getRealData(fjztls);
        List<PointData> glvaluels=mongodb.getRealData(glls);
        List<PointData> fsvaluels=mongodb.getRealData(fsls);

        if(null !=fjztvaluels && null !=glvaluels && null !=fsvaluels && glvaluels.size()==fjztvaluels.size() && fsvaluels.size()==ls.size() && fjztvaluels.size()==ls.size())
        {
            for ( int i=0;i<ls.size();i++) {

                Windturbine wt=ls.get(i);
                MatrixVo vo = new MatrixVo();
                StringBuilder sbs = new StringBuilder();
                vo.setId(wt.getId());
                String temp=wt.getId().substring(wt.getId().length()-3);
                if(StringUtils.isNumeric(temp))
                {
                    vo.setIdnum(temp);
                }else
                {
                    temp=wt.getId().substring(wt.getId().length()-2);
                    vo.setIdnum(temp);
                }
                //风机状态
                PointData fjzt_real =fjztvaluels.get(i);
                sbs.append("00");
                int fjzt = new BigDecimal(fjzt_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).intValue();
                vo.setIcon(String.valueOf(sbs.append(fjzt)));
                //功率
                PointData power_real = glvaluels.get(i);
                double power = new BigDecimal(power_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
                vo.setPower(String.valueOf(power));

                //风速
                PointData speed_real =fsvaluels.get(i);
                double speed = new BigDecimal(speed_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
                vo.setSpeed(String.valueOf(speed));
                vos.add(vo);
            }
        }
    }
}