package zmo.technology.lite.core;

import java.io.IOException;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;

import zmo.technology.lite.core.ZeroFolders.ZeroFolderContainer;

public class ZeroFolders extends HashMap<String, ZeroFolderContainer> {

    public static final String ZERO_FOLDER_ENTITY_TICKET = "ticket";

    public static class ZeroFolderEntity extends ZeroCoreStructs {

        private String relevancyID;
        private String directory;
        private String path;

        private String name;
        private String mineType;
        private byte[] datas;

        public ZeroFolderEntity() {
            super();
        }

        public ZeroFolderEntity(ResultSet set) throws SQLException {
            super(set);

            if (inResultSet(set, "RELEVANCY_ID"))
                relevancyID = set.getString("RELEVANCY_ID");

            if (inResultSet(set, "DIRECTORY"))
                directory = set.getString("DIRECTORY");
            if (inResultSet(set, "PATH"))
                path = set.getString("PATH");

            if (inResultSet(set, "NAME"))
                name = set.getString("NAME");

            if (inResultSet(set, "MINE_TYPE"))
                mineType = set.getString("MINE_TYPE");

            if (inResultSet(set, "BYTES"))
                datas = set.getBytes("BYTES");

            if (inResultSet(set, "REF"))
                getFeatures().put("ref", set.getString("REF"));
        }

        public String getRelevancyID() {
            return relevancyID;
        }

        public void setRelevancyID(String relevancyID) {
            this.relevancyID = relevancyID;
        }

        public String getDirectory() {
            return directory;
        }

        public void setDirectory(String directory) {
            this.directory = directory;
        }

        public String getName() {
            return name;
        }

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

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getMineType() {
            return mineType;
        }

        public void setMineType(String mineType) {
            this.mineType = mineType;
        }

        public byte[] getDatas() {
            return datas;
        }

        public void setDatas(byte[] datas) {
            this.datas = datas;
        }
    }

    public static class ZeroFolderContainer {

        private int maxLimit;
        private String directory;
        private List<ZeroFolderEntity> entities;

        public ZeroFolderContainer() {
            super();
            entities = new ArrayList<>();
        }

        public ZeroFolderContainer(int maxLimit, String directory) {
            this();
            this.maxLimit = maxLimit;
            this.directory = directory;
        }

        public int getMaxLimit() {
            return maxLimit;
        }

        public void setMaxLimit(int maxLimit) {
            this.maxLimit = maxLimit;
        }

        public String getDirectory() {
            return directory;
        }

        public void setDirectory(String directory) {
            this.directory = directory;
        }

        public List<ZeroFolderEntity> getEntities() {
            return entities;
        }

        public void setEntities(List<ZeroFolderEntity> entities) {
            this.entities = entities;
        }

        public void addEntity(ZeroFolderEntity entity) {
            entities.add(entity);
        }
    }

    public static class ZeroFoldersParser implements ObjectDeserializer, ObjectSerializer {

        @Override
        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
                throws IOException {
            serializer.write(object);
        }

        @Override
        @SuppressWarnings("unchecked")
        public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
            JSONObject jsonObject = parser.parseObject();
            ZeroFolders folders = new ZeroFolders();
            for (String key : jsonObject.keySet())
                folders.put(key, jsonObject.getJSONObject(key).toJavaObject(ZeroFolderContainer.class));
            return (T) folders;
        }

        @Override
        public int getFastMatchToken() {
            return 0;
        }

    }

    public ZeroFolders() {
        super();
    }

    public void addContainer(ZeroFolderContainer container) {
        put(container.getDirectory(), container);
    }

    public void resetContainer(String directory, int maxLimit) {
        if (containsKey(directory)) {
            get(directory).setDirectory(directory);
            get(directory).setMaxLimit(maxLimit);
        }
    }
}
