package com.example.onetap_ssh.ui.tabs;

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

public abstract class ListItem {
    private int type;

    public ListItem(int type) {
        this.type = type;
    }

    public JSONObject toJson() throws JSONException {
        JSONObject toJson = new JSONObject();
        toJson.put("type", type);
        JSONArray a = new JSONArray();
        for(String s : getStrings()) {
            a.put(s);
        }
        toJson.put("strings", a);
        return toJson;
    }


    abstract public String[] getStrings();

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public static ListItem fromJson(JSONObject object) throws JSONException {
        JSONArray Strings = object.getJSONArray("strings");
        switch (object.getInt("type")){
            case ListAdapter.SINGLEBUTTON:{
                return new ListItemSingle(Strings.getString(0), Strings.getString(1), Strings.getString(2));
            }
            case ListAdapter.DOUBLEBUTTON:{
                return new ListItemDouble(Strings.getString(0), Strings.getString(1), Strings.getString(2), Strings.getString(3), Strings.getString(4));
            }
            case ListAdapter.HEADLINE:{
                return new ListItemHeadline(Strings.getString(0));
            }
            case ListAdapter.SPACE:{
                return new ListItemSpace();
            }
            default: {
                throw new RuntimeException("UNGÜLTIGER OBJEKTTYP");
            }

        }

    }
}
