package com.gyee.alarm.service;


import com.gyee.alarm.controller.feigns.IAlarmService;
import com.gyee.alarm.model.vo.AlarmSimpleVo;
import com.gyee.alarm.model.vo.AlarmSuperTalbeType;
import com.gyee.alarm.model.vo.AlarmTag;
import com.gyee.alarm.model.vo.AlarmTypeValue;
import com.gyee.alarm.service.auto.IAlarmTsService;
import com.gyee.alarm.task.thread.AlarmThread;
import com.gyee.alarm.util.StringUtils;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import com.gyee.common.model.PointData;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;


@Service
@Slf4j
public class AsyncService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @SneakyThrows
    @Async("taskExecutor")
    public CompletableFuture<String>  asyncService(
            List<AlarmTag> alarmVoList,Integer readRows,IEdosUtil edosUtil,Map<String, Integer> historymap,
    String alarmType, IAlarmService alarmService,int len) throws Exception {




//			while (true)
//			{
        List<String> ls = new ArrayList<>();
        List<PointData> values = new ArrayList<>();


        int times = 0;

        for (AlarmTag vo : alarmVoList) {

            ls.add(vo.getTagId());
            times++;
            if (times == readRows) {
                List<PointData> templs = edosUtil.getRealData(ls);
                values.addAll(templs);
                ls = new ArrayList<>();
                times=0;
            }
        }

        if (!ls.isEmpty()) {
            List<PointData> templs = edosUtil.getRealData(ls);
            values.addAll(templs);
        }


//				System.out.println(values.size()+"____"+ alarmVoList.size());
        int alarmNumber = 0;
        if (values.size() == alarmVoList.size()) {
            List<AlarmTag> saveAlarmTags = new ArrayList<>();

            List<AlarmTag> updateAlarmTags = new ArrayList<>();

            for (int i = 0; i < alarmVoList.size(); i++) {

                AlarmTag vo = alarmVoList.get(i);

//                    if(vo.getTagId().equals("FD-DJY-YuanDong.140623F0221MDQ02GS006DB0111SW09RAW00"))
//                    {
//                        System.out.println("");
//                    }

                PointData value = values.get(i);

//                    if (vo.getTagId().equals("FD-PYZK-Taizhong1.140728F0111MDY01GS002DB0111SW01RAW00")) {
//                        System.out.println("");
//                    }
                if (vo.updateAndCheck(value)) {
                    if (vo.getVal() == 1) {
                        AlarmTag po = new AlarmTag();

                        po.setId(vo.getId());
                        po.setTs(vo.getTs());
//                        po.setEndts(po.getEndts());
                        po.setVal(vo.getVal());
                        po.setOval(vo.getOval());
                        po.setTriggerType(vo.getTriggerType());
                        po.setTagId(vo.getTagId());
                        po.setRank(vo.getRank());
                        po.setTimeLong(0.0);
                        saveAlarmTags.add(po);
                        alarmNumber++;

                        historymap.put(vo.getId().toLowerCase(),0);


                    } else if (vo.getVal() == 2) {

                        if (historymap.containsKey(vo.getId().toLowerCase())) {
                            //有结束时间，创建新的报警
                            if (historymap.get(vo.getId().toLowerCase()) == 0) {
                                //没有有结束时间，结束报警
                                AlarmTag po = new AlarmTag();

                                po.setId(vo.getId());
                                po.setTs(vo.getTs());
                                po.setEndts(vo.getEndts());
                                po.setVal(vo.getVal());
                                po.setOval(vo.getOval());
                                po.setTriggerType(vo.getTriggerType());
                                po.setTagId(vo.getTagId());
                                po.setRank(vo.getRank());
                                po.setTimeLong(0.0);
                                updateAlarmTags.add(po);
                                historymap.put(vo.getId().toLowerCase(),1);
                            }
                        }

                    } else {
                        AlarmTag po = new AlarmTag();

                        po.setId(vo.getId());
                        po.setTs(vo.getTs());
                        po.setEndts(vo.getEndts());
                        po.setVal(vo.getVal());
                        po.setOval(vo.getOval());
                        po.setTriggerType(vo.getTriggerType());
                        po.setTagId(vo.getTagId());
                        po.setRank(vo.getRank());
                        po.setTimeLong(0.0);
                        updateAlarmTags.add(po);
                        alarmNumber++;
                        historymap.put(vo.getId().toLowerCase(),1);
                    }
                }
//                else  if (!historymap.containsKey(vo.getId().toLowerCase())) {
//                    if (vo.updateAndCheckNoInfo(value)) {
//                        if (vo.getVal() == 1) {
//                            AlarmTag po = new AlarmTag();
//
//                            po.setId(vo.getId());
//                            po.setTs(vo.getTs());
//                            po.setEndts(po.getEndts());
//                            po.setVal(vo.getVal());
//                            po.setOval(vo.getOval());
//                            po.setTriggerType(vo.getTriggerType());
//                            po.setTagId(vo.getTagId());
//                            po.setRank(vo.getRank());
//                            po.setTimeLong(0.0);
//                            saveAlarmTags.add(po);
//                            alarmNumber++;
//                            historymap.put(vo.getId().toLowerCase(),0);
//                        }
//                    }
//                }
            }

            switch (alarmType) {
                case "in_alarms":
                    if (!saveAlarmTags.isEmpty()) {
                        alarmService.saveInTags(saveAlarmTags);
                    }
                    if (!updateAlarmTags.isEmpty()) {
                        alarmService.updateInTags(updateAlarmTags);
                    }
                    break;
                case "wt_alarms":
                    if (!saveAlarmTags.isEmpty()) {
                        alarmService.saveWtTags(saveAlarmTags);
                    }
                    if (!updateAlarmTags.isEmpty()) {
                        alarmService.updateWtTags(updateAlarmTags);
                    }
                    break;
                case "bt_alarms":
                    if (!saveAlarmTags.isEmpty()) {

//                        for(AlarmTag tag:saveAlarmTags)
//                        {
//                            logger.info("tagId_"+tag.getTagId()+"_wpId_"+tag.getWpId()+"_ts_"+tag.getTs()+"_val_"+tag.getVal());
//                        }

                        alarmService.saveBtTags(saveAlarmTags);
                    }
                    if (!updateAlarmTags.isEmpty()) {
//                        for(AlarmTag tag:updateAlarmTags)
//                        {
//                            logger.info("tagId_"+tag.getTagId()+"_wpId_"+tag.getWpId()+"_ts_"+tag.getTs()+"_val_"+tag.getVal());
//                        }
                        alarmService.updateBtTags(updateAlarmTags);
                    }

                    break;

            }


            logger.info(len+ "开始报警数量：" + saveAlarmTags.size());
            logger.info(len+"结束报警数量：" + updateAlarmTags.size());
        }

        Thread.sleep(1000);


        return CompletableFuture.completedFuture(len+"Task completed");
    }


}
