package com.gyee.wisdom.dao.redis;


import com.alibaba.fastjson.JSONObject;
import com.gyee.wisdom.common.utils.ByteUtil;
import com.gyee.wisdom.it235.util.RedisValUtil;
import com.gyee.wisdom.common.data.timeseries.*;
import com.gyee.wisdom.dao.timeseries.ILatestDao;
import com.gyee.wisdom.dao.timeseries.RedisDao;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


@Component
@RedisDao
@Slf4j
public class RedisLatestDao implements ILatestDao {

    @Autowired
    private RedisValUtil redisValUtil;


    public Map<String, TsData> getTsDataLatest(List<TsPoint> tsPoints) throws Exception {

        Map<String, TsData> resultTest = new HashMap<>();
        try {
            List<String> collect = tsPoints.stream().map(s -> s.getId()).collect(Collectors.toList());
            List<byte[]> bytesList = redisValUtil.getBytesList(0, collect);
            for (int i = 0; i < bytesList.size(); i++) {
                double val = ByteUtil.getDouble(bytesList.get(i), 0);
                long ts = ByteUtil.getLong2(bytesList.get(i), 8);
                if (collect.get(i).contains("DI")) {
                    Boolean bl = false;
                    if (val != 0)
                        bl = true;
                    BooleanTsData booleanTsData = new BooleanTsData(ts, (short) 0, bl);
                    resultTest.put(collect.get(i), booleanTsData);
                } else {
                    DoubleTsData doubleTsData = new DoubleTsData(ts, (short) 0, val);
                    resultTest.put(collect.get(i), doubleTsData);
                }
            }
        } catch (Exception ex) {

        }


//        Map<String, TsData> result = new HashMap<>();
//        for (TsPoint tsPoint:tsPoints){
//            try {
//                int index = Integer.parseInt(tsPoint.getTag());
//                if (index == 3) {
//                    //王博版redis读取
//                    byte[] dataBytes = redisValUtil.getBytes(index, tsPoint.getId());
//                    double val = ByteUtil.getDouble(dataBytes,0);
//                    long ts = ByteUtil.getLong2(dataBytes,8);
//                    DoubleTsData doubleTsData = new DoubleTsData(ts, (short) 0, val);
//                    result.put(tsPoint.getId(), doubleTsData);
//                }
////                String s = redisValUtil.get(index, tsPoint.getId()).toString();
////                if (s != null) {
////                    DoubleTsData doubleTsData = TsDataParser.parseWBString(s);
////                    if (doubleTsData != null)
////                        result.put(tsPoint.getId(), doubleTsData);
////                } else {
////                    log.error(tsPoint.getId() + ": 空值！");
////                }
//            }catch (Exception e){
//                log.error(e.getMessage());
//            }
//        }
        return resultTest;
    }

    public boolean setTsDataLatest(List<TsPoint> tsPoints) throws Exception {
        List<String> collect = tsPoints.stream().map(s -> s.getId()).collect(Collectors.toList());
        return redisValUtil.setBytesList(Integer.parseInt(tsPoints.get(0).getTag()), collect);
    }

    public int writeDoubleLatest(List<TsPointData> list) throws Exception {
        return 1;
    }

    @Override
    public int writeStringLatest(List<TsPointData> list) throws Exception {

        return 1;
    }

    @Override
    public int writeBooleanLatest(List<TsPointData> list) throws Exception {
        return 1;
    }

    @Override
    public int writeLongLatest(List<TsPointData> list) throws Exception {
        return 1;
    }

    @Override
    public int writeBlobLatest(List<TsPointData> list) throws Exception {
        return 1;
    }

    @Override
    public int writeCoordinateLatest(List<TsPointData> list) throws Exception {

        return 1;
    }

    @Override
    public boolean writeLatest(List<TsPointData> dataList) throws Exception {
        return true;
    }


}
