<?php

namespace BanSystem\BSListener;

use BanSystem\Util\date\Countdown;
use DateTime;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerPreLoginEvent;
use pocketmine\utils\TextFormat;

class PlayerPreLoginListener implements Listener {
    
    public function onPlayerPreLogin(PlayerPreLoginEvent $event) {
        $player = $event->getPlayer();
        $banList =  $player->getServer()->getNameBans();
        if ($banList->isBanned(strtolower($player->getName()))) {
            $kickMessage = "";
            $banEntry = $banList->getEntries();
            $entry = $banEntry[strtolower($player->getName())];
            if ($entry->getExpires() == null) {
                $reason = $entry->getReason();
                if ($reason != null || $reason != "") {
                    $kickMessage = "§fYour §enickname §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$entry->getSource()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b ".$reason."";
                } else {
                    $kickMessage = "§fYour §enickname §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$entry->getSource()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b ".$reason."";
                }
            } else {
                $expiry = Countdown::expirationTimerToString($entry->getExpires(), new DateTime());
                if ($entry->hasExpired()) {
                    $banList->remove($entry->getName());
                    return;
                }
                $banReason = $entry->getReason();
                if ($banReason != null || $banReason != "") {
                    $kickMessage = "§fYour §enickname §fwas temporarily §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$entry->getSource()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b ".$banReason."\n§fUnban:§b ".$expiry."";
                } else {
                    $kickMessage = "§fYour §enickname §fwas temporarily §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$entry->getSource()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b not stated\n§fUnban:§b ".$expiry."";
                }
            }
            $player->close("", $kickMessage);
        }
    }
    
    public function onPlayerPreLogin2(PlayerPreLoginEvent $event) {
        $player = $event->getPlayer();
        $banList = $player->getServer()->getIPBans();
        if ($banList->isBanned(strtolower($player->getAddress()))) {
            $kickMessage = "";
            $banEntry = $banList->getEntries();
            $entry = $banEntry[strtolower($player->getAddress())];
            if ($entry->getExpires() == null) {
                $reason = $entry->getReason();
                if ($reason != null || $reason != "") {
                    $kickMessage = "§fYour §eIP-Address §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy:§b ".$entry->getSource()."\n§fYour nick: §b".$player->getName()."\n§fReason:§b ".$reason."";
                } else {
                    $kickMessage = "§fYour §eIP-Address §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy:§b ".$entry->getSource()."\n§fYour nick:§b ".$player->getName()."\n§fReason: §bnot stated";
                }
            }
            $player->close("", $kickMessage);
        }
   }
public function onPlayerPreLogin3(PlayerPreLoginEvent $event) {
        $player = $event->getPlayer();
        $banList = $player->getServer()->getCIDBans();
        if ($banList->isBanned(strtolower($player->getClientId()))) {
            $kickMessage = "";
            $banEntry = $banList->getEntries();
            $entry = $banEntry[strtolower($player->getClientId())];
            if ($entry->getExpires() == null) {
                $reason = $entry->getReason();
                if ($reason != null || $reason != "") {
                    $kickMessage = "§fYour §eClient ID §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$entry->getSource()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b ".$reason."";
                } else {
                    $kickMessage = "§fYour §eClient ID §fwas permanently §cbanned §ffrom §bDragonfly\n\n§fBy: §b".$entry->getSource()."\n§fYour nick:§b ".$player->getName()."\n§fReason:§b not stated";
                }
            }
         $player->close("", $kickMessage);
       }
}
}