<?php

class ChangeClass extends CharacterModule
{
    public function NeedToChange($Character){
        if($this->AccountOnline)
        {
            return;
        }
        $ChangeClassConfig = App::$Configs->mod_ChangeClass;
        if (!$ChangeClassConfig['Enable'])
            return;

        $ReturnArr = array();
        $ReturnArr['NeedLevel'] = $ChangeClassConfig['NeedLevel'];
        $ReturnArr['NeedReset'] = $ChangeClassConfig['NeedReset'];
        $ReturnArr['NeedEmptyInv'] = $ChangeClassConfig['NeedEmptyInv'];
        $ReturnArr['Classes'] = $ChangeClassConfig['Classes'];
        
        $ReturnArr['Take']['Valutes'] = $ChangeClassConfig['Take']['Valutes'];

        return $ReturnArr;

    }
    public function Change($NickCharacter, $Class)
    {
        if ($this->AccountOnline)
        {
            return;
        }
        $ChangeClassConfig = App::$Configs->mod_ChangeClass;
        if (!$ChangeClassConfig['Enable'])
            return;

        $Character = Character::GetActivePlayerInfo($NickCharacter);
        $Character = $Character[0];
        $Block = false;
        if($Character['cLevel'] < $ChangeClassConfig['NeedLevel']){
            $Block = true;
            Messages::Add("block_change_class_need_level");
        }
        if($Character['Resets'] < $ChangeClassConfig['NeedReset']){
            $Block = true;
            Messages::Add("block_change_class_need_reset");
        }
        if($ChangeClassConfig['NeedEmptyInv'] && !Character::EmptyInventory($Character)){
            $Block = true;
            Messages::Add("block_change_class_empty_inv");
        }else if ($Class == 'DW' && !$ChangeClassConfig['Classes']['DW'])
        {
            $Class = 0;
            $Block = true;
            Messages::Add("block_change_class_no_correct_class");
        }else if ($Class == 'DK' && !$ChangeClassConfig['Classes']['DK'])
        {
            $Class = 16;
            $Block = true;
            Messages::Add("block_change_class_no_correct_class");
        }else if ($Class == 'ELF' && !$ChangeClassConfig['Classes']['ELF'])
        {
            $Class = 32;
            $Block = true;
            Messages::Add("block_change_class_no_correct_class");
        }else if ($Class == 'MG' && !$ChangeClassConfig['Classes']['MG'])
        {
            $Class = 48;
            $Block = true;
            Messages::Add("block_change_class_no_correct_class");
        }else if ($Class == 'SUM' && !$ChangeClassConfig['Classes']['SUM'])
        {
            $Class = 80;
            $Block = true;
            Messages::Add("block_change_class_no_correct_class");
        }else if ($Class == 'DL' && !$ChangeClassConfig['Classes']['DL'])
        {
            $Class = 64;
            $Block = true;
            Messages::Add("block_change_class_no_correct_class");
        }else if ($Class == 'RF' && !$ChangeClassConfig['Classes']['RF'])
        {
            $Class = 96;
            $Block = true;
            Messages::Add("block_change_class_no_correct_class");
        }

        if ($Class == 'DW') {$Class = 0;}
        else if ($Class == 'DK') {$Class = 16;}
        else if ($Class == 'ELF') {$Class = 32;}
        else if ($Class == 'MG') {$Class = 48;}
        else if ($Class == 'SUM') {$Class = 80;}
        else if ($Class == 'DL') {$Class = 64;}
        else if ($Class == 'RF') {$Class = 96;}


        if ($Block == false) {
            if (ValuteManager::CheckFive($ChangeClassConfig['Take']['Valutes'][0], $ChangeClassConfig['Take']['Valutes'][1], $ChangeClassConfig['Take']['Valutes'][2], $ChangeClassConfig['Take']['Valutes'][3], $ChangeClassConfig['Take']['Valutes'][4])) {

                if (
                	App::$DBs[$_SESSION['user_db']]->where('Name', $Character['Name'])->set('MagicList', 'NULL', false)->set('Quest', 'NULL', false)->set('Class',$Class)->update('Character') &&

                	App::$DBs[$_SESSION['user_db']]->where('CHAR_NAME', $Character['Name'])->set('MASTER_LEVEL','0')->set('ML_EXP','0')->set('ML_NEXTEXP','0')->set('ML_POINT','0')->update('T_SkillTree_Info')
                	) {
                    ValuteManager::TakeFive($ChangeClassConfig['Take']['Valutes'][0], $ChangeClassConfig['Take']['Valutes'][1], $ChangeClassConfig['Take']['Valutes'][2], $ChangeClassConfig['Take']['Valutes'][3], $ChangeClassConfig['Take']['Valutes'][4]);

                    Logs::WriteLog("Module: [ChangeClass], Account [{$_SESSION['login']}]. Success change class({$Class})", 'Modules.log');
                    Messages::Add('success_change_class');
                    return true;
                }
            }
        }
        Logs::WriteLog("Module: [ChangeClass], Account [{$_SESSION['login']}]. Fail change class", 'Modules.log');
        Messages::Add('fail_changeclass');
        return false;
    }
}