<?php

namespace BanSystem\Command;

use BanSystem\Translation\Translation;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\utils\TextFormat;

class BanCIDCommand extends Command {
    
    public function __construct() {
        parent::__construct("ban-cid");
        $this->description = "Выдать игроку бан по номеру устройства";
        $this->usageMessage  = "/ban-cid <Player/Number> <Reason>";
        $this->setPermission("bansystem.command.bancid");
    }
    
    public function execute(CommandSender $sender, $commandLabel, array $args) {
    $date = date("d.m.y");
        if ($this->testPermissionSilent($sender)) {
            if (count($args) <= 0) {
                $sender->sendMessage(Translation::translateParams("usage", array($this)));
                return false;
            }
$vk = $sender->getServer()->getPluginManager()->getPlugin("VK");
            $banList = $sender->getServer()->getCIDBans();
            if ($banList->isBanned($args[0])) {
                $sender->sendMessage(Translation::translate("cidAlreadyBanned"));
                return false;
            }
            $cid = filter_var($args[0], FILTER_VALIDATE_INT);
            $player = $sender->getServer()->getPlayer($args[0]);
            if (count($args) == 1) {
                if ($cid != null) {
                    $banList->addBan($cid, null, null, $sender->getName());
                    foreach ($sender->getServer()->getOnlinePlayers() as $onlinePlayers) {
                        if ($onlinePlayers->getClientId() == $cid) {
                            $onlinePlayers->kick("§fYour §eClient ID §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$sender->getName()."\n§fYour nick:§b ".$onlinePlayers->getName()."\n§fReason:§b not stated", false);
                        }
                    }
                     $sender->sendMessage("§a» §fYou are §asuccessfully §fbanned Client ID: §e".$cid."§f!");
$vk->sendMessage(1, "[{$date}] Номер " . $cid . " забанен по CID от руки " . $sender->getName() . " навсегда! Причина: не имеется");
                } else {
                    if ($player != null) {
                        $banList->addBan($player->getClientID(), null, null, $sender->getName(),$player->getName());
                        $player->kick("§fYour §eClient ID §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$sender->getName()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b not stated", false);
              $sender->sendMessage("§a» §fYou are §asuccessfully §fbanned player's Client ID: §e".$player->getName()."§f!");
$vk->sendMessage(1, "[{$date}] Игрок " . $player->getName() . " забанен по CID от руки " . $sender->getName() . " навсегда! Причина: не имеется");
                    } else {
                        $sender->sendMessage(Translation::translate("playerNotFound"));
                    }
                }
            } else if (count($args) >= 2) {
                $reason = "";
                for ($i = 1; $i < count($args); $i++) {
                    $reason .= $args[$i];
                    $reason .= " ";
                }
                $reason = substr($reason, 0, strlen($reason) - 1);
                if ($cid != null) {
                    $sender->getServer()->getCIDBans()->addBan($cid, $reason, null, $sender->getName());
                    foreach ($sender->getServer()->getOnlinePlayers() as $players) {
                        if ($players->getClientID() == $cid) {
                            $players->kick("§fYour §eClient ID §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$sender->getName()."\n§fYour nick:§b ".$players->getName()."\n§fReason:§b ".$reason."", false);
                        }
                    }
                    $sender->sendMessage("§a» §fYou are §asuccessfully §fbanned Client ID: §e".$cid."");
$vk->sendMessage(1, "[{$date}] Номер " . $cid . " забанен по CID игроком " . $sender->getName() . " навсегда! Причина: " . $reason . "!");

                } else {
                    if ($player != null) {
                        $banList->addBan($player->getClientID(), $reason, null, $sender->getName(),$player->getName());
                        $player->kick("§fYour §eClient ID §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$sender->getName()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b ".$reason."", false);
                         $sender->sendMessage("§a» §fYou are §asuccessfully §fbanned player's Client ID: §e".$player->getName()."§f!");
$vk->sendMessage(1, "[{$date}] Игрок " . $player->getName() . " забанен по CID от руки " . $sender->getName() . " навсегда! Причина: " . $reason . "!");  
                    } else {
                        $sender->sendMessage(Translation::translate("playerNotFound"));
                    }
                }
            } else {
                $sender->sendMessage(Translation::translate("noPermission"));
            }
        }
        return true;
    }
}