package com.gyee.wisdom.dataadapter.domain.topic;

import com.gyee.wisdom.common.data.timeseries.BasicTsPoint;
import com.gyee.wisdom.common.data.timeseries.TsDataType;
import com.gyee.wisdom.common.data.timeseries.TsPoint;
import com.gyee.wisdom.common.data.topic.Topic;

import com.gyee.wisdom.common.data.topic.TopicPoint;
import com.gyee.wisdom.dao.topic.ITopicDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
@Slf4j
public class TopicService {

    private HashMap<String, List<TsPoint>> topicPointsMap = new HashMap<>();

    @Autowired
    private ITopicDao topicDao;

    public List<Topic> getAll() {
        return topicDao.getAll();
    }

    public List<TsPoint> getTsPointListByTopic(String topic) {

        ArrayList<TsPoint> tsPoints = new ArrayList<>();

        if (topicPointsMap.containsKey(topic) == false) {
            //todo: 加载topic关联的TsPoint
            Topic topicData = topicDao.getByTopic(topic);

            if (topicData != null) {
                System.out.println(topicData.getId());
                List<TopicPoint> pointList = topicData.getPointList();
                pointList.forEach((k) -> {
                    TsDataType tsDataType = TsDataType.DOUBLE;
                    if (k.getDataType().equals("bool"))
                        tsDataType = TsDataType.BOOLEAN;
                    tsPoints.add(new BasicTsPoint(k.getId(), tsDataType));
                });

            }
        }
        return tsPoints;

    }

    public List<TsPoint> createTsPointsList() {
        ArrayList<TsPoint> tsPoints = new ArrayList<>();
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0001", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0002", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0003", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0004", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0005", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0006", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0007", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0008", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0009", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0010", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0011", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0012", TsDataType.DOUBLE));
        tsPoints.add(new BasicTsPoint("XSFJ.NX_GD_XSF_FJ_P1_L1_001_AI0013", TsDataType.DOUBLE));

        return tsPoints;
    }
}
