<?php

namespace bedwars\entity;

use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;

use pocketmine\Player;

use pocketmine\level\particle\LavaParticle;
use pocketmine\level\particle\LavaDripParticle;

use bedwars\BedWars;
use bedwars\game\Game;

class Manager extends NPC {

	const TITLES = ["§l§aCLICK TO PLAY!", "§l§eCLICK TO PLAY!"];
	const MODE = [
	    "solo" => "SOLO",
	    "duo" => "DUO",
	    "trio" => "TRIO",
	    "squad" => "QUARTETO"
	];

	/** @var int */
	private $canUpdate;
	
	/** @var int */
	private $titleTick = 0;

	/** @var time() */
	private $effectTime = null;
	
	/**
	 * @param float $currentTick
	 * @param return bool
     */
	public function onUpdate($currentTicks) {
		if ($this->closed) {
			return false;
		} 

		if ($this->canUpdate++ >= 13) {
			$this->canUpdate = 0;

			if ($this->titleTick++ > (count(self::TITLES) - 2)) {
				$this->titleTick = 0;
			}	
			$plugin = BedWars::$loaded;
			
			$lines = [self::TITLES[$this->titleTick], '§bBEDWARS'];
			
			if (isset($this->namedtag->Mode)) {
			    $mode = $this->namedtag["Mode"];
			    
			    $lines[] = "§f" . self::MODE[$mode];
			    $playing = 0;

	    		if ($plugin instanceof BedWars) {
			     	foreach ($plugin->arenas as $value) {
		    			if (!($value instanceof Game)) {
			    			continue;
		    			}
		              
		                if ($value->getMode() == $mode) {
		                     $playing += $value->getSlot(true);
		                }
	         	    }	
		     		$lines[] = "§l§ePlaying: " . $playing;
    			} else {
				    $lines[] = "§cInstall the bedwars plugin!";
	     		}
			} else {
			    $lines[] = "§cNO MODE";
			}
			$this->addLines($lines);
		}
		return parent::onUpdate($currentTicks);
	}

	/**
	 * @param float             $damage
	 * @param EntityDamageEvent $source
	 *
	 * @return bool
	 */
	public function attack($damage, EntityDamageEvent $source) {
	    parent::attack($damage, $source);
	    
	    if ($this->closed) {
	        return false;
	    }
	    
		if ($source instanceof EntityDamageByEntityEvent and $source->getDamager() instanceof Player) {
			$damager = $source->getDamager();
			
			$canEffect = true;
			
			if ($this->effectTime == null) {
				$this->effectTime = time();
			} else {
				if ((time() - $this->effectTime) >= 1)  {
					$this->effectTime = time();
				} else {
					$canEffect = false;
				}
			}

			//if ($canEffect) {
				for ($i = 0; $i <= 3; $i++) {
					$va = [0 => -0.1, 1 => 0.1, 2 => 0.2, 3 => -0.2, 4 => 0.4, 5 => -0.4, 6 => 0.5, 7 => -0.5];
					$this->level->addParticle(new LavaParticle($this->add($va[mt_rand(0, 7)], $va[mt_rand(0, 7)] + 1, $va[mt_rand(0, 7)])), $this->hasSpawned);
					$this->level->addParticle(new LavaDripParticle($this->add($va[mt_rand(0, 7)], $va[mt_rand(0, 7)] + 1, $va[mt_rand(0, 7)])), $this->hasSpawned);
				}
			//} 

			if (isset($this->namedtag->Mode)) {
			    $mode = $this->namedtag["Mode"];
			    $plugin = BedWars::$loaded;
			    $canJoin = false;
			
		    	foreach ($plugin->arenas as $value) {
	                if (!$value instanceof Game) {
						continue;
					}
	            
	                 if ($value->getMode() == $mode) {
	                    if ($value->join($damager) === true) {
							$canJoin = true;
							break;
						}
	                }
	            }
	            if ($canJoin === false) {
	                $damager->sendMessage("§cThere are no matches available!");
	            }
			} else {
			    $damager->sendMessage("§cMode of this npc was not specified");
			}
		}
		
	}
}
?>