<?php

namespace BanSystem\Command;

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

class TempMuteCommand extends Command {
    
    public function __construct() {
        parent::__construct("tempmute");
        $this->description = "Замутить игрока по нику на время";
        $this->usageMessage = "/tempmute <Player> <Time> <Reason>";
        $this->setPermission("bansystem.command.tempmute");
    }
    
    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 = Manager::getNameMutes();
            try {
                if ($banList->isBanned($args[0])) {
                    $sender->sendMessage(Translation::translate("playerAlreadyMuted"));
                    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->sendMessage("§c» §fYou have been §etemporarily §fmuted§f!\n§c» §fBy: §b" . $sender->getName() . " §7| §fReason: §bnot stated\n§c» §fUnmute: §b".$expiryToString."");
$playerName = $player->getName();
                    } else {
                        $banList->addBan($args[0], null, $expiry->getDate(), $sender->getName());
                    }
                     $sender->sendMessage("§a» §fYou are §asuccessfully §fmuted player:§e ".$playerName."");
$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->sendMessage("§c» §fYou have been §etemporarily §fmuted§f!\n§c» §fBy: §b" . $sender->getName() . " §7| §fReason: §b".$banReason."\n§c» §fUnmute:§b ".$expiryToString."", false);
$playerName = $player->getName();
                    } else {
                        $banList->addBan($args[0], $banReason, $expiry->getDate(), $sender->getName());
                    }
                     $sender->sendMessage("§a» §fYou are §asuccessfully §fmuted player:§e ".$playerName."");
$vk->sendMessage(1, "[{$date}] Игрок " . $playerName . " заблокирован в чате от руки " . $sender->getName() . " на " . $expiryToString . "! Причина: " . $banReason . "!");
                }
            } catch (InvalidArgumentException $e) {
                $sender->sendMessage($e->getMessage());
            }
        } else {
            $sender->sendMessage(Translation::translate("noPermission"));
        }
        return true;
    }
}