/* 
    This file is a part of the SteamWar software.
    
    Copyright (C) 2020  SteamWar.de-Serverteam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

package de.steamwar.schematicsystem;

import de.steamwar.sql.SchematicType;
import lombok.Getter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;

@Getter
public class CheckSchemType {

    private static final Map<SchematicType, CheckSchemType> types = new HashMap<>();

    private final int width;
    private final int height;
    private final int depth;
    private final int maxDispenserItems;

    private final Map<Set<String>, Integer> limits;
    private final int maxBlocks;

    private final Date deadline;
    private final float maxBlastResistance;

    private CheckSchemType(ConfigurationSection section) {
        String name = section.getString("Schematic.Type");
        width = section.getInt("Schematic.Size.x");
        height = section.getInt("Schematic.Size.y");
        depth = section.getInt("Schematic.Size.z");

        maxDispenserItems = section.getInt("Schematic.MaxDispenserItems", 128);
        maxBlocks = section.getInt("Schematic.MaxBlocks", 0);

        maxBlastResistance = (float) section.getDouble("Schematic.MaxDesignBlastResistance", Double.MAX_VALUE);

        limits = new HashMap<>();
        for(Map<?, ?> entry : section.getMapList("Schematic.Limited")) {
            int amount = (Integer) entry.get("Amount");
            Set<String> materials = new HashSet<>((List<String>) entry.get("Materials"));
            if(amount == 0) {
                materials.forEach(material -> limits.put(Collections.singleton(material), 0));
            } else {
                limits.put(materials, amount);
            }
        }

        String deadlineString = section.getString("deadline", null);
        if (deadlineString != null) {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
                deadline = dateFormat.parse(deadlineString);
            } catch (ParseException e) {
                throw new SecurityException(e.getMessage(), e);
            }
        } else {
            deadline = null;
        }

        types.put(SchematicType.fromDB(name.toLowerCase()), this);
        types.put(SchematicType.fromDB("c" + name.toLowerCase()), this);
    }

    static {
        File folder = new File(SchematicSystem.getInstance().getDataFolder().getParentFile(), "FightSystem");

        if(folder.exists()) {
            for(File configFile : folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))) {
                YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
                if (!config.isList("CheckQuestions") && config.getBoolean("Schematic.ManualCheck", true))
                    continue;

                new CheckSchemType(config);
            }
        }
    }

    public static CheckSchemType get(SchematicType type){
        return types.get(type);
    }

    public Map<Set<String>, Integer> getLimits() {
        return new HashMap<>(limits);
    }

    public boolean isAfterDeadline() {
        return deadline != null && deadline.before(Date.from(Instant.now()));
    }
}
