<?php

namespace Lunarelly\Events;

use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\Server;
use pocketmine\Player;
use pocketmine\event\player\{PlayerChatEvent, PlayerJoinEvent, PlayerRespawnEvent, PlayerCommandPreprocessEvent};
use PLevel\PlayerAddXpLevelEvent;
use _64FF00\PurePerms\event\PPGroupChangedEvent;
use Lunarelly\Main;

class Chat implements Listener {
	
	public $players = [];
	public $cplayers = [];

	public function __construct(Main $main) {
		$this->main = $main;
		}
		
		public function onChat(PlayerChatEvent $e) {
			    $p = $e->getPlayer();
				$msg = $e->getMessage();
				$this->sa = $this->main->getServer()->getPluginManager()->getPlugin("ServerAuth");
			    if(!$this->sa->getAPI()->isPlayerAuthenticated($p)) {
				$e->setCancelled();
				return;
				}
			    if(isset($this->players[$p->getName()])) {
			    if(time() - $this->players[$p->getName()] <= 1) {
			    $e->setCancelled();
			    $p->sendMessage("§8(§bB§aG§8) §fDont spam!, let others chat too.");
			    }
			    }
			    $this->players[$p->getName()] = time();
			    $symbols = array("§", "×", "₽", "•", "¶", "&", "®", "©", "™", "✓", "`", "√", "π", "÷", "∆");
			    foreach($symbols as $symbol) {
	            $badsymbols = strpos(mb_strtolower($msg), $symbol);
	            if($badsymbols !== false) {
			    $e->setCancelled();
			    $p->sendMessage("§8(§bB§aG§8) §fIncorrect symbols!");
			    }
			    }
			    $piarmsg = array("portalwix", "pw", "hylex", "hypixel", "mcpehost", "pvpcraft", "24main", "19132", "murcraft", "magnacube", "anonwix", "greenwix", "brokenlens", ".ru", ".ga", ".tk", ".ml", ". r u", ".com", ".net", "play.pvp", "s.pvp", "bufydrest", "modcraft", "craftmg", "24serv", "webjenkins", "server.pro", "ddns.net", "wasd", "colony", "wind.", "fr.", "kraken.", "17002", "17000", "k r a k e n", "m u r c r a f t", "flores", "florespvp", "f l o r e s", "m a g n a c u b e", "25565", "25566", ".gq", "cosmic", "23003", "c o s m i c", "castix", "castixpe", "hyworks", "mint.", "25569");
			    foreach($piarmsg as $piar){
	            $pr = strpos(mb_strtolower($msg), $piar);
	            if($pr !== false) {
			    $e->setCancelled();
			    $p->sendMessage("§8(§bB§aG§8) §fDon't promote other servers!");
			    }
				}
			    if($e->isCancelled()) {
				return;
				}
				$this->pp = $this->main->getServer()->getPluginManager()->getPlugin("PurePerms");
				$this->lvl = $this->main->getServer()->getPluginManager()->getPlugin("PLevel");
				$name = $p->getName();
				$group = $this->pp->getUserDataMgr()->getGroup($p)->getName();
				$level = $this->lvl->getLvl($p);
				if(strtolower($e->getMessage()) == "gg") {
				if($p->hasPermission("gapi.gg")) {
				$msg = "§bGood Game!";
				}
				}
					}
					
			       public function send($msg) {
                   $this->main->getServer()->broadcastMessage($msg);
				   }
					
					
					public function updateDisplay(Player $p) {
					   $this->pp = $this->main->getServer()->getPluginManager()->getPlugin("PurePerms");
			           $this->lvl = $this->main->getServer()->getPluginManager()->getPlugin("PLevel");
			           $name = $p->getName();
				       $group = $this->pp->getUserDataMgr()->getGroup($p)->getName();
				       $level = $this->lvl->getLvl($p);
					   if($group == "Player") {
						   $p->setDisplayName("§8(§b{$level}§8) §f{$name}");
						   }
					   if($group == "Premium") {
						   $p->setDisplayName("§8(§b{$level}§8) §9{$name}");
						   }
					   if($group == "Deluxe") {
						   $p->setDisplayName("§8(§b{$level}§8) §3{$name}");
						   }
					   if($group == "Platinum") {
						   $p->setDisplayName("§8(§b{$level}§8) §d{$name}");
						   }
					   if($group == "YouTube") {
						   $p->setDisplayName("§8(§b{$level}§8) §4{$name}");
						   }
					   if($group == "Warrior") {
						   $p->setDisplayName("§8(§b{$level}§8) §5{$name}");
						   }
					   if($group == "YouTube+") {
						   $p->setDisplayName("§8(§b{$level}§8) §4{$name}");
						   }
					   if($group == "Moderator") {
						   $p->setDisplayName("§8(§b{$level}§8) §e{$name}");
						   }
					   if($group == "Builder") {
						   $p->setDisplayName("§8(§b{$level}§8) §2{$name}");
						   }
					   if($group == "Admin") {
						   $p->setDisplayName("§8(§b{$level}§8) §c{$name}");
						   }
					   if($group == "Staff") {
						   $p->setDisplayName("§8(§b{$level}§8) §6{$name}");
						   }
					   if($group == "Owner") {
						   $p->setDisplayName("§8(§b{$level}§8) §a{$name}");
						   }
						   }

				    public function onCmd(PlayerCommandPreprocessEvent $e) {
				    	$p = $e->getPlayer();
						$msg = $e->getMessage();
			            if($msg[0] == "/") {
			            if(isset($this->cplayers[$p->getName()])) {
			            if(time() - $this->cplayers[$p->getName()] <= 2) {
			            $e->setCancelled();
			            $p->sendMessage("§8(§bB§aG§8) §fDont spam!, let others use commands too.");
			            }
			            }
			            }
			            $this->cplayers[$p->getName()] = time();
				        }
					}
				 ?>