package org.black_ixx.bossshop.listeners;

import java.util.HashMap;
import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.core.BSShop;
import org.black_ixx.bossshop.core.BSShopManager;
import org.black_ixx.bossshop.managers.StringManager;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;

import cc.util.bossshop.pluginmodel.INeedConfig;

public class SignListener implements Listener,INeedConfig{

    private boolean mEnable;
    private BossShop mPlugin;

    public SignListener(BossShop plugin){
        this.mPlugin=plugin;
        this.mPlugin.getServer().getPluginManager().registerEvents(this,this.mPlugin);
        this.mPlugin.registerConfigModel(this);
    }

    @Override
    public void setConfig(){
        this.mEnable=this.mPlugin.getConfigManager().signsEnabled();
    }
    
    private BSShop getBossShopSign(String line){
        if(line==null||line=="") return null;
        line=line.toLowerCase();
        HashMap<String,BSShop> set=this.mPlugin.getManager(BSShopManager.class).getShops();
        for(String s : set.keySet()){
            BSShop shop=set.get(s);
            if(line.endsWith(shop.getSignText().toLowerCase())) return shop;
        }
        return null;
    }

    @EventHandler
    public void createSign(SignChangeEvent e){
        if(!this.mEnable) return;
        BSShop shop=getBossShopSign(e.getLine(0));
        if(shop!=null){
            if(shop.needPermToCreateSign()){
                if(!e.getPlayer().hasPermission("BossShop.createSign")){
                    this.mPlugin.send(e.getPlayer(),this.mPlugin.getLangManager().getNode("Main.NoPermitToCreateSignShop"));
                    e.setCancelled(true);
                    return;
                }
            }
            if(e.getLine(0)!=""){
                e.setLine(0,StringManager.transform(e.getLine(0)));
            }
            if(e.getLine(1)!=""){
                e.setLine(1,StringManager.transform(e.getLine(1)));
            }
            if(e.getLine(2)!=""){
                e.setLine(2,StringManager.transform(e.getLine(2)));
            }
            if(e.getLine(3)!=""){
                e.setLine(3,StringManager.transform(e.getLine(3)));
            }
        }
    }

    @EventHandler
    public void interactSign(PlayerInteractEvent e){
        if(!this.mEnable) return;
        if(e.getClickedBlock()!=null){
            if(e.getAction()==Action.RIGHT_CLICK_BLOCK){
                Block b=e.getClickedBlock();
                if(b.getType()==Material.SIGN||b.getType()==Material.SIGN_POST||b.getType()==Material.WALL_SIGN){
                    if(b.getState() instanceof Sign){
                        Sign s=(Sign)b.getState();
                        BSShop shop=getBossShopSign(s.getLine(0));
                        if(shop!=null){
                            if(e.getPlayer().hasPermission("BossShop.open")||e.getPlayer().hasPermission("BossShop.open.sign")){
                                this.mPlugin.getManager(BSShopManager.class).openShop(e.getPlayer(),shop);
                                return;
                            }
                            this.mPlugin.send(e.getPlayer(),this.mPlugin.getLangManager().getNode("Main.NoPermitToOpenSignShop"));
                            return;
                        }
                    }
                }
            }
        }
    }

}
