package jp.agentec.abook.abv.bl.acms.client.json;

import org.json.adf.JSONObject;

import jp.agentec.abook.abv.bl.common.exception.AcmsException;
import jp.agentec.abook.abv.bl.common.exception.JSONValidationException;

/**
 * checkapi/shopSyncWatermark/ のレスポンスを変換するクラス
 */
public class ShopSyncWatermarkJSON extends AcmsCommonJSON {
    public static final String NeedsFullSync = "needsFullSync";
    public static final String LastChangedAt = "lastChangedAt";

    public boolean needsFullSync;
    public String lastChangedAt;

    public ShopSyncWatermarkJSON(String jsonString) throws AcmsException {
        super(jsonString);
    }

    @Override
    protected void parse(JSONObject json) throws JSONValidationException {
        super.parse(json);
        // default: fail-safe true (field may be absent)
        needsFullSync = true;

        if (json.has(NeedsFullSync)) {
            Object value = json.get(NeedsFullSync);
            if (value != null) {
                String lowerValue = String.valueOf(value).trim().toLowerCase();
                if ("true".equals(lowerValue) || "1".equals(lowerValue)) {
                    needsFullSync = true;
                } else if ("false".equals(lowerValue) || "0".equals(lowerValue)) {
                    needsFullSync = false;
                }
            }
        }
        lastChangedAt = getStringOrNull(json, LastChangedAt);
    }
}
