package com.gyee.frame.common.dao.golden.timeseries;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gyee.frame.model.custom.DNAStatVal;
import com.gyee.frame.model.custom.DNAVal;
import com.gyee.frame.model.custom.PointData;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * @descrition:JSONObject转实体辅助类
 * @author:Wanghs
 * @date:2018-05-10
 */
public class JsonObjectHelper {
    public static List<PointData> phrasePointData(JSONArray jsonArray, String tagName) {
        List<PointData> result = new ArrayList<>();

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject tsData = jsonArray.getJSONObject(i);

            Long ts = tsData.getLong("ts");
            String pointValue = null;
            if (tsData.containsKey("doubleValue")) {
                pointValue = tsData.getString("doubleValue");
            } else if (tsData.containsKey("booleanValue")) {
                pointValue = tsData.getString("booleanValue");
            } else if (tsData.containsKey("longValue")) {
                pointValue = tsData.getString("longValue");
            } else if (tsData.containsKey("stringValue")) {
                pointValue = tsData.getString("stringValue");
            }
            PointData pointData = new PointData();
            pointData.setEdnaId(tagName);
            pointData.setPointName("1");
            pointData.setPointTime(ts / 1000);
            pointData.setPointValue(pointValue);
            pointData.setPointValueInDouble(Double.parseDouble(pointValue));
            ////System.out.println("key= " + tagName + ", ts = " + ts + ", value = " + pointValue);
            result.add(pointData);
        }
        return result;
    }
//
//    public static List<PointData> phrasePointData(JSONArray jsonArray,String...tagName) {
//        List<PointData> result = new ArrayList<>();
//
//        for (int i = 0; i < jsonArray.size(); i++) {
//            JSONObject jsonData = jsonArray.getJSONObject(i);
//            JSONObject tsData = jsonData.getJSONObject("tsData");
//            Long ts = tsData.getLong("ts");
//            String pointValue = null;
//            if (tsData.containsKey("doubleValue")) {
//                pointValue = tsData.getString("doubleValue");
//            } else if (tsData.containsKey("booleanValue")) {
//                pointValue = tsData.getString("booleanValue");
//            } else if (tsData.containsKey("longValue")) {
//                pointValue = tsData.getString("longValue");
//            } else if (jsonData.containsKey("stringValue")) {
//                pointValue = tsData.getString("stringValue");
//            }
//            PointData pointData = new PointData();
//            pointData.setPointName("1");
//            pointData.setEdnaId(tagName[i]);
//            pointData.setPointTime(ts / 1000);
//            pointData.setPointValue(pointValue);
//            pointData.setPointValueInDouble(Double.parseDouble(pointValue));
//         //   //System.out.println("key= " + tagName + ", ts = " + ts + ", value = " + pointValue);
//            result.add(pointData);
//        }
//        return result;
//    }

    public static List<PointData> phrasePointData(JSONObject jsonObject) {
        List<PointData> result = new ArrayList<>();
        Iterator<String> sIterator = jsonObject.keySet().iterator();
        while (sIterator.hasNext()) {
            // 获得key
            String key = sIterator.next();
            // 根据key获得value, value也可以是JSONObject,JSONArray,使用对应的参数接收即可
            JSONObject jsonData = jsonObject.getJSONObject(key);
            Long ts = jsonData.getLong("ts");
            String pointValue = null;
            if (jsonData.containsKey("doubleValue")) {
                pointValue = jsonData.getString("doubleValue");
            } else if (jsonData.containsKey("booleanValue")) {
                pointValue = jsonData.getString("booleanValue");
            } else if (jsonData.containsKey("longValue")) {
                pointValue = jsonData.getString("longValue");
            } else if (jsonData.containsKey("stringValue")) {
                pointValue = jsonData.getString("stringValue");
            }

            PointData pointData = new PointData();
            pointData.setEdnaId(key);
            pointData.setPointName("1");
            pointData.setPointValue(pointValue);
            pointData.setPointTime(ts/1000);
            if(pointValue.equals("false")  )
            {
            	pointData.setPointValueInDouble(0.0);
            }else  if(pointValue.equals("true") )
            {
            	pointData.setPointValueInDouble(1.0);
            }else
            {
                pointData.setPointValueInDouble(Double.parseDouble(pointValue));
            }

         //  //System.out.println("key= " + key + ", ts = " + ts + ", value = " + pointValue);
            result.add(pointData);
        }
        return result;
    }
    /**
     * @param arry    DoubleStatData集合
     * @param tagName 标签点名
     * @param type    0、最大值。1、最小值。2、平均值
     */
    public static List<PointData> GeneralTsDataToPointDataByStat(JSONArray arry, String tagName, int type) {
        List<PointData> result = new ArrayList<>();
        for (int i = 0; i < arry.size(); i++) {
            JSONObject jsonData = arry.getJSONObject(i);
            PointData data = new PointData();
            data.setEdnaId(tagName);
            data.setPointName("1");
            JSONObject avgData = null;
            JSONObject maxData = null;
            JSONObject minData = null;
            if (jsonData.containsKey("avg")) {
                avgData = jsonData.getJSONObject("avg");
            }
            if (jsonData.containsKey("max")) {
                maxData = jsonData.getJSONObject("max");
            }
            if (jsonData.containsKey("min")) {
                minData = jsonData.getJSONObject("min");
            }
            switch (type) {
                case 0:
                    data.setPointValue(String.valueOf(maxData.getString("doubleValue")));
                    data.setPointValueInDouble(Double.valueOf(maxData.getString("doubleValue")));
                    data.setPointTime(Long.valueOf(maxData.getString("ts")) / 1000);
                    break;
                case 1:
                    data.setPointValue(String.valueOf(minData.getString("doubleValue")));
                    data.setPointValueInDouble(Double.valueOf(minData.getString("doubleValue")));
                    data.setPointTime(Long.valueOf(minData.getString("ts")) / 1000);
                    break;
                case 2:
                    data.setPointValue(String.valueOf(avgData.getString("doubleValue")));
                    data.setPointValueInDouble(Double.valueOf(avgData.getString("doubleValue")));
                    data.setPointTime(Long.valueOf(avgData.getString("ts")) / 1000);
                    break;
                default:
                    break;
            }
            result.add(data);
        }
        return result;
    }

    public static DNAVal[] phraseDNAVal(JSONObject jsonObject, String[] tagName) {
        DNAVal[] arr = new DNAVal[jsonObject.size()];
        List<DNAVal> list = new ArrayList<>();
        Iterator<String> sIterator = jsonObject.keySet().iterator();

        for (int i = 0; i < tagName.length; i++) {
            // 获得key
            String key = tagName[i];
            // 根据key获得value, value也可以是JSONObject,JSONArray,使用对应的参数接收即可
            JSONObject jsonData = jsonObject.getJSONObject(key);
            DNAVal data = new DNAVal();
            data.Time = Integer.parseInt(String.valueOf(Long.valueOf(jsonData.getString("ts")) / 1000));
            data.Status = (short) jsonData.getShortValue("status");
            data.DValue = (Double.valueOf(jsonData.getDoubleValue("doubleValue")));
            list.add(data);
        }

        return list.toArray(arr);
    }

    public static DNAStatVal[] phraseDNAVal(JSONArray jsonArray) {

        DNAStatVal[] arr = new DNAStatVal[jsonArray.size()];
        List<DNAStatVal> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonData = jsonArray.getJSONObject(i);
            JSONObject avgData = null;
            JSONObject maxData = null;
            JSONObject minData = null;
            if (jsonData.containsKey("avg")) {
                avgData = jsonData.getJSONObject("avg");
            }
            if (jsonData.containsKey("max")) {
                maxData = jsonData.getJSONObject("max");
            }
            if (jsonData.containsKey("min")) {
                minData = jsonData.getJSONObject("min");
            }
            DNAStatVal statVal = new DNAStatVal();

            DNAVal avgVal = new DNAVal();
            DNAVal maxVal = new DNAVal();
            DNAVal minVal = new DNAVal();

            avgVal.Time = Integer.valueOf(String.valueOf(Long.valueOf(avgData.getString("ts")) / 1000));
            avgVal.DValue = Double.valueOf(avgData.getString("doubleValue"));

            maxVal.Time =  Integer.valueOf(String.valueOf(Long.valueOf(maxData.getString("ts")) / 1000));
            maxVal.DValue = Double.valueOf(maxData.getString("doubleValue"));

            minVal.Time =  Integer.valueOf(String.valueOf(Long.valueOf(minData.getString("ts")) / 1000));
            minVal.DValue = Double.valueOf(minData.getString("doubleValue"));

            statVal.max = maxVal;
            statVal.min = minVal;
            statVal.avg = avgVal;

            list.add(statVal);
        }

        return list.toArray(arr);
    }

}
