<?php

namespace Plugin\Commands;

use pocketmine\permission\Permission;
use pocketmine\command\Command;
use pocketmine\command\CommandExecutor;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\utils\Config;

use pocketmine\utils\TextFormat;

use Plugin\ServerAuth;

class ChangePassword implements CommandExecutor {

	public function __construct(ServerAuth $plugin){
        $this->plugin = $plugin;
    }
    
    public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) {
    	$fcmd = strtolower($cmd->getName());
    	switch($fcmd){
    		case "changepassword":
    			if($sender->hasPermission("serverauth.changepassword")){
    				$cfg = $this->plugin->getConfig()->getAll();
    				if($sender instanceof Player){
    					if($cfg["changepassword"]["enabled"]){
    						if(ServerAuth::getAPI()->isPlayerAuthenticated($sender)){
    							if($cfg["changepassword"]["password-confirm-required"]){
    								if(count($args) == 2){
    									if($args[0] == $args[1]){
    										$status = ServerAuth::getAPI()->changePlayerPassword($sender, $args[0]);
    										if($status == ServerAuth::SUCCESS){
    											$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["changepassword-success"]));
    										}elseif($status == ServerAuth::ERR_USER_NOT_REGISTERED){
    											$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["user-not-registered"]));
    										}elseif($status == ServerAuth::ERR_PASSWORD_TOO_SHORT){
    											$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["password-too-short"]));
    										}elseif($status == ServerAuth::ERR_PASSWORD_TOO_LONG){
    											$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["password-too-long"]));
    										}elseif($status == ServerAuth::CANCELLED){
    											$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . ServerAuth::getAPI()->getCancelledMessage()));
    										}else{
    											$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["generic"]));
    										}
    									}else{
    										$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["password-no-match"]));
    									}
    								}else{
    									$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["command-conf"]));
    								}
    							}else{
    								if(count($args) == 1){
    									$status = ServerAuth::getAPI()->changePlayerPassword($sender, $args[0]);
    									if($status == ServerAuth::SUCCESS){
    										$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["changepassword-success"]));
    									}elseif($status == ServerAuth::ERR_USER_NOT_REGISTERED){
    										$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["changepassword-success"]));
    									}elseif($status == ServerAuth::ERR_PASSWORD_TOO_SHORT){
    										$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["password-too-short"]));
    									}elseif($status == ServerAuth::ERR_PASSWORD_TOO_LONG){
    										$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["password-too-long"]));
    									}elseif($status == ServerAuth::CANCELLED){
    										$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . ServerAuth::getAPI()->getCancelledMessage()));
    									}else{
    										$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["generic"]));
    									}
    								}else{
    									$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["command"]));
    								}
    							}
    						}else{
    							$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["login-required"]));
    						}
    					}else{
    						$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["disabled"]));
    					}
    					break;
    				}else{
    					if(count($args) == 2){
    						$status = ServerAuth::getAPI()->changePlayerPassword($this->plugin->getServer()->getOfflinePlayer($args[0]), $args[1]);
    						if($status == ServerAuth::SUCCESS){
    							$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . ServerAuth::getAPI()->replaceArrays($this->plugin->chlang["changepassword"]["changepassword-cons-success"], array("PLAYER" => $args[0], "PASSWORD" => $args[1]))));
    						}elseif($status == ServerAuth::ERR_USER_NOT_REGISTERED){
    							$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["user-not-registered-3rd"]));
    						}elseif($status == ServerAuth::ERR_PASSWORD_TOO_SHORT){
    							$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["password-too-short"]));
    						}elseif($status == ServerAuth::ERR_PASSWORD_TOO_LONG){
    							$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["password-too-long"]));
    						}elseif($status == ServerAuth::CANCELLED){
    							$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . ServerAuth::getAPI()->getCancelledMessage()));
    						}else{
    							$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["errors"]["generic"]));
    						}
    					}else{
    						$sender->sendMessage($this->plugin->translateColors("&", $cfg["prefix"] . $this->plugin->chlang["changepassword"]["command-cons"]));
    					}
    					break;
    				}
    			}else{
    				$sender->sendMessage($this->plugin->translateColors("&", $this->plugin->chlang["errors"]["no-permissions"]));
    				break;
    			}
    			return true;
    			}
    	}
}
?>