<?php
    namespace shp;
use pocketmine\plugin\PluginBase;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\math\Vector3;
use pocketmine\inventory\ChestInventory;
use pocketmine\inventory\Inventory;
use onebone\economyapi\EconomyAPI;
use _64FF00\PurePerms\PurePerms;

class nigg extends PluginBase {

    private $eco;
    private $pp;
    private $players;

    public function onEnable(){
        $this->saveDefaultConfig();

        if(!file_exists($this->getDataFolder()."players.yml")){
            file_put_contents($this->getDataFolder()."players.yml", yaml_emit(["players" => []]));
        }

        $this->players = yaml_parse_file($this->getDataFolder()."players.yml");

        $this->eco = EconomyAPI::getInstance();
        $this->pp = $this->getServer()->getPluginManager()->getPlugin("PurePerms");

        if(!$this->pp instanceof PurePerms){
            $this->getServer()->getPluginManager()->disablePlugin($this);
        }
    }

    public function onDisable(){
        yaml_emit_file($this->getDataFolder()."players.yml", $this->players);
    }

    private function savePlayers(){
        yaml_emit_file($this->getDataFolder()."players.yml", $this->players);
    }

    public function onCommand(CommandSender $sender, Command $command, $label, array $args){
      
        if(!$sender instanceof Player){
            return true;
        }
$inv=$sender->getInventory();
        $name = strtolower($sender->getName());

        if(!isset($this->players["players"][$name])){
            $this->players["players"][$name] = [];
        }

        if(!isset($args[0])){
        	$sender->sendMessage("§bBlue§aGames §6RankShop:");
            $sender->sendMessage("§6/rankshop list");
            $sender->sendMessage("§6/rankshop myranks");
            $sender->sendMessage("§6/rankshop buy <rank>");
            $sender->sendMessage("§6/rankshop switch <rank>");
            return true;
        }

        switch(strtolower($args[0])){

            case "list":
                foreach($this->getConfig()->get("ranks") as $rank => $price){
                    $sender->sendMessage("§e$rank §7- §a$price");
                }
                return true;

            case "myranks":
                if(empty($this->players["players"][$name])){
                    $sender->sendMessage("§8(§6RS§8) §cYou own no ranks.");
                    return true;
                }
                $sender->sendMessage("§6Your ranks:");
                foreach($this->players["players"][$name] as $r){
                    $sender->sendMessage("§e- $r");
                }
                return true;

            case "buy":
                if(!isset($args[1])) return true;
                $rank = $args[1];
$group = $this->pp->getGroup($rank);
                if(in_array($rank, $this->players["players"][$name])){
                    
                    $this->pp->setGroup($sender, $group);                return true;
                }

                $ranks = $this->getConfig()->get("ranks");
               if(!isset($ranks[$rank])){
                   
                    $sender->sendMessage("§8(§6RS§8) §cRank not for sale.");
                    return true;
                }

                $price = $ranks[$rank];
                if($this->eco->myMoney($sender) < $price){
                
                    $sender->sendMessage("§8(§6RS§8) §cNot enough money.");
                    return true;
                }

                
                if($group === null){
                    $sender->sendMessage("§8(§6RS§8) §cRank group missing.");
                    return true;
                }

                $this->eco->reduceMoney($sender, $price);
                $this->players["players"][$name][] = $rank;
                $this->pp->setGroup($sender, $group);
              
                $sender->sendMessage("§8(§6RS§8) §aYou purchased §e$rank!");
                return true;

            case "switch":
                if(!isset($args[1])) return true;
                $rank = $args[1];

                if(!in_array($rank, $this->players["players"][$name])){
                    $sender->sendMessage("§8(§6RS§8) §cYou do not own this rank.");
                    return true;
                }

                $group = $this->pp->getGroup($rank);
                if($group === null){
                    $sender->sendMessage("§8(§6RS§8) §cRank group missing.");
                    return true;
                }

                $this->pp->setGroup($sender, $group);
                $sender->sendMessage("§8(§6RS§8) §aSwitched to §e$rank");
                return true;
        }

        return true;
    }
}