package com.gyee.frame.service.app.bracelet;

import com.gyee.frame.model.auto.Shbraceletpoint;
import com.gyee.frame.model.enumeration.Messagetype;
import com.gyee.frame.netty.websocket.util.SocketToolService;
import com.gyee.frame.service.ShbraceletpointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class BraceletService extends SocketToolService {
    public static Map<String, List<String>> bracelet_pointMap = new HashMap<>();
    public static Map<String, Map<String, Map<String, List<Shbraceletpoint>>>> shbraceletpointMapMapMapList = new HashMap<>();
    /*ip,  Y1-A0BC,  Y1-A0BC_PLD,*/
    @Autowired
    private ShbraceletpointService shbraceletpointService;
    /*2020-05-01 12:13:11*/
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void braceletPushTask() throws Exception {
        Set set = bracelet_pointMap.keySet();
        Iterator<String> it = set.iterator();
        Map<String, Double> pointDataMap = new HashMap();
        while (it.hasNext()) {
            String ip = it.next();
            List<String> braceletDateList = bracelet_pointMap.get(ip);
            String beginDate = braceletDateList.get(0);
            String endDate = braceletDateList.get(1);

            Long beginDateTs = sdf.parse(beginDate).getTime();
            Long endDateTs = sdf.parse(endDate).getTime();

            List<Shbraceletpoint> shbraceletpointList = shbraceletpointService.shbraceletpointList(beginDateTs, endDateTs);
            /*Map<String,Map<String,List<Shbraceletpoint>>> deviceNameMap = new HashMap<>();
            Map<String, List<Shbraceletpoint>> deviceMetricsNameMap = new HashMap<>();

            *//*获取设备指标名称*//*
            List<String> deviceMetricsName = shbraceletpointList.stream().map(Shbraceletpoint::getKey).collect(Collectors.toList());

            for (int i = 0;i<deviceMetricsName.size();i++){
                List<Shbraceletpoint> list = new ArrayList<>();
                list.clear();
                String metriceName = deviceMetricsName.get(i);
                for (int j = 0;j<deviceMetricsName.size();j++) {
                    String key = shbraceletpointList.get(j).getKey();
                    if (metriceName.equals(key)) {
                        Shbraceletpoint shbraceletpoint = shbraceletpointList.get(j);
                        list.add(shbraceletpoint);
                        deviceMetricsNameMap.put(metriceName, list);
                    }
                }
            }

            Map<String,Map<String, List<Shbraceletpoint>>>  deviceMap = new HashMap<>();
            for(Shbraceletpoint shbraceletpoint1:shbraceletpointList){
                String deviceName = shbraceletpoint1.getKey().split("_")[0];
                deviceNameMap.put(deviceName,deviceMetricsNameMap);
                Map<String,List<Shbraceletpoint>> listMap = new HashMap<>();
                for(String key:deviceMetricsNameMap.keySet()){

                    listMap.put(key, deviceMetricsNameMap.get(key));

                    deviceNameMap.put(deviceName,listMap);
                }

            }*/
            Map<String, Map<String, List<Shbraceletpoint>>> deviceNameMap = injectionMap(shbraceletpointList);
            shbraceletpointMapMapMapList.put(ip,deviceNameMap);
        }

        this.pushToWeb("pageNumber_9", "functionNumber_1","all", shbraceletpointMapMapMapList, Messagetype.MESSAGE);
    }
    private  Map<String,Map<String,List<Shbraceletpoint>>> injectionMap(List<Shbraceletpoint> shbraceletpointList ){
        Map<String,Map<String,List<Shbraceletpoint>>> deviceNameMap = new HashMap<>();/*设备名*/
        Map<String, List<Shbraceletpoint>> deviceMetricsNameMap = new HashMap<>();/*设备指标*/

        /*获取设备指标名称*/
        List<String> deviceMetricsName = shbraceletpointList.stream().map(Shbraceletpoint::getKey).collect(Collectors.toList());

        for (int i = 0;i<deviceMetricsName.size();i++){
            List<Shbraceletpoint> list = new ArrayList<>();
            list.clear();
            String metriceName = deviceMetricsName.get(i);
            for (int j = 0;j<deviceMetricsName.size();j++) {
                String key = shbraceletpointList.get(j).getKey();
                if (metriceName.equals(key)) {
                    Shbraceletpoint shbraceletpoint = shbraceletpointList.get(j);
                    list.add(shbraceletpoint);
                    deviceMetricsNameMap.put(metriceName, list);
                }
            }
        }

        /*Map<String,Map<String, List<Shbraceletpoint>>>  deviceMap = new HashMap<>();*/
        for(Shbraceletpoint shbraceletpoint1:shbraceletpointList){
            String deviceName = shbraceletpoint1.getKey().split("_")[0];
            deviceNameMap.put(deviceName,deviceMetricsNameMap);
            Map<String,List<Shbraceletpoint>> listMap = new HashMap<>();
            for(String key:deviceMetricsNameMap.keySet()){

                listMap.put(key, deviceMetricsNameMap.get(key));

                deviceNameMap.put(deviceName,listMap);
            }

        }
        return deviceNameMap;
    }
}
