package org.djodjo.json.schema;


import org.djodjo.json.JsonElement;
import org.djodjo.json.JsonObject;
import org.djodjo.json.Validator;
import org.djodjo.json.exception.JsonException;
import org.djodjo.json.schema.validation.SchemaV5Validator;

public class SchemaV5 extends Schema {

    public SchemaV5() {
        super();
        setSchema("http://json-schema.org/draft-05/schema#");
    }

    @Override
    public Schema getEmptySchema() {
        return new SchemaV5().setSchemaFetcher(schemaFetcher);
    }

    @Override
    public Validator getDefaultValidator() {
        return new SchemaV5Validator(this);
    }


    public String getTitle(String locale) {
        String res = "";

        if (getJson().opt("title").isString()) {
            res = getJson().optString("title") ;
        } else {
            JsonObject titles = getJson().optJsonObject("title");
            if(titles!=null && titles.length()>0) {
                res = titles.optString(locale);
            } else {
                res = null;
            }
        }

        return res;
    }

    @Override
    public String getTitle() {
        String res = null;

        if(getJson().opt("title").isString()) {
            res = getJson().optString("title") ;
        } else {
            JsonObject titles = getJson().optJsonObject("title");
            if(titles!=null && titles.length()>0) {
                res = titles.valuesSet().toArray(new String[0])[0];
            } else {
                res = null;
            }
        }


        return res;
    }

    public JsonObject getTitles() {
        JsonObject res = new JsonObject();

        if(getJson().opt("title").isString()) {
            try {
                res.put("default", getJson().optString("title"));
            } catch (JsonException e) {
                e.printStackTrace();
            }
        } else {
            res = getJson().optJsonObject("title");
        }

        return res;
    }

    //replaces title if not object
    public SchemaV5 addTitle(String title, String locale) {
        JsonElement tit = getJson().opt("title");
        if(tit!=null && !tit.isJsonObject()) {
            getJson().remove("title");
            try {
                getJson().put("title", new JsonObject());
            } catch (JsonException e) {
                e.printStackTrace();
            }
        }  else if(tit==null) {
            try {
                getJson().put("title", new JsonObject());
            } catch (JsonException e) {
                e.printStackTrace();
            }
        }


        try {
            getJson().optJsonObject("title").put(locale, title);
        } catch (JsonException e) {
            e.printStackTrace();
        }
        return this;
    }
}

@@ -2753,11 +2753,2258 @@
 s;%0A    %7D
+%0A%0A    //replaces title if not object%0A    public SchemaV5 addDescription(String title, String locale) %7B%0A        JsonElement tit = getJson().opt(%22description%22);%0A        if(tit!=null && !tit.isJsonObject()) %7B%0A            getJson().remove(%22description%22);%0A            try %7B%0A                getJson().put(%22description%22, new JsonObject());%0A            %7D catch (JsonException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D  else if(tit==null) %7B%0A            try %7B%0A                getJson().put(%22description%22, new JsonObject());%0A            %7D catch (JsonException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A%0A%0A        try %7B%0A            getJson().optJsonObject(%22description%22).put(locale, title);%0A        %7D catch (JsonException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return this;%0A    %7D%0A%0A%0A%0A    public String getDescription(String locale) %7B%0A        String res = %22%22;%0A%0A        if (getJson().opt(%22description%22).isString()) %7B%0A            res = getJson().optString(%22description%22) ;%0A        %7D else %7B%0A            JsonObject titles = getJson().optJsonObject(%22description%22);%0A            if(titles!=null && titles.length()%3E0) %7B%0A                res = titles.optString(locale);%0A            %7D else %7B%0A                res = null;%0A            %7D%0A        %7D%0A%0A        return res;%0A    %7D%0A%0A    @Override%0A    public String getDescription() %7B%0A        String res = null;%0A%0A        if(getJson().opt(%22description%22).isString()) %7B%0A            res = getJson().optString(%22description%22) ;%0A        %7D else %7B%0A            JsonObject titles = getJson().optJsonObject(%22description%22);%0A            if(titles!=null && titles.length()%3E0) %7B%0A                res = titles.valuesSet().toArray(new String%5B0%5D)%5B0%5D;%0A            %7D else %7B%0A                res = null;%0A            %7D%0A        %7D%0A%0A%0A        return res;%0A    %7D%0A%0A    public JsonObject getDescriptions() %7B%0A        JsonObject res = new JsonObject();%0A%0A        if(getJson().opt(%22description%22).isString()) %7B%0A            try %7B%0A                res.put(%22default%22, getJson().optString(%22description%22));%0A            %7D catch (JsonException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D else %7B%0A            res = getJson().optJsonObject(%22description%22);%0A        %7D%0A%0A        return res;%0A    %7D%0A%0A%0A%0A%0A
 %0A%7D%0A

add multiple description languages support