package com.bpa.scalage.HBL.DataUtil;


import com.bpa.scalage.HBL.Result.OperateResultT;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Arrays;
import java.util.List;

public class Convert {
    public static OperateResultT<Boolean> TryToBoolean(Object value){
        if(value==null) return OperateResultT.CreateFailedT("TryToBoolean 解析对象为空");
        try{
            return OperateResultT.CreateSuccess(Boolean.parseBoolean(value.toString()));
        }catch (Exception e) {
            return OperateResultT.CreateFailedT(e);
        }
    }

    public static OperateResultT<Integer> TryToInt(Object value){
        if(value==null) return OperateResultT.CreateFailedT("TryToInt 解析对象为空");
        try{
            return OperateResultT.CreateSuccess(Integer.parseInt(value.toString()));
        }catch (Exception e) {
            return OperateResultT.CreateFailedT(e);
        }
    }

    public static OperateResultT<Short> TryToShort(Object value){
        if(value==null) return OperateResultT.CreateFailedT("TryToShort 解析对象为空");
        try{
            return OperateResultT.CreateSuccess(Short.parseShort(value.toString()));
        }catch (Exception e) {
            return OperateResultT.CreateFailedT(e);
        }
    }

    public static OperateResultT<Float> TryToFloat(Object value){
        if(value==null) return OperateResultT.CreateFailedT("TryToFloat 解析对象为空");
        try{
            return OperateResultT.CreateSuccess(Float.parseFloat(value.toString()));
        }catch (Exception e) {
            return OperateResultT.CreateFailedT(e);
        }
    }

    public static <T> OperateResultT<T> TryToGenericity( Object value){
        if(value==null) return OperateResultT.CreateFailedT("TryToGenericity 解析对象为空");
        try{
            return OperateResultT.CreateSuccess((T)value);
        }catch (Exception e) {
            return OperateResultT.CreateFailedT(e);
        }
    }

    public static <T> OperateResultT<T> fromJson(Object value, Class<T> clazz) {
        if(value==null) return OperateResultT.CreateFailedT("TryToGenericity 解析对象为空");
        try{
            Gson gson = new GsonBuilder().create();
            String temp = gson.toJson(value);
            T arr = gson.fromJson(temp, clazz);
            if(arr!=null)return OperateResultT.CreateSuccess( arr );
            else  return OperateResultT.CreateFailedT("返回对象为空");
        }catch (Exception e) {
            return OperateResultT.CreateFailedT(e);
        }
    }

    public static <T> OperateResultT<T> fromJson(String value, Class<T> clazz) {
        if(value==null||value.isEmpty()) return OperateResultT.CreateFailedT("TryToGenericity 解析对象为空");
        try{
            Gson gson = new GsonBuilder().create();
            T arr = gson.fromJson(value, clazz);
            if(arr!=null)return OperateResultT.CreateSuccess( arr);
            else  return OperateResultT.CreateFailedT("返回对象为空");
        }catch (Exception e) {
            return OperateResultT.CreateFailedT(e);
        }
    }

//    public static <T> OperateResultT<List<T>> fromJson(Object value, Class<T[]> clazz) {
//        if(value==null) return OperateResultT.CreateFailedT("TryToGenericity 解析对象为空");
//        try{
//            Gson gson = new GsonBuilder().create();
//            String temp = gson.toJson(value);
//            T[] arr = gson.fromJson(temp, clazz);
//            if(arr!=null)return OperateResultT.CreateSuccess( Arrays.asList(arr) );
//            else  return OperateResultT.CreateFailedT("返回对象为空");
//        }catch (Exception e) {
//            return OperateResultT.CreateFailedT(e);
//        }
//    }



}
