<?php

/**
 * Created by PhpStorm.
 * User: MainUser
 * Date: 18.07.16
 * Time: 18:28
 */
class ResetStats extends CharacterModule
{  
    public function Reset($Nick)
    {
        $Nick = App::Validate($Nick);
        if ($this->AccountOnline)
            return false;
        if (!in_array($Nick, Character::GetListCharsInAccount($_SESSION['login']))) {
            Logs::WriteLog("Module: [ResetStats], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Fail reset (Nick excluded to account)", 'Modules.log');
            Messages::Add('fail_reset_stats');
            return false;
        }
        $Config = App::$Configs->mod_ResetStats;
        if ($Nick == '')
            $Nick = $_SESSION['ActivePlayer'];
        if (!$Config['Enable']){
            Messages::Add('disable_module');
            return false;
        }
        $Character = Character::GetActivePlayerInfo($Nick);
        $Character = $Character[0];
        if($Character['cLevel'] < $Config['MinLevel']){
            Messages::Add('min_level');
            return false;
        }
        if($Character[App::$DBs[$_SESSION['user_db']]->column_names['Resets']] < $Config['MinReset']){
            Messages::Add('min_reset');
            return false;
        }
        $isDl = false;
        if ($Character['Class'] == 64 || $Character['Class'] == 65 || $Character['Class'] == 66)
            $isDl = true;

        $SumStats = $Character['Strength'] + $Character['Dexterity'] + $Character['Vitality'] + $Character['Energy'] + $Character['LeaderShip'];

        $Query = App::$DBs[$_SESSION['user_db']]->set('Strength',25)->set('Dexterity',25)->set('Vitality',25)->set('Energy',25);
        $SumStats -= 100;

        if ($isDl) {
            $Query->set('LeaderShip', 25);
            $SumStats -= 25;
        }
        
        $Query->set('LevelUpPoint', 'LevelUpPoint + ' . $SumStats, false);

        if (ValuteManager::CheckFive($Config['Price']['Valutes'][0], $Config['Price']['Valutes'][1], $Config['Price']['Valutes'][2], $Config['Price']['Valutes'][3], $Config['Price']['Valutes'][4])) {

            if ( $Query->update('Character',null ,array('Name' => $Nick)) ) {
                ValuteManager::TakeFive($Config['Price']['Valutes'][0], $Config['Price']['Valutes'][1], $Config['Price']['Valutes'][2], $Config['Price']['Valutes'][3], $Config['Price']['Valutes'][4]);

                Logs::WriteLog("Module: [Reset Stats], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Success reset stats", 'Modules.log');
                Messages::Add('success_reset_stats_nick');
                return true;
            } else {
                Logs::WriteLog("Module: [Reset Stats], Account: [{$_SESSION['login']}] Nick: [{$Nick}]. Fail reset stats, Error in Query", 'Modules.log');
                Messages::Add('fail_reset_stats_nick');
                return true;
            }
        }
    }
}