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;

@SuppressWarnings("unused")
public class Template extends IMContainer {

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

    //<editor-fold desc="Class Variables"
    private String author;
    private String authorDescription;
    private String iMClass;
    private String classDescription;
    private String contentType;
    private String createDate;

//    //Custom fields variables
    private String[] customField = new String[30];
    private String[] customDescription = new String[30];


    private String database;
    private String defaultSecurity;
    private String documentNumber;
    private String editDate;
    private String editProfileDate;
    private String effectiveSecurity;
    private String fileCreateDate;
    private String fileEditDate;
    private String extension;
    private String iwl;
    private String lastUser;
    private String lastUserDescription;
    private String location;
    private String name;
    private String operator;
    private String operatorDescription;
    private String owner;
    private String ownerDescription;
    private String subtype;
    private String type;
    private String typeDescription;
    private String workspaceID;
    private String wsType;
    private String id;

    private int retainDays;
    private int version;

    private long size;

    private boolean declared;
    private boolean declared2;
    private boolean hasDocuments;
    private boolean hasSubfolders;
    private boolean inUse;
    private boolean indexable;
    private boolean checkedOut;
    private boolean containerSavedSearch;
    private boolean contentSavedSearch;
    private boolean external;
    private boolean externalAsNormal;
    private boolean hidden;
    private boolean hipaa;
    private boolean inUse2;
    private boolean restorable;
    //</editor-fold>



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

        //JsonObject templateProfile = parser.parse(templateProfileResponseString).getAsJsonObject();

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

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

        if (templateProfile.has("class_description")){
            this.classDescription = templateProfile.get("class_description").getAsString();
        }

        if (templateProfile.has("content_type")){
            this.contentType = templateProfile.get("content_type").getAsString();
        }

        if (templateProfile.has("create_date")){
            this.createDate = templateProfile.get("create_date").getAsString();
        }

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

        if (templateProfile.has("declared")){
            this.declared = templateProfile.get("declared").getAsBoolean();
        }

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

        if (templateProfile.has("document_number")){
            this.documentNumber = templateProfile.get("document_number").getAsString();
        }

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

        if (templateProfile.has("edit_profile_date")){
            this.editProfileDate = templateProfile.get("edit_profile_date").getAsString();
        }

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

        if (templateProfile.has("extension")){
            this.extension = templateProfile.get("extension").getAsString();
        }

        if (templateProfile.has("file_create_date")){
            this.fileCreateDate = templateProfile.get("file_create_date").getAsString();
        }

        if (templateProfile.has("file_edit_date")){
            this.fileEditDate = templateProfile.get("file_edit_date").getAsString();
        }
        if (templateProfile.has("has_subfolders")){
            this.hasSubfolders = templateProfile.get("has_subfolders").getAsBoolean();
        }

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

        if (templateProfile.has("in_use")){
            this.inUse = templateProfile.get("in_use").getAsBoolean();
        }

        if (templateProfile.has("indexable")){
            this.indexable = templateProfile.get("indexable").getAsBoolean();
        }

        if (templateProfile.has("is_checked_out")){
            this.checkedOut = templateProfile.get("is_checked_out").getAsBoolean();
        }

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

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

        if (templateProfile.has("is_declared")){
            this.declared2 = templateProfile.get("is_declared").getAsBoolean();
        }

        if (templateProfile.has("is_external")){
            this.external = templateProfile.get("is_external").getAsBoolean();
        }

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

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

        if (templateProfile.has("is_hipaa")){
            this.hipaa = templateProfile.get("is_hipaa").getAsBoolean();
        }

        if (templateProfile.has("is_in_use")){
            this.inUse2 = templateProfile.get("is_in_use").getAsBoolean();
        }

        if (templateProfile.has("is_restorable")){
            this.restorable = templateProfile.get("is_restorable").getAsBoolean();
        }

        if (templateProfile.has("iwl")){
            this.iwl = templateProfile.get("iwl").getAsString();
        }

        if (templateProfile.has("last_user")){
            this.lastUser = templateProfile.get("last_user").getAsString();
        }

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

        if (templateProfile.has("operator")){
            this.operator = templateProfile.get("operator").getAsString();
        }

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

        if (templateProfile.has("retain_days")){
            this.retainDays = templateProfile.get("retain_days").getAsInt();
        }

        if (templateProfile.has("size")){
            this.size = templateProfile.get("size").getAsLong();
        }

        if (templateProfile.has("subtype")){
            this.subtype = templateProfile.get("subtype").getAsString();
        }

        if (templateProfile.has("type")){
            this.type = templateProfile.get("type").getAsString();
        }

        if (templateProfile.has("type_description")){
            this.typeDescription = templateProfile.get("type_description").getAsString();
        }

        if (templateProfile.has("version")){
            this.version = templateProfile.get("version").getAsInt();
        }

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

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


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

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

            if (templateProfile.has("custom" + (x+1) + "_description")) {
                this.customDescription[x] = templateProfile.get("custom" + (x+1) + "_description").getAsString();
            }
        }
    }

    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();
            }
        }
    }

    public Folder addSubfolder(JsonObject folderProfile){
        Folder subfolderToAdd = new Folder(folderProfile);
        this.subFolders.add(subfolderToAdd);
        return subfolderToAdd;
    }


    @Override
    public String toString() {
        return "Template{" +
                "subFolders=" + subFolders + "\n" +
                ", templateGson=" + templateGson + "\n" +
                ", parser=" + parser + "\n" +
                ", author='" + author + '\'' + "\n" +
                ", authorDescription='" + authorDescription + '\'' + "\n" +
                ", iMClass='" + iMClass + '\'' + "\n" +
                ", classDescription='" + classDescription + '\'' + "\n" +
                ", contentType='" + contentType + '\'' + "\n" +
                ", createDate='" + createDate + '\'' + "\n" +
                ", customField=" + Arrays.toString(customField) + "\n" +
                ", customDescription=" + Arrays.toString(customDescription) + "\n" +
                ", database='" + database + '\'' + "\n" +
                ", defaultSecurity='" + defaultSecurity + '\'' + "\n" +
                ", documentNumber='" + documentNumber + '\'' + "\n" +
                ", editDate='" + editDate + '\'' + "\n" +
                ", editProfileDate='" + editProfileDate + '\'' + "\n" +
                ", effectiveSecurity='" + effectiveSecurity + '\'' + "\n" +
                ", fileCreateDate='" + fileCreateDate + '\'' + "\n" +
                ", fileEditDate='" + fileEditDate + '\'' + "\n" +
                ", extension='" + extension + '\'' + "\n" +
                ", iwl='" + iwl + '\'' + "\n" +
                ", lastUser='" + lastUser + '\'' + "\n" +
                ", lastUserDescription='" + lastUserDescription + '\'' + "\n" +
                ", location='" + location + '\'' + "\n" +
                ", name='" + name + '\'' + "\n" +
                ", operator='" + operator + '\'' + "\n" +
                ", operatorDescription='" + operatorDescription + '\'' + "\n" +
                ", owner='" + owner + '\'' + "\n" +
                ", ownerDescription='" + ownerDescription + '\'' + "\n" +
                ", subtype='" + subtype + '\'' + "\n" +
                ", type='" + type + '\'' + "\n" +
                ", typeDescription='" + typeDescription + '\'' + "\n" +
                ", workspaceID='" + workspaceID + '\'' + "\n" +
                ", wsType='" + wsType + '\'' + "\n" +
                ", id='" + id + '\'' + "\n" +
                ", retainDays=" + retainDays + "\n" +
                ", version=" + version + "\n" +
                ", size=" + size + "\n" +
                ", declared=" + declared + "\n" +
                ", declared2=" + declared2 + "\n" +
                ", hasDocuments=" + hasDocuments + "\n" +
                ", hasSubfolders=" + hasSubfolders + "\n" +
                ", inUse=" + inUse + "\n" +
                ", indexable=" + indexable + "\n" +
                ", checkedOut=" + checkedOut + "\n" +
                ", containerSavedSearch=" + containerSavedSearch + "\n" +
                ", contentSavedSearch=" + contentSavedSearch + "\n" +
                ", external=" + external + "\n" +
                ", externalAsNormal=" + externalAsNormal + "\n" +
                ", hidden=" + hidden + "\n" +
                ", hipaa=" + hipaa + "\n" +
                ", inUse2=" + inUse2 + "\n" +
                ", restorable=" + restorable + "\n" +
                '}';
    }

    // <editor-fold> desc="Getters"

    public JsonArray getFoldersJsonArray() {
        return foldersJsonArray;
    }

    public ArrayList<Folder> getSubFolders() {
        return subFolders;
    }

    public String getAuthor() {
        return author;
    }

    public String getAuthorDescription() {
        return authorDescription;
    }

    public String getiMClass() {
        return iMClass;
    }

    public String getClassDescription() {
        return classDescription;
    }

    public String getContentType() {
        return contentType;
    }

    public String getCreateDate() {
        return createDate;
    }

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

    public String getCustomFieldDescription(int x){
        return customDescription[x];
    }

    public String getDatabase() {
        return database;
    }

    public String getDefaultSecurity() {
        return defaultSecurity;
    }

    public String getDocumentNumber() {
        return documentNumber;
    }

    public String getEditDate() {
        return editDate;
    }

    public String getEditProfileDate() {
        return editProfileDate;
    }

    public String getFileCreateDate() {
        return fileCreateDate;
    }

    public String getFileEditDate() {
        return fileEditDate;
    }

    public String getIwl() {
        return iwl;
    }

    public String getLastUser() {
        return lastUser;
    }

    public String getLastUserDescription() {
        return lastUserDescription;
    }

    public String getLocation() {
        return location;
    }

    public String getName() {
        return name;
    }

    public String getOperator() {
        return operator;
    }

    public String getOperatorDescription() {
        return operatorDescription;
    }

    public String getOwner() {
        return owner;
    }

    public String getOwnerDescription() {
        return ownerDescription;
    }

    public String getSubtype() {
        return subtype;
    }

    public String getType() {
        return type;
    }

    public String getTypeDescription() {
        return typeDescription;
    }

    public String getWorkspaceID() {
        return workspaceID;
    }

    public String getWsType() {
        return wsType;
    }

    public String getId() {
        return id;
    }

    public int getRetainDays() {
        return retainDays;
    }

    public int getVersion() {
        return version;
    }

    public long getSize() {
        return size;
    }

    public boolean isDeclared() {
        return declared;
    }

    public boolean isExternal() {
        return external;
    }

    public boolean isExternalAsNormal() {
        return externalAsNormal;
    }

    public boolean isHidden() {
        return hidden;
    }

    public boolean isHipaa() {
        return hipaa;
    }

    public boolean isInUse2() {
        return inUse2;
    }

    public boolean hasDocuments() {
        return hasDocuments;
    }

    public boolean hasSubfolders() {
        return hasSubfolders;
    }

    public boolean isInUse() {
        return inUse;
    }

    public boolean isIndexable() {
        return indexable;
    }

    public boolean isCheckedOut() {
        return checkedOut;
    }

    public boolean isContainerSavedSearch() {
        return containerSavedSearch;
    }

    public boolean isContentSavedSearch() {
        return contentSavedSearch;
    }

    public String getEffectiveSecurity() {
        return effectiveSecurity;
    }

    public String getExtension() {
        return extension;
    }

    public boolean isDeclared2() {
        return declared2;
    }

    public boolean isRestorable() {
        return restorable;
    }

    //</editor-fold>

    // <editor-fold> desc="Setters"


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

    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 setClassDescription(String classDescription) {
        this.classDescription = classDescription;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public void setCustomField(int x, String customFieldValue){
        customField[x] = customFieldValue;
    }

    public void setCustomFieldDescription(int x, String customFieldDescription){
        customDescription[x] = customFieldDescription;
    }

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

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

    public void setDocumentNumber(String documentNumber) {
        this.documentNumber = documentNumber;
    }

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

    public void setEditProfileDate(String editProfileDate) {
        this.editProfileDate = editProfileDate;
    }

    public void setFileCreateDate(String fileCreateDate) {
        this.fileCreateDate = fileCreateDate;
    }

    public void setFileEditDate(String fileEditDate) {
        this.fileEditDate = fileEditDate;
    }

    public void setIwl(String iwl) {
        this.iwl = iwl;
    }

    public void setLastUser(String lastUser) {
        this.lastUser = lastUser;
    }

    public void setLastUserDescription(String lastUserDescription) {
        this.lastUserDescription = lastUserDescription;
    }

    public void setLocation(String location) {
        this.location = location;
    }

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

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public void setOperatorDescription(String operatorDescription) {
        this.operatorDescription = operatorDescription;
    }

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

    public void setOwnerDescription(String ownerDescription) {
        this.ownerDescription = ownerDescription;
    }

    public void setSubtype(String subtype) {
        this.subtype = subtype;
    }

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

    public void setTypeDescription(String typeDescription) {
        this.typeDescription = typeDescription;
    }

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

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

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

    public void setRetainDays(int retainDays) {
        this.retainDays = retainDays;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public void setDeclared(boolean declared) {
        this.declared = declared;
    }

    public void setExternal(boolean external) {
        this.external = external;
    }

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

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

    public void setHipaa(boolean hipaa) {
        this.hipaa = hipaa;
    }

    public void setInUse2(boolean inUse2) {
        this.inUse2 = inUse2;
    }

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

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

    public void setInUse(boolean inUse) {
        this.inUse = inUse;
    }

    public void setIndexable(boolean indexable) {
        this.indexable = indexable;
    }

    public void setCheckedOut(boolean checkedOut) {
        this.checkedOut = checkedOut;
    }

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

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

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

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public void setDeclared2(boolean declared2) {
        this.declared2 = declared2;
    }

    public void setRestorable(boolean restorable) {
        this.restorable = restorable;
    }

    //</editor-fold>
}
