package jp.odakyu.toiletsignage.json;

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

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

/**
 * Created by agentec on 2017/12/08.
 */

public class ToiletDefaultJson {
    /**
     * JSON Param
     */
    private final String KEY_PRIORITY = "priority";
    private final String KEY_TOILET_ID = "toiletId";
    private final String KEY_TOILET_SEX = "toiletSex";
    private final String KEY_TOILET_NAME_EN = "toiletName_en";
    private final String KEY_TOILET_NAME_JP = "toiletName_jp";

    private int priority;
    private int toiletId;
    private int toiletSex;
    private String toiletName_en;
    private String toiletName_jp;

    public int getPriority() {
        return priority;
    }
    public int getToiletId() {
        return toiletId;
    }
    public int getToiletSex() {return toiletSex;}
    public String getToiletNameEN() {
        return toiletName_en;
    }
    public String getToiletNameJP() {
        return toiletName_jp;
    }


    public ToiletDefaultJson(JSONObject jsonObject) throws JSONException {
        priority = jsonObject.getInt(KEY_PRIORITY);
        toiletId = jsonObject.getInt(KEY_TOILET_ID);
        toiletSex = jsonObject.getInt(KEY_TOILET_SEX);

        if (jsonObject.has(KEY_TOILET_NAME_EN)) {
            toiletName_en = jsonObject.getString(KEY_TOILET_NAME_EN);
        }

        if (jsonObject.has(KEY_TOILET_NAME_JP)) {
            toiletName_jp = jsonObject.getString(KEY_TOILET_NAME_JP);
        }
    }

    public boolean validation(JSONObject jsonObject) {
        if (!jsonObject.has(KEY_PRIORITY)) {
            return FALSE;
        }

        if (!jsonObject.has(KEY_TOILET_ID)) {
            return FALSE;
        }

        if (!jsonObject.has(KEY_TOILET_SEX)) {
            return FALSE;
        }

        if (getToiletId() < 0 || getToiletId() > 1000000) {
            return FALSE;
        }

        if (getToiletSex() != 0 && getToiletSex() != 1) {
            return FALSE;
        }

        if (getToiletSex() != 0 && getToiletSex() != 1) {
            return FALSE;
        }

        if (getPriority() != 0 && getPriority() != 1 && getPriority() != 2) {
            return FALSE;
        }

        return TRUE;
    }
}
