<?php

/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 01.11.2016
 * Time: 5:40
 */
class ResetMasterSkillTree extends CharacterModule
{
    public $Config;

    public function __construct()
    {
        parent::__construct();
        $this->Config = App::$Configs->mod_ResetMasterSkillTree;
    }

    public function ModulePage()
    {
        $ReturnArr = array();

        $ReturnArr['Chars'] = Character::GetListCharsInAccount($_SESSION['login']);
        $ReturnArr['ActivePlayer'] = $_SESSION['ActivePlayer'];
        $ReturnArr['Config'] = $this->Config;
        $ReturnArr['Valutes'] = json_decode(file_get_contents("configs/DB_" . $_SESSION['user_db'] . "/ValuteManager.json"), true);
        $ReturnArr['CharacterValutes'] = ValuteManager::GetAllValutes($_SESSION['user_db']);
        return $ReturnArr;
    }

    public function Reset($Nick)
    {
        $Nick = App::Validate($Nick);
        if ($Nick == '') {
            Logs::WriteLog("Module: [ResetMasterSkillTree], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Fail reset (Nick incorrect)", 'Modules.log');
            Messages::Add('fail_reset_stats');
            return false;
        }
        if ($this->AccountOnline) {
            return false;
        }
        if (!in_array($Nick, Character::GetListCharsInAccount($_SESSION['login']))) {
            Logs::WriteLog("Module: [ResetMasterSkillTree], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Fail reset (Nick excluded to account)", 'Modules.log');
            Messages::Add('fail_reset_stats');
            return false;
        }
        if (!$this->Config['Enable']) {
            Messages::Add('disable_module');
            return false;
        }

        $Character = Character::GetActivePlayerInfo($Nick);
        $Character = $Character[0];

        if ($Character['cLevel'] < $this->Config['NeedLevel']) {
            Messages::Add('min_level_reset_master_skill_tree');
            Logs::WriteLog("Module: [ResetMasterSkillTree], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Fail reset (not enough levels)", 'Modules.log');
            return false;
        }
        if ($Character[App::$DBs[$_SESSION['user_db']]->column_names['Resets']] < $this->Config['NeedReset']) {
            Messages::Add('min_resets_reset_master_skill_tree');
            Logs::WriteLog("Module: [ResetMasterSkillTree], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Fail reset (not enough resets)", 'Modules.log');
            return false;
        }
        if ($Character[App::$DBs[$_SESSION['user_db']]->column_names['GrandResets']] < $this->Config['NeedGrandReset']) {
            Messages::Add('min_grand_resets_reset_master_skill_tree');
            Logs::WriteLog("Module: [ResetMasterSkillTree], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Fail reset (not enough grand resets)", 'Modules.log');
            return false;
        }

        if (ValuteManager::CheckFive($this->Config['Price']['Valutes'][0], $this->Config['Price']['Valutes'][1], $this->Config['Price']['Valutes'][2], $this->Config['Price']['Valutes'][3], $this->Config['Price']['Valutes'][4])) {
            if (App::$DBs[$_SESSION['user_db']]->set('ML_POINT', "MASTER_LEVEL", false)->set('ML_EXP', 0)->set('ML_NEXTEXP', 35507050)->set('MASTER_LEVEL', 1)->where('CHAR_NAME', $Nick)->update('T_SkillTree_Info') &&
                App::$DBs[$_SESSION['user_db']]->where('Name', $Nick)->set('MagicList', 'NULL', false)->update('Character')

            ) {
                ValuteManager::TakeFive($this->Config['Price']['Valutes'][0], $this->Config['Price']['Valutes'][1], $this->Config['Price']['Valutes'][2], $this->Config['Price']['Valutes'][3], $this->Config['Price']['Valutes'][4]);
                Logs::WriteLog("Module: [ResetMasterSkillTree], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Success reset Master Skill Tree", 'Modules.log');
                Messages::Add('success_reset_master_skill_tree');
                return true;
            } else {
                Logs::WriteLog("Module: [ResetMasterSkillTree], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Fail reset Master Skill Tree, Error in Query", 'Modules.log');
                Messages::Add('fail_reset_master_skill_tree');
                return false;
            }
        }
    }

    public function CreateConfig()
    {
        $Config['Enable'] = true;
        $Config['NeedLevel'] = 300;
        $Config['NeedReset'] = 10;
        $Config['NeedGrandReset'] = 0;

        $Config["Price"]["Valutes"][0] = "0";
        $Config["Price"]["Valutes"][1] = "1";
        $Config["Price"]["Valutes"][2] = "2";
        $Config["Price"]["Valutes"][3] = "3";
        $Config["Price"]["Valutes"][4] = "4";

        $text = json_encode($Config);
        file_put_contents("configs/modules/DB_0/ResetMasterSkillTree.json", $text);
    }


    public static function ConfigResetMasterSkillTree($Db)
    {
        $Config = file_get_contents("configs/modules/DB_" . $Db . "/ResetMasterSkillTree.json");
        $Config = json_decode($Config, true);
        if (property_exists(App::$Post, 'Enable')) {
            $Config['Enable'] = true;
        } else {
            $Config['Enable'] = false;
        }
        if (property_exists(App::$Post, 'NeedLevel'))
            $Config['NeedLevel'] = App::$Post->NeedLevel;
        if (property_exists(App::$Post, 'NeedReset'))
            $Config['NeedReset'] = App::$Post->NeedReset;
        if (property_exists(App::$Post, 'NeedGrandReset'))
            $Config['NeedGrandReset'] = App::$Post->NeedGrandReset;

        $ValuteConfig = file_get_contents("configs/DB_" . $Db . "/ValuteManager.json");
        $ValuteConfig = json_decode($ValuteConfig, true);
        for ($i = 0; $i < 5; $i++) {
            if ($ValuteConfig[$i]['Name'] != '' && property_exists(App::$Post, 'val_take_' . $i)) {
                $Config['Price']['Valutes'][$i] = App::$Post->{'val_take_' . $i};
            } else {
                $Config['Price']['Valutes'][$i] = 0;
            }
        }

        file_put_contents('configs/modules/DB_' . $Db . '/ResetMasterSkillTree.json', json_encode($Config));

    }
}