<?php

namespace bedwars\entity;

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

use pocketmine\event\entity\EntityDamageEvent;

use pocketmine\network\protocol\AddPlayerPacket;

use pocketmine\Player;

use pocketmine\level\format\FullChunk;

use bedwars\util\Team;

class BedText extends NPC {
    
    /** @var Game */
    private $game;
    
    /** @var int */
    private $timeUpdate = 0;
    
    /**
     * @return string
     */
    public function getTeam() : string {
        return $this->namedtag["Team"];
    }
    
    /**
     * @return bool
     */
    public function isAlive() : bool {
        return ($this->namedtag["Alive"] == 1 ? true : false);
    }
    
    /**
    * @param int $currentTick 
    */
    public function onUpdate($tick) {
        if ($this->closed) {
        	return false;
        }

	    if (!($this->chunk instanceof FullChunk)) {
	   		return false;
	    }

	    if ($this->timeUpdate > 20) {
	        $this->timeUpdate = 0;
	        
	        if (!isset($this->game->teams[$this->getTeam()])) {
	            return true;
	        }

	        $team = $this->game->teams[$this->getTeam()];
            
            if (!($team instanceof Team)) {
                return false;
            }
            
            if (!$team->isAlive() and $this->isAlive()) {
                $this->namedtag->Alive->setValue(0);
                $this->respawnToAll();
            }
        } else {
            $this->timeUpdate++;
        }
        return true;
    }
    
    /** 
    * TODO:
    */
    public function attack($damager, EntityDamageEvent $source) {}
    
    
	public function spawnTo(Player $player) {
		if ($player !== $this and !isset($this->hasSpawned[$player->getLoaderId()])) {
			$this->hasSpawned[$player->getLoaderId()] = $player;
			
			$plugin = BedWars::$loaded;

            if (!($plugin instanceof BedWars)) {
                $this->close();
                return false;
            }

            $gameId = $this->namedtag["GameId"];

            if (isset($plugin->arenas[$gameId]) === false) {
                $this->close();
                return false;
            }

            $game = $plugin->arenas[$gameId];
        
            $this->game = $game;
    
            if (!($this->game instanceof Game)) {
             	return false;
            }
        
            $team = $this->game->getTeam($player->getName());
        
            if (!($team instanceof Team)) {
             	return false;
            }

            $string = '§cFailed to read the team';

            if ($this->isAlive()) {
             	if ($team->getName() ==$this->getTeam()) {
             		$string = "§cThis Is Your bed, Protect It";
            	} else {
        	    	$string = "§cThis Is The Enemy's bed, Destroy It!";
            	}
            } else {
            	$string = "§cBed destroyed!";
            }

			$pk = new AddPlayerPacket();
			$pk->uuid = $this->getUniqueId();
			$pk->username = $this->getName();
			$pk->eid = $this->getId();
			$pk->x = $this->x;
			$pk->y = $this->y;
			$pk->z = $this->z;
			$pk->speedX = $this->motionX;
			$pk->speedY = $this->motionY;
			$pk->speedZ = $this->motionZ;
			$pk->yaw = $this->yaw;
			$pk->pitch = $this->pitch;
			$pk->item = $this->getInventory()->getItemInHand();
			$pk->metadata = array (
					self::DATA_FLAGS => array (self::DATA_TYPE_BYTE, 1 << self::DATA_FLAG_INVISIBLE),
					self::DATA_NAMETAG => array (self::DATA_TYPE_STRING, $string),
					self::DATA_NO_AI => array (self::DATA_TYPE_BYTE, 1),
					self::DATA_LEAD_HOLDER => array (self::DATA_TYPE_LONG, -1),
					self::DATA_LEAD => array (self::DATA_TYPE_BYTE, 0)
	            );
			$player->dataPacket($pk);

			$this->sendLinkedData();
			$this->inventory->sendArmorContents($player);
		}
	}
}
?>