package nl.sbdeveloper.themeparkplus.listeners;

import nl.sbdeveloper.themeparkplus.ThemeParkPlus;
import nl.sbdeveloper.themeparkplus.api.PlusAPI;
import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow;
import nl.sbdeveloper.themeparkplus.util.ConfigUtil;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;

import java.util.Collections;
import java.util.Optional;

/**
 * Sign setup listener
 */
public class SignListeners implements Listener {
    /*
    [ThemeParkPlus]
    XXXX
    <RideName>
    XXXX
     */

    @EventHandler
    public void onCreate(SignChangeEvent e) {
        Player p = e.getPlayer();
        String[] lines = e.getLines();

        //Only check themeparkplus signs!
        if (!lines[0].equalsIgnoreCase("[ThemeParkPlus]")) return;

        if (!ThemeParkPlus.hasParkHook()) {
            p.sendMessage(ConfigUtil.getMessage("General.RequiresThemePark"));
            return;
        }

        if (!p.hasPermission("tpp.fastpass.create")) {
            p.sendMessage(ConfigUtil.getMessage("General.NoPermission"));
            return;
        }

        if (!nl.iobyte.themepark.ThemePark.getInstance().getAPI().getAttractionService().hasAttraction(lines[2])) {
            p.sendMessage(ConfigUtil.getMessage("Fastpass.UnknownRide", Collections.singletonMap("ridename", e.getLine(2))));
            return;
        }

        String mLineOne = ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("Fastpass.MachineSign.Row1"));
        String mLineTwo = ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("Fastpass.MachineSign.Row2"));
        String sLineOne = ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("Fastpass.ScannerSign.Row1"));
        String sLineTwo = ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("Fastpass.ScannerSign.Row2"));
        String wrLineOne = ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("WaitingRow.Sign.Row1"));
        String wrLineTwo = ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("WaitingRow.Sign.Row2"));
        String mrLineOne = ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.Sign.Row1"));
        String mrLineTwo = ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.Sign.Row2"));

        if (lines[1].equalsIgnoreCase("Machine") && !lines[2].isEmpty() && !lines[3].isEmpty()) {
            e.setLine(0, mLineOne);
            e.setLine(1, mLineTwo);
        } else if (lines[1].equalsIgnoreCase("Scanner") && !lines[2].isEmpty() && !lines[3].isEmpty()) {
            e.setLine(0, sLineOne);
            e.setLine(1, sLineTwo);
        } else if (lines[1].equalsIgnoreCase("WaitingRow") && !lines[2].isEmpty()) {
            WaitingRow foundRow = PlusAPI.getRow(lines[2]);
            if (foundRow == null) {
                if (lines[3].isEmpty()) {
                    p.sendMessage(ConfigUtil.getMessage("General.IncorrectSign"));
                    return;
                }
                foundRow = new WaitingRow(lines[2], lines[3]);
                PlusAPI.addRow(foundRow);
            }
            foundRow.addSignLocation(e.getBlock().getLocation());
            ThemeParkPlus.getData().save();

            e.setLine(0, wrLineOne);
            e.setLine(1, wrLineTwo);
            e.setLine(2, ConfigUtil.__(nl.iobyte.themepark.ThemePark.getInstance().getAPI().getAttractionService().getAttraction(lines[2]).getName()));
            e.setLine(3, "0 min.");

            p.sendMessage(ConfigUtil.getMessage("WaitingRow.SignCreated"));
        } else if (lines[1].equalsIgnoreCase("Malfunction") && !lines[2].isEmpty()) {
            e.setLine(0, mrLineOne);
            e.setLine(1, mrLineTwo);
        } else {
            p.sendMessage(ConfigUtil.getMessage("General.IncorrectSign"));
        }
    }

    @EventHandler
    public void onDestroySign(BlockBreakEvent e) {
        BlockState signblocks = e.getBlock().getState();
        if ((signblocks instanceof Sign)) {
            Optional<WaitingRow> rowOptional = PlusAPI.getRowBySign(e.getBlock().getLocation());
            if (rowOptional.isEmpty()) return;

            WaitingRow row = rowOptional.get();
            if (row.getSignLocations().size() == 1) {
                PlusAPI.removeRow(row);
            } else {
                row.removeSignLocation(e.getBlock().getLocation());
            }
            ThemeParkPlus.getData().save();
        }
    }
}
