import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Folder extends IMContainer{

    ArrayList<Folder> subFolders = new ArrayList<>();
    Gson gson = new Gson();
    JsonParser parser = new JsonParser();
    JsonArray foldersJsonArray;

    //<editor-fold desc="Class Vars"

    // main vars
    private String database;
    private String defaultSecurity;
    private String editDate;
    private String effectiveSecurity;
    private String folderType;
    private String id;
    private String inheritedDefaultSecurity;
    private String name;
    private String owner;
    private String parentID;
    private String viewType;
    private String workspaceID;
    private String workspaceName;
    private String wsType;


    //profile vars
    private String author;
    private String authorDescription;
    private String iMClass;
    private String iMSubclass;
    private String[] customField = new String[30];


    //bools
    private boolean hasDocuments;
    private boolean hasSubfolders;
    private boolean containerSavedSearch;
    private boolean contentSavedSearch;
    private boolean externalAsNormal;
    private boolean hidden;

    //</editor-fold>

    //main constructor, parses response string to JSON and pulls all data to fill class variables.
    public Folder(JsonObject folderProfile) {

        if (folderProfile.has("database")) {
            this.database = folderProfile.get("database").getAsString();
        }

        if (folderProfile.has("default_security")) {
            this.defaultSecurity = folderProfile.get("default_security").getAsString();
        }

        if (folderProfile.has("edit_date")) {
            this.editDate = folderProfile.get("edit_date").getAsString();
        }

        if (folderProfile.has("effective_security")) {
            this.effectiveSecurity = folderProfile.get("effective_security").getAsString();
        }

        if (folderProfile.has("folder_type")) {
            this.folderType = folderProfile.get("folder_type").getAsString();
        }

        if (folderProfile.has("has_documents")) {
            this.hasDocuments = folderProfile.get("has_documents").getAsBoolean();
        }

        if (folderProfile.has("has_subfolders")) {
            this.hasSubfolders = folderProfile.get("has_subfolders").getAsBoolean();
        }

        if (folderProfile.has("id")) {
            this.id = folderProfile.get("id").getAsString();
        }

        if (folderProfile.has("inherited_default_security")) {
            this.inheritedDefaultSecurity = folderProfile.get("inherited_default_security").getAsString();
        }

        if (folderProfile.has("is_container_saved_search")) {
            this.containerSavedSearch = folderProfile.get("is_container_saved_search").getAsBoolean();
        }

        if (folderProfile.has("is_content_saved_search")) {
            this.contentSavedSearch = folderProfile.get("is_content_saved_search").getAsBoolean();
        }

        if (folderProfile.has("is_external_as_normal")) {
            this.externalAsNormal = folderProfile.get("is_external_as_normal").getAsBoolean();
        }

        if (folderProfile.has("is_hidden")) {
            this.hidden = folderProfile.get("is_hidden").getAsBoolean();
        }

        if (folderProfile.has("name")) {
            this.name = folderProfile.get("name").getAsString();
        }

        if (folderProfile.has("owner")) {
            this.owner = folderProfile.get("owner").getAsString();
        }

        if (folderProfile.has("parent_id")) {
            this.parentID = folderProfile.get("parent_id").getAsString();
        }

        if (folderProfile.has("profile")) {

            JsonObject imProfile = folderProfile.get("profile").getAsJsonObject();

            if (imProfile.has("author")) {
                this.author = imProfile.get("author").getAsString();
            }

            if (imProfile.has("author_description")) {
                this.authorDescription = imProfile.get("author_description").getAsString();
            }

            if (imProfile.has("class")) {
                this.iMClass = imProfile.get("class").getAsString();
            }

            if (imProfile.has("subclass")) {
                this.iMSubclass = imProfile.get("subclass").getAsString();
            }

            for (int x = 0; x <= 30; x++) {

                if (imProfile.has("custom" + (x + 1))) {                                                        // add one to x var at member name to account for array starting at 0
                    this.customField[x] = imProfile.get("custom" + (x + 1)).getAsString();
                }
            }
        }

        if (folderProfile.has("view_type")) {
            this.viewType = folderProfile.get("view_type").getAsString();
        }

        if (folderProfile.has("workspace_id")) {
            this.workspaceID = folderProfile.get("workspace_id").getAsString();
        }

        if (folderProfile.has("workspace_name")) {
            this.workspaceName = folderProfile.get("workspace_name").getAsString();
        }

        if (folderProfile.has("wstype")) {
            this.wsType = folderProfile.get("wstype").getAsString();
        }
    }

    public Folder addSubfolder(JsonObject folderProfile){
        System.out.println("Adding " + folderProfile.get("id") + " to subfolders of " + this.getId());
        Folder subfolder = new Folder(folderProfile);
        this.subFolders.add(subfolder);
        System.out.println("subfolder added at index " + this.subFolders.indexOf(subfolder));
        return subfolder;
    }

    public void saveFolders() {

        for (int x = 0; x < this.foldersJsonArray.size(); x++) {
            this.subFolders.add(new Folder(this.foldersJsonArray.get(x).getAsJsonObject()));
        }

        for(int x = 0; x < subFolders.size(); x++){
            if(subFolders.get(x).hasSubfolders()){
                subFolders.get(x).saveFolders();
            }
        }
    }

    @Override
    public String toString() {
        return "Folder{" +
                "database='" + database + '\'' + "\n" +
                ", defaultSecurity='" + defaultSecurity + '\'' + "\n" +
                ", editDate='" + editDate + '\'' + "\n" +
                ", effectiveSecurity='" + effectiveSecurity + '\'' + "\n" +
                ", folderType='" + folderType + '\'' + "\n" +
                ", id='" + id + '\'' + "\n" +
                ", inheritedDefaultSecurity='" + inheritedDefaultSecurity + '\'' + "\n" +
                ", name='" + name + '\'' + "\n" +
                ", owner='" + owner + '\'' + "\n" +
                ", parentID='" + parentID + '\'' + "\n" +
                ", viewType='" + viewType + '\'' + "\n" +
                ", workspaceID='" + workspaceID + '\'' + "\n" +
                ", workspaceName='" + workspaceName + '\'' + "\n" +
                ", wsType='" + wsType + '\'' + "\n" +
                ", author='" + author + '\'' + "\n" +
                ", authorDescription='" + authorDescription + '\'' + "\n" +
                ", iMClass='" + iMClass + '\'' + "\n" +
                ", iMSubclass='" + iMSubclass + '\'' + "\n" +
                ", customField=" + Arrays.toString(customField) + "\n" +
                ", hasDocuments=" + hasDocuments + "\n" +
                ", hasSubfolders=" + hasSubfolders + "\n" +
                ", containerSavedSearch=" + containerSavedSearch + "\n" +
                ", contentSavedSearch=" + contentSavedSearch + "\n" +
                ", externalAsNormal=" + externalAsNormal + "\n" +
                ", hidden=" + hidden + "\n" +
                '}';
    }

    //<editor-fold desc="Getters"


    public JsonArray getFoldersJsonArray() {
        return foldersJsonArray;
    }

    public String getDatabase() {
        return database;
    }

    public String getDefaultSecurity() {
        return defaultSecurity;
    }

    public String getEditDate() {
        return editDate;
    }

    public String getEffectiveSecurity() {
        return effectiveSecurity;
    }

    public String getFolderType() {
        return folderType;
    }

    public String getId() {
        return id;
    }

    public String getInheritedDefaultSecurity() {
        return inheritedDefaultSecurity;
    }

    public String getName() {
        return name;
    }

    public String getOwner() {
        return owner;
    }

    public String getParentID() {
        return parentID;
    }

    public String getViewType() {
        return viewType;
    }

    public String getWorkspaceID() {
        return workspaceID;
    }

    public String getWorkspaceName() {
        return workspaceName;
    }

    public String getWsType() {
        return wsType;
    }

    public String getAuthor() {
        return author;
    }

    public String getAuthorDescription() {
        return authorDescription;
    }

    public String getiMClass() {
        return iMClass;
    }

    public String getiMSubclass() {
        return iMSubclass;
    }

    public String[] getCustomField() {
        return customField;
    }

    public boolean HasDocuments() {
        return hasDocuments;
    }

    public boolean hasSubfolders() {
        return hasSubfolders;
    }

    public boolean isContainerSavedSearch() {
        return containerSavedSearch;
    }

    public boolean isContentSavedSearch() {
        return contentSavedSearch;
    }

    public boolean isExternalAsNormal() {
        return externalAsNormal;
    }

    public boolean isHidden() {
        return hidden;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    //</editor-fold>

    //<editor-fold desc="Setters"


    public void setFoldersJsonArray(JsonArray foldersJsonArray) {
        this.foldersJsonArray = foldersJsonArray;
    }

    public void setDefaultSecurity(String defaultSecurity) {
        this.defaultSecurity = defaultSecurity;
    }

    public void setEditDate(String editDate) {
        this.editDate = editDate;
    }

    public void setEffectiveSecurity(String effectiveSecurity) {
        this.effectiveSecurity = effectiveSecurity;
    }

    public void setFolderType(String folderType) {
        this.folderType = folderType;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setInheritedDefaultSecurity(String inheritedDefaultSecurity) {
        this.inheritedDefaultSecurity = inheritedDefaultSecurity;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public void setParentID(String parentID) {
        this.parentID = parentID;
    }

    public void setViewType(String viewType) {
        this.viewType = viewType;
    }

    public void setWorkspaceID(String workspaceID) {
        this.workspaceID = workspaceID;
    }

    public void setWorkspaceName(String workspaceName) {
        this.workspaceName = workspaceName;
    }

    public void setWsType(String wsType) {
        this.wsType = wsType;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setAuthorDescription(String authorDescription) {
        this.authorDescription = authorDescription;
    }

    public void setiMClass(String iMClass) {
        this.iMClass = iMClass;
    }

    public void setiMSubclass(String iMSubclass) {
        this.iMSubclass = iMSubclass;
    }

    public void setCustomField(String[] customField) {
        this.customField = customField;
    }

    public void setHasDocuments(boolean hasDocuments) {
        this.hasDocuments = hasDocuments;
    }

    public void setHasSubfolders(boolean hasSubfolders) {
        this.hasSubfolders = hasSubfolders;
    }

    public void setContainerSavedSearch(boolean containerSavedSearch) {
        this.containerSavedSearch = containerSavedSearch;
    }

    public void setContentSavedSearch(boolean contentSavedSearch) {
        this.contentSavedSearch = contentSavedSearch;
    }

    public void setExternalAsNormal(boolean externalAsNormal) {
        this.externalAsNormal = externalAsNormal;
    }

    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }

    //</editor-fold>

}

