<?php

namespace BanSystem\Command;

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

class BanCommand extends Command {
    
    public function __construct() {
        parent::__construct("ban");
        $this->description = "Выдать игроку бан по нику";
        $this->usageMessage = "/ban <Player> <Reason>";
        $this->setPermission("bansystem.command.ban");
    }
    
    public function execute(CommandSender $sender, $label, array $args) {
    $date = date("d.m.y");
        if ($this->testPermissionSilent($sender)) {
            if (count($args) <= 0) {
                $sender->sendMessage(Translation::translateParams("usage", array($this)));
                return false;
            }
            $player = $sender->getServer()->getPlayer($args[0]);
            $banList = $sender->getServer()->getNameBans();
$vk = $sender->getServer()->getPluginManager()->getPlugin("VK");
            $playerName = $args[0];
            if ($banList->isBanned($args[0])) {
                $sender->sendMessage(Translation::translate("playerAlreadyBanned"));
                return false;
            }
            if (count($args) == 1) {
                if ($player != null) {
                    $banList->addBan($player->getName(), null, null, $sender->getName());
                    $player->kick("§fYour §enickname §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$sender->getName()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b not stated", false);
$playerName = $player->getName();
                } else {
                    $banList->addBan($args[0], null, null, $sender->getName());
                }
                 $sender->sendMessage("§a» §fYou are §asuccessfully §fbanned player's nickname: §e".$playerName. "§f!");
$vk->sendMessage(1, "[{$date}] Игрок " . $playerName . " забанен по нику от руки " . $sender->getName() . " навсегда! Причина: не имеется");
            } 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 ($player != null) {
                    $banList->addBan($player->getName(), $reason, null, $sender->getName());
                    $player->kick("§fYour §enickname §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$sender->getName()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b ".$reason."", false);
                    $playerName = $player->getName();
                } else {
                    $banList->addBan($args[0], $reason, null, $sender->getName());
                }
                 $sender->sendMessage("§a» §fYou are §asuccessfully §fbanned player's nickname: §e".$playerName."§f!");

$vk->sendMessage(1, "[{$date}] Игрок " . $playerName . " забанен по нику от руки " . $sender->getName() . " навсегда! Причина: " . $reason . "!");
            }
        } else {
            $sender->sendMessage(Translation::translate("noPermission"));
        }
        return true;
    }
}