/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2021  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.bausystem.features.region.items;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.TNTMode;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;

@Linked
public class TntBauGuiItem extends BauGuiItem {

    public TntBauGuiItem() {
        super(0);
    }

    @Override
    public ItemStack getItem(Player player) {
        switch (Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class)) {
            case DENY:
                return new SWItem(Material.MINECART, BauSystem.MESSAGE.parse("REGION_ITEM_TNT_OFF", player)).getItemStack();
            case ONLY_TB:
                return new SWItem(Material.TNT_MINECART, BauSystem.MESSAGE.parse("REGION_ITEM_TNT_ONLY_TB", player)).getItemStack();
            case ONLY_BUILD:
                return new SWItem(Material.OBSIDIAN, BauSystem.MESSAGE.parse("REGION_ITEM_TNT_ONLY_BUILD", player)).getItemStack();
            default:
                return new SWItem(Material.TNT, BauSystem.MESSAGE.parse("REGION_ITEM_TNT_ON", player)).getItemStack();
        }
    }

    @Override
    public boolean click(ClickType click, Player p) {
        if (click == ClickType.LEFT) {
            switch (Region.getRegion(p.getLocation()).getPlain(Flag.TNT, TNTMode.class)) {
                case DENY:
                    updateTntMode(TNTMode.ALLOW, p);
                    break;
                case ALLOW:
                    if (Region.getRegion(p.getLocation()).hasType(RegionType.BUILD) && Region.getRegion(p.getLocation()).hasType(RegionType.TESTBLOCK)) {
                        updateTntMode(TNTMode.ONLY_TB, p);
                        break;
                    }
                default:
                    updateTntMode(TNTMode.DENY, p);
            }
        } else {
            p.closeInventory();
            SWInventory selector = new SWInventory(p, 9, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_TITLE", p));
            selector.setItem(1, new SWItem(Material.TNT, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_ON", p), clickType -> {
                updateTntMode(TNTMode.ALLOW, p);
                p.closeInventory();
            }));
            if (Region.getRegion(p.getLocation()).hasType(RegionType.BUILD) && Region.getRegion(p.getLocation()).hasType(RegionType.TESTBLOCK)) {
                selector.setItem(3, new SWItem(Material.TNT_MINECART, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_ONLY_TB", p), clickType -> {
                    updateTntMode(TNTMode.ONLY_TB, p);
                    p.closeInventory();
                }));
                selector.setItem(5, new SWItem(Material.OBSIDIAN, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_ONLY_BUILD", p), clickType -> {
                    updateTntMode(TNTMode.ONLY_BUILD, p);
                    p.closeInventory();
                }));
            }
            selector.setItem(7, new SWItem(Material.MINECART, BauSystem.MESSAGE.parse("REGION_ITEM_SELECTOR_OFF", p), clickType -> {
                updateTntMode(TNTMode.DENY, p);
                p.closeInventory();
            }));
            selector.open();
        }
        return false;
    }

    private void updateTntMode(TNTMode mode, Player p) {
        switch (mode) {
            case ONLY_TB:
                p.performCommand("tnt tb");
                break;
            case ONLY_BUILD:
                p.performCommand("tnt build");
                break;
            case ALLOW:
                p.performCommand("tnt on");
                break;
            default:
                p.performCommand("tnt off");
                break;
        }
        update();
    }

    @Override
    public Permission permission() {
        return Permission.BUILD;
    }
}
