<?php

namespace Juqn;

use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\event\player\PlayerDropItemEvent;
use pocketmine\event\player\PlayerDeathEvent;
use pocketmine\event\player\PlayerMoveEvent;
use pocketmine\math\Vector3;
use pocketmine\Player;

class EventListener implements Listener {
	
	public $plugin;
	
	public function __construct(Loader $plugin){
		$this->plugin = $plugin;
	}
	
	public function getPlugin(){
		return $this->plugin;
	}
	
	public function notFall(EntityDamageEvent $event){
		$entity = $event->getEntity();
		
		$manager = $this->getPlugin()->getArenaManager();
		
		if($entity instanceof Player){
			if($manager->inArena($entity)){
				$arena = $manager->inArena($entity);
				$cause = $event->getCause();
				if($arena->getStatus() !== 2){
					$event->setCancelled();
				} else {
					if($cause == EntityDamageEvent::CAUSE_FALL){
						$event->setCancelled();
					}
				}
			}
		}
	}
	
	public function onDamage(EntityDamageEvent $event){
		$entity = $event->getEntity();
		
		$manager = $this->getPlugin()->getArenaManager();
		
		if($entity instanceof Player){
			$last = $entity->getLastDamageCause();
			
			if($manager->inArena($entity)){
				if($last instanceof EntityDamageByEntityEvent){
					$damager = $last->getDamager();
					if($damager instanceof Player){
						if($manager->inArena($damager)){
							$arena = $manager->inArena($entity);
							$arena->setLastHit($entity, $damager);
						}
					}
				}
			}
		}
	}
	
	public function onBreak(BlockBreakEvent $event){
		$player = $event->getPlayer();
		
		$manager = $this->getPlugin()->getArenaManager();
		
		if($manager->inArena($player)){
			$arena = $manager->inArena($player);
			if($arena->getStatus() !== 2){
				$event->setCancelled();
			}
		}
	}
	
	public function onPlace(BlockPlaceEvent $event){
		$player = $event->getPlayer();
		
		$manager = $this->getPlugin()->getArenaManager();
		
		if($manager->inArena($player)){
			$arena = $manager->inArena($player);
			if($arena->getStatus() !== 2){
				$event->setCancelled();
			}
		}
	}
	
	public function onQuit(PlayerQuitEvent $event){
		$player = $event->getPlayer();
		
		$manager = $this->getPlugin()->getArenaManager();
		
		if($manager->inArena($player)){
			$arena = $manager->inArena($player);
			if($arena->quit($player)){
				foreach($arena->getPlayers() as $players){
					$players->sendMessage("§8(§aM§fB§8) §e{$player->getName()} §fleft the game! §7[".count($arena->getPlayers())."/{$arena->getSlots()}]");
				}
			}
		}
	}
	
	public function onDropItem(PlayerDropItemEvent $event){
		$player = $event->getPlayer();
		
		$manager = $this->getPlugin()->getArenaManager();
		
		if($manager->inArena($player)){
			$arena = $manager->inArena($player);
			if($arena->getStatus() !== 2){
				$event->setCancelled();
			}
		}
	}
	
	public function onInteract(PlayerInteractEvent $event)
    {
        $player = $event->getPlayer();
        $name = $player->getName();

        $block = $event->getBlock();

        $item = $player->getInventory()->getItemInHand();

        $manager = $this->getPlugin()->getArenaManager();

        /**if (isset($manager->sessions[$name])) {
            $session = $manager->sessions[$name];
            $xyz = array($block->x, $block->y, $block->z);
            $player->sendMessage("§aСпавн успешно добавлен §7[" . (count($session['spawns']) + 1) . " - " . $session['slots'] . "]");
            $manager->sessions[$name]['spawns'][count($session['spawns']) + 1] = $xyz;
            if (count($session['spawns']) == ($session['slots'] - 1)) {
                $manager->registerArena($name);
                $player->sendMessage("§aАрена создана успешно");
                $player->setGamemode(0);
                $player->teleport($this->getPlugin()->getServer()->getDefaultLevel()->getSafeSpawn());
                $player->setFood(20);
                $player->setHealth(20);
            }
        }**/

        if ($manager->inArena($player)) {
            $arena = $manager->inArena($player);
            if ($arena->getStatus() == 2) {
                if ($item->getId() == 288) {
                    $event->setCancelled();
                    $player->setMotion(new Vector3(0, 1.9, 0));
                    $hand = $player->getInventory()->getItemInHand();
                    $handClone = clone $player->getInventory()->getItemInHand();
                    $handClone->setCount($handClone->getCount() - 1);
                    $player->getInventory()->removeItem($hand);
                    $player->getInventory()->addItem($handClone);
                }
            } else {
            	if($item->getId() == 355){
            		$event->setCancelled();
            		$arena->quit($player);
            		foreach($arena->getPlayers() as $players){
						$players->sendMessage("§8(§aM§fB§8) §e{$player->getName()}§f left the game! §7[".count($arena->getPlayers())."/{$arena->getSlots()}]");
					}
            	}
            }
        }
    }
	
	public function onDeath(PlayerDeathEvent $event){
		$player = $event->getPlayer();
		
		$manager = $this->getPlugin()->getArenaManager();
		
		if($manager->inArena($player)){
			$arena = $manager->inArena($player);
			$event->setDrops([]);
			if($arena->getLastHit($player)){
				$arena->quit($player);
				$last = $arena->getLastHit($player);
				foreach($arena->getPlayers() as $players){
					$players->sendMessage("§8(§aM§fB§8) §e{$player->getName()} §fwas killed by §e{$last}§f! §7[".count($arena->getPlayers())."/{$arena->getSlots()}]");
				}
			} else {
				$arena->quit($player);
				foreach($arena->getPlayers() as $players){
					$players->sendMessage("§8(§aM§fB§8) §e{$player->getName()} §ffell into the void! §7[".count($arena->getPlayers())."/{$arena->getSlots()}]");
				}
			}
			$event->setDeathMessage(null);
		}
	}
	
	public function onMove(PlayerMoveEvent $event){
		$player = $event->getPlayer();
		
		$manager = $this->getPlugin()->getArenaManager();
		
		if($manager->inArena($player)){
			$arena = $manager->inArena($player);
			if($arena->getStatus() !== 2){
				if($player->getY() <= 65){
                    
					$arena->backPosition($player);
				}
			}
		}
	}
}