package com.bonait.bnframework.HBL.DataUtil;


import com.alibaba.fastjson.TypeReference;
import com.bonait.bnframework.HBL.Result.OperateResultT;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

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 extends Number> OperateResultT<T> TryToNumber(Object value,Class<T> type){
//        if(value==null) return OperateResultT.CreateFailedT("TryToGenericity：转换对象为空");
//        try{
//            String temp = value.toString();
//            if(type==Integer.TYPE){
//                return OperateResultT.CreateSuccess((T)Integer.valueOf(temp));
//            }else if (type==Short.TYPE){
//                return OperateResultT.CreateSuccess((T)Short.valueOf(temp));
//            }else if (type==Byte.TYPE){
//                return OperateResultT.CreateSuccess((T)Byte.valueOf(temp));
//            }else if (type==Float.TYPE){
//                return OperateResultT.CreateSuccess((T)Float.valueOf(temp));
//            }else if (type==Long.TYPE){
//                return OperateResultT.CreateSuccess((T)Long.valueOf(temp));
//            }else if (type==Double.TYPE){
//                return OperateResultT.CreateSuccess((T)Double.valueOf(temp));
//            }else if (type.getName().contains(Boolean.TYPE.getName())){
//                return OperateResultT.CreateSuccess((T)Boolean.valueOf(temp));
//            }else if (type.getName().contains(String.class.getName())){
//                return OperateResultT.CreateSuccess((T)temp);
//            }
//            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("TryToBoolean：转换对象为空");
        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("TryToBoolean：转换对象为空");
        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);
        }
    }


}
