<?php

/*
 *
 *  ____            _        _   __  __ _                  __  __ ____
 * |  _ \ ___   ___| | _____| |_|  \/  (_)_ __   ___      |  \/  |  _ \
 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
 * |  __/ (_) | (__|   <  __/ |_| |  | | | | | |  __/_____| |  | |  __/
 * |_|   \___/ \___|_|\_\___|\__|_|  |_|_|_| |_|\___|     |_|  |_|_|
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * @author PocketMine Team
 * @link http://www.pocketmine.net/
 *
 *
*/

namespace pocketmine;


use pocketmine\metadata\MetadataValue;
use pocketmine\nbt\tag\Compound;
use pocketmine\plugin\Plugin;

class OfflinePlayer implements IPlayer{

	private $name;
	private $server;
	private $namedtag;

	/**
	 * @param Server $server
	 * @param string $name
	 */
	public function __construct(Server $server, $name){
		$this->server = $server;
		$this->name = $name;
		if(\file_exists($this->server->getDataPath() . "players/" . \strtolower($this->getName()) . ".dat")){
			$this->namedtag = $this->server->getOfflinePlayerData($this->name);
		}else{
			$this->namedtag = \null;
		}
	}

	public function isOnline(){
		return $this->getPlayer() !== \null;
	}

	public function getName(){
		return $this->name;
	}

	public function getServer(){
		return $this->server;
	}

	public function isOp(){
		return $this->server->isOp(\strtolower($this->getName()));
	}

	public function setOp($value){
		if($value === $this->isOp()){
			return;
		}

		if($value === \true){
			$this->server->addOp(\strtolower($this->getName()));
		}else{
			$this->server->removeOp(\strtolower($this->getName()));
		}
	}

	public function isBanned(){
		return $this->server->getNameBans()->isBanned(\strtolower($this->getName()));
	}

	public function setBanned($value){
		if($value === \true){
			$this->server->getNameBans()->addBan($this->getName(), \null, \null, \null);
		}else{
			$this->server->getNameBans()->remove($this->getName());
		}
	}

	public function isWhitelisted(){
		return $this->server->isWhitelisted(\strtolower($this->getName()));
	}

	public function setWhitelisted($value){
		if($value === \true){
			$this->server->addWhitelist(\strtolower($this->getName()));
		}else{
			$this->server->removeWhitelist(\strtolower($this->getName()));
		}
	}

	public function getPlayer(){
		return $this->server->getPlayerExact($this->getName());
	}

	public function getFirstPlayed(){
		return $this->namedtag instanceof Compound ? $this->namedtag["firstPlayed"] : \null;
	}

	public function getLastPlayed(){
		return $this->namedtag instanceof Compound ? $this->namedtag["lastPlayed"] : \null;
	}

	public function hasPlayedBefore(){
		return $this->namedtag instanceof Compound;
	}

	public function setMetadata($metadataKey, MetadataValue $metadataValue){
		$this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $metadataValue);
	}

	public function getMetadata($metadataKey){
		return $this->server->getPlayerMetadata()->getMetadata($this, $metadataKey);
	}

	public function hasMetadata($metadataKey){
		return $this->server->getPlayerMetadata()->hasMetadata($this, $metadataKey);
	}

	public function removeMetadata($metadataKey, Plugin $plugin){
		$this->server->getPlayerMetadata()->removeMetadata($this, $metadataKey, $plugin);
	}


}
