<?php

namespace BanSystem\Command;

use BanSystem\Translation\Translation;
use BanSystem\Util\date\Countdown;
use DateTime;
use InvalidArgumentException;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\utils\TextFormat;

class TempBanCommand extends Command {
    
    public function __construct() {
        parent::__construct("tempban");
        $this->description = "Забанить игрока по нику на время";
        $this->usageMessage = "/tempban <Player> <Time> <Reason>";
        $this->setPermission("bansystem.command.tempban");
    }
    
    public function execute(CommandSender $sender, $commandLabel, array $args) {
    $date = date("d.m.y");
        if ($this->testPermissionSilent($sender)) {
            if (count($args) <= 1) {
                $sender->sendMessage(Translation::translateParams("usage", array($this)));
                return false;
            }
$vk = $sender->getServer()->getPluginManager()->getPlugin("VK");

            $player = $sender->getServer()->getPlayer($args[0]);
            $playerName = $args[0]; 
            $banList = $sender->getServer()->getNameBans();
            try {
                if ($banList->isBanned($args[0])) {
                    $sender->sendMessage(Translation::translate("playerAlreadyBanned"));
                    return false;
                }
                $expiry = new Countdown($args[1]);
                $expiryToString = Countdown::expirationTimerToString($expiry->getDate(), new DateTime());
                if (count($args) == 2) {
                    if ($player != null) {

                        $banList->addBan($player->getName(), null, $expiry->getDate(), $sender->getName());
                        $player->kick("§fYour §enickname §fwas temporarily §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$sender->getName()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§bnot stated\n§fUnban:§b ".$expiryToString."", false);
$playerName = $player->getName();
                    } else {
                        $banList->addBan($args[0], null, $expiry->getDate(), $sender->getName());
                    }
                     $sender->sendMessage("§a» §fYou are §asuccessfully §fbanned player's nickname: §e".$playerName."§f!");
$vk->sendMessage(1, "[{$date}] Игрок " . $playerName . " забанен по нику от руки " . $sender->getName() . " на " . $expiryToString . "! Причина: не имеется");

                    
                } else if (count($args) >= 3) {
                    $banReason = "";
                    for ($i = 2; $i < count($args); $i++) {
                        $banReason .= $args[$i];
                        $banReason .= " ";
                    }
                    $banReason = substr($banReason, 0, strlen($banReason) - 1);
                    if ($player != null) {
                        $banList->addBan($player->getName(), $banReason, $expiry->getDate(), $sender->getName());
                        $player->kick("§fYour §enickname §fwas temporarily §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$sender->getName()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b ".$banReason."\n§fUnban:§b ".$expiryToString."", false);
$playerName = $player->getName();
                    } else {
                        $banList->addBan($args[0], $banReason, $expiry->getDate(), $sender->getName());
                    }
                     $sender->sendMessage("§a» §fYou are §asuccessfully §fbanned player's nickname: §e".$playerName."§f!");
$vk->sendMessage(1, "[{$date}] Игрок " . $playerName . " забанен по нику от руки " . $sender->getName() . " на " . $expiryToString . "! Причина: " . $banReason . "!");
                }
            } catch (InvalidArgumentException $e) {
                $sender->sendMessage($e->getMessage());
            }
        } else {
            $sender->sendMessage(Translation::translate("noPermission"));
        }
        return true;
    }
}