package cn.wise.sc.energy.power.plant.business.domain;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.spring4all.spring.boot.starter.hbase.api.RowMapper;
import org.apache.hadoop.hbase.client.Result;

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;

/**
 * @description:
 * @author: qh
 * @create: 2020-08-20 15:25
 **/
@Deprecated
public class OscillogramRowMapper implements RowMapper<Oscillogram> {

    private static byte[] RECORDS = "records".getBytes();
    private static byte[] MAP = "map".getBytes();
    private static byte[] DATA = "Data".getBytes();
    private static byte[] OFFSET = "KeyPhaseOffset".getBytes();
    private List<String> kks = new ArrayList<>();

    public List<String> getKks() {
        return kks;
    }

    @Override
    public Oscillogram mapRow(Result result, int rowNum) throws Exception {

        Oscillogram oscillogram = new Oscillogram();
        oscillogram.setRowKey(Base64Encoder.encode(result.getRow()));
        //解密当前kkscode
        String kkscode = getKKsCode(result.getRow());
        oscillogram.setKKsCode(kkscode);


        //获取特征信息
        NavigableMap<byte[], NavigableMap<byte[], byte[]>> noVersionMap = result.getNoVersionMap();
        NavigableMap<byte[], byte[]> navigableMap1 = noVersionMap.get(RECORDS);
        //获取特征数据
        byte[] noVersionDataByte = navigableMap1.get(DATA);
        String noVersionData = Base64Decoder.decodeStr(Base64Encoder.encode(noVersionDataByte));
        oscillogram.setNoVersionData(noVersionData);
        byte[] noVersionOffSetByte = navigableMap1.get(OFFSET);
        oscillogram.setNoVersionData(Base64Decoder.decodeStr(Base64Encoder.encode(noVersionOffSetByte)));
        //=============================================================================
        //获取值信息
        NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> dataMap = result.getMap();
        NavigableMap<byte[], NavigableMap<Long, byte[]>> navigableMapNavigableMap1 = dataMap.get(RECORDS);
        NavigableMap<Long, byte[]> longNavigableMapData = navigableMapNavigableMap1.get(DATA);
        Long firstKey = longNavigableMapData.firstKey();
        oscillogram.setStart(firstKey);
        byte[] mapDataByte = longNavigableMapData.get(firstKey);
        String mapData = Base64Decoder.decodeStr(Base64Encoder.encode(mapDataByte));
        oscillogram.setMapData(JSON.parseArray(mapData, Float.class));
        //获取偏移量
        NavigableMap<Long, byte[]> longNavigableMapOffset = navigableMapNavigableMap1.get(OFFSET);
        Long firstOffsetKey = longNavigableMapOffset.firstKey();
        byte[] offSetByte = longNavigableMapOffset.get(firstOffsetKey);
        oscillogram.setNoVersionKeyPhaseOffset(JSON.parseArray(Base64Decoder.decodeStr(Base64Encoder.encode(offSetByte)), Float.class));

        return oscillogram;

        //获取偏移量
//        Set<byte[]> keySet = noVersionMap.keySet();
//        for (byte[] keyByte : keySet) {
//            System.out.println(Base64Decoder.decodeStr(Base64Encoder.encode(keyByte)));
//            NavigableMap<byte[], byte[]> navigableMap = noVersionMap.get(keyByte);
//            for (byte[] keyByteIn : navigableMap.keySet()) {
//                System.out.println("-----------------------");
//                System.out.println(Base64Decoder.decodeStr(Base64Encoder.encode(keyByteIn)));
//            }
//        }
//
//        Set<byte[]> keySet2 = dataMap.keySet();
//        System.out.println("========================================");
//        System.out.println("========================================");
//        System.out.println("========================================");
//        System.out.println("========================================");
//        for (byte[] keyByte : keySet2) {
//            System.out.println(Base64Decoder.decodeStr(Base64Encoder.encode(keyByte)));
//            NavigableMap<byte[], NavigableMap<Long, byte[]>> navigableMapNavigableMap = dataMap.get(keyByte);
//            System.out.println("第二层");
//            for (byte[] keyByteIn : navigableMapNavigableMap.keySet()) {
//                System.out.println("-----------------------");
//                System.out.println(Base64Decoder.decodeStr(Base64Encoder.encode(keyByteIn)));
//                NavigableMap<Long, byte[]> longNavigableMap = navigableMapNavigableMap.get(keyByteIn);
//                System.out.println("第三层");
//                for (Long l : longNavigableMap.keySet()) {
//                    System.out.println("++++++++++++++++++++++++");
//                    System.out.println(l);
//                }
//            }
//        }
//
    }

    /**
     * 解密kkscode
     * <p>
     * rowkey:UNIX时间第10位+KKS+UNIX时间:0rm0000000000000000000002171589741900581
     *
     * @param row 带解密
     * @return str
     */
    private String getKKsCode(byte[] row) {

        if (row.length == 0) {
            throw new RuntimeException("获取Hbase数据失败,RowKey为空!");
        }
        String kksCodeEncode = Base64Decoder.decodeStr(Base64Encoder.encode(row));
        return StrUtil.swapCase(StrUtil.sub(kksCodeEncode, 1, kksCodeEncode.length() - 13));
    }

}
