package kr.co.hanwha.hifive.network.hanwha;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.ReqMemberInfo;

/**
 * Created by sanghoonkwon on 2018. 1. 11..
 */

public class HanwhaJsonMaker {
    JSONObject jsonObject = null;

    private void checkNull() {
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }
    }

    public boolean addParam(String key, Object value) {
        checkNull();
        if (value instanceof String) {
            return addParam(key, (String) value);
        } else if (value instanceof Integer) {
            return addParam(key, (int) value);
        } else if (value instanceof Boolean) {
            return addParam(key, (boolean) value);
        } else if (value instanceof List) {
            return addParam(key, (List<Object>) value);
        } else {
            try {
                if (value != null) {
                    String json = new Gson().toJson(value);
                    JSONObject obj = new JSONObject(json);
                    jsonObject.put(key, obj);
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean addParam(String key, int value) {
        try {
            jsonObject.put(key, value);
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return false;
    }

    public boolean addParam(String key, String value) {
        try {
            jsonObject.put(key, value);
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return false;
    }

    public boolean addParam(String key, boolean value) {
        try {
            jsonObject.put(key, value);
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return false;
    }

    public boolean addParam(String key, List<Object> params) {
        JSONArray jsonArray = new JSONArray();
        for (Object param : params) {
            if (param instanceof ReqMemberInfo) {
                JSONObject obj = new JSONObject();
                try {
                    obj.put("member_id", ((ReqMemberInfo) param).getMember_id());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                jsonArray.put(obj);
            } else if (param instanceof CampaignData) {
                JSONObject obj = new JSONObject();
                try {
                    obj.put("campaign_id", ((CampaignData) param).getCampaign_id());
                    obj.put("project_id", ((CampaignData) param).getProject_id());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                jsonArray.put(obj);
            } else {
                jsonArray.put(param);
            }
        }

        try {
            jsonObject.put(key, jsonArray);
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public String toString() {
        return jsonObject != null ? jsonObject.toString() : "";
    }
}
