<?php

namespace BanSystem\BSListener;

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

class PlayerChatListener implements Listener {
    
    public function onPlayerChat(PlayerChatEvent $event) {
        $player = $event->getPlayer();
        $muteList = Manager::getNameMutes();
        if ($muteList->isBanned($player->getName())) {
            $entries = $muteList->getEntries();
            $entry = $entries[strtolower($player->getName())];
            $muteMessage = "";
            if ($entry->getExpires() == null) {
                $reason = $entry->getReason();
                if ($reason != null || $reason != "") {
                    $muteMessage = "§c» §fYou have been §epermanently §fmuted§f!\n§c» §fBy: §b" . $entry->getSource() . " §7| §fReason: §b" . $reason . "";
                } else {
                    $muteMessage = "§c» §fYou have been §epermanently §fmuted!\n§c» §fBy: §b" . $entry->getSource() . " §7| §fReason: §bnot stated§f";
                }
} else {
                $expiry = Countdown::expirationTimerToString($entry->getExpires(), new DateTime());
$reason = $entry->getReason();
                if ($entry->hasExpired()) {
                    $muteList->remove($entry->getName());
                    return;
                }
                $banReason = $entry->getReason();
                if ($banReason != null || $banReason != "") {
                    $muteMessage = "§c» §fYou have been §etemporarily §fmuted§f!\n§c» §fBy: §b" . $entry->getSource() . " §7| §fReason: §b".$reason."\n§c» §fUnmute: §b".$expiry."";
                } else {
                    $muteMessage = "§c» §fYou have been §etemporarily §fmuted§f!\n§c» §fBy: §b" . $entry->getSource() . " §7| §fReason: §bnot stated\n§c» §fUnmute: §b".$expiry."";
                }
            }
            $event->setCancelled(true);
            $player->sendMessage($muteMessage);
        }
}
}