<?php

namespace BanSystem\Command;

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

class BanIPCommand extends Command {
    
    public function __construct() {
        parent::__construct("ban-ip");
        $this->description = "Выдать игроку бан по айпи адресу";
        $this->usageMessage  = "/ban-ip <Player/IP> <Reason>";
        $this->setPermission("bansystem.command.banip");
    }
    
    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()->getIPBans();
            if ($banList->isBanned($args[0])) {
                $sender->sendMessage(Translation::translate("ipAlreadyBanned"));
                return false;
            }
            $ip = filter_var($args[0], FILTER_VALIDATE_IP);
            $player = $sender->getServer()->getPlayer($args[0]);
            if (count($args) == 1) {
                if ($ip != null) {
                    $banList->addBan($ip, null, null, $sender->getName());
                    foreach ($sender->getServer()->getOnlinePlayers() as $onlinePlayers) {
                        if ($onlinePlayers->getAddress() == $ip) {
                            $onlinePlayers->kick("§fYour §eIP-Address §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 IP: §e".$ip."§f!");
$vk->sendMessage(1, "[{$date}] Адрес " . $ip . " забанен по IP от руки " . $sender->getName() . " навсегда! Причина: не имеется");
                } else {
                    if ($player != null) {
                        $banList->addBan($player->getAddress(), null, null, $sender->getName(),$player->getName());
                        $player->kick("§fYour §eIP-Address §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 IP: §e".$player->getName()."§f!");
$vk->sendMessage(1, "[{$date}] Игрок " . $player->getName() . " забанен по IP от руки " . $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 ($ip != null) {
                    $sender->getServer()->getIPBans()->addBan($ip, $reason, null, $sender->getName());
                    foreach ($sender->getServer()->getOnlinePlayers() as $players) {
                        if ($players->getAddress() == $ip) {
                            $players->kick("§fYour §eIP-Address §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 IP: §e".$ip."§f!");
$vk->sendMessage(1, "[{$date}] Адрес " . $ip . " забанен по IP от руки " . $sender->getName() . " навсегда! Причина: " . $reason . "!");
                } else {
                    if ($player != null) {
                        $banList->addBan($player->getAddress(), $reason, null, $sender->getName(),$player->getName());
                        $player->kick("§fYour §eIP-Address §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 IP: §e".$player->getName()."§f!");
$vk->sendMessage(1, "[{$date}] Игрок " . $player->getName() . " забанен по IP от руки " . $sender->getName() . " навсегда! Причина: ". $reason . "!"); 
                    } else {
                        $sender->sendMessage(Translation::translate("playerNotFound"));
                    }
                }
            } else {
                $sender->sendMessage(Translation::translate("noPermission"));
            }
        }
        return true;
    }
}