package Gson;

import Database.CityDetail;
import Database.FisData;
import com.google.gson.JsonObject;

import java.time.LocalDateTime;

public class JsonHelper {
    public static String GetStringValue(JsonObject obj, String key, String defaultValue){
        if (obj!=null){
            if (obj.has(key)){
                String value = obj.get(key).getAsString();
                if (value!=null){
                    return value;
                }
            }
        }
        return defaultValue;
    }

    public static Character GetCharacterValue(JsonObject obj, String key, Character defaultValue){
        if (obj!=null){
            if (obj.has(key)){
                String value = obj.get(key).getAsString();
                if (value!=null && !value.isEmpty()){
                    return value.charAt(0);
                }
            }
        }
        return defaultValue;
    }

    public static LocalDateTime GetLocalDateTimeValue(JsonObject obj, String key, LocalDateTime defaultValue){
        if (obj!=null){
            if (obj.has(key)){
                String value = obj.get(key).getAsString();
                if (value!=null && !value.isEmpty()){
                    try{
                        return LocalDateTime.parse(value, FisData.dtf);
                    } catch (Exception ignored){
                    }
                }
            }
        }
        return defaultValue;
    }

    public static int GetIntValue(JsonObject obj, String key, int defaultValue){
        if (obj!=null){
            if (obj.has(key)){
                return obj.get(key).getAsInt();
            }
        }
        return defaultValue;
    }

    public static double GetDoubleValue(JsonObject obj, String key, double defaultValue){
        if (obj!=null){
            if (obj.has(key)){
                return obj.get(key).getAsDouble();
            }
        }
        return defaultValue;
    }

    public static CityDetail[] GetCityDetailValues(JsonObject obj, String key, CityDetail[] defaultValue){
        if (obj!=null){
            if (obj.has(key)){
                String vv = obj.get(key).getAsString();
                if (vv!=null && !vv.isEmpty()){
                    try{
                        CityDetail[] cd = GsonFormatter.fromJson(vv, CityDetail[].class);
                        if (cd!=null){
                            return cd;
                        }
                    } catch (Exception ignored){
                    }
                }
            }
        }
        return defaultValue;
    }

    public static CityDetail GetCityDetailValue(JsonObject obj, String key, CityDetail defaultValue){
        if (obj!=null){
            if (obj.has(key)){
                String vv = obj.get(key).getAsString();
                if (vv!=null && !vv.isEmpty()){
                    try{
                        CityDetail cd = GsonFormatter.fromJson(vv, CityDetail.class);
                        if (cd!=null){
                            return cd;
                        }
                    } catch (Exception ignored){
                    }

                }
            }
        }
        return defaultValue;
    }

    public static byte[] JsonObjectToBytes(JsonObject obj){
        return obj!=null ? obj.toString().getBytes() : null;
    }


}
