<?php
class Reset extends CharacterModule
{
    public function NeedToReset($Character)
    {
        if($this->AccountOnline)
        {
            return;
        }
        $ResetConfig = App::$Configs->mod_Reset;
        if (!$ResetConfig['Enable']) {
            Logs::WriteLog("Module: [Reset], Account [{$_SESSION['login']}]. Module disable", 'Modules.log');
            Messages::Add("module_disable");
            return false;
        }
        $ReturnArr = array();
        $ReturnArr['NeedLevel'] = $ResetConfig['NeedLevel'];
        $ReturnArr['NeedEmptyInv'] = $ResetConfig['NeedEmptyInv'];
        $ReturnArr['ClearSkill'] = $ResetConfig['ClearSkill'];
        /*
            Point Type 1 - Points * Reset
            point Type 2 - Save Point + Points
        */
        if ($ResetConfig['RewardPoint']['Type'] == 1) {
            switch ($Character['Class']) {
                case 0:
                case 1:
                case 2:
                case 3:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['DW'] * ($Character['Resets'] + 1);
                    break;
                case 16:
                case 17:
                case 18:
                case 19:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['DK'] * ($Character['Resets'] + 1);
                    break;
                case 32:
                case 34:
                case 35:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['ELF'] * ($Character['Resets'] + 1);
                    break;
                case 48:
                case 49:
                case 50:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['MG'] * ($Character['Resets'] + 1);
                    break;

                case 64:
                case 65:
                case 66:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['DL'] * ($Character['Resets'] + 1);
                    break;
                case 80:
                case 81:
                case 82:
                case 83:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['SUM'] * ($Character['Resets'] + 1);
                    break;
                case 96:
                case 97:
                case 98:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['RF'] * ($Character['Resets'] + 1);
                    break;
            }
        }
        else if ($ResetConfig['RewardPoint']['Type'] == 2) {
            $SumPoint = $Character['LevelUpPoint'] + $Character['Strength'] + $Character['Dexterity'] + $Character['Vitality'] + $Character['Energy'] + $Character['LeaderShip']; 
            switch ($Character['Class']) {
                case 0:
                case 1:
                case 2:
                case 3:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['DW'] + $SumPoint;
                    break;
                case 16:
                case 17:
                case 18:
                case 19:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['DK'] + $SumPoint;
                    break;
                case 32:
                case 34:
                case 35:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['ELF'] + $SumPoint;
                    break;
                case 48:
                case 49:
                case 50:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['MG'] + $SumPoint;
                    break;

                case 64:
                case 65:
                case 66:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['DL'] + $SumPoint;
                    break;
                case 80:
                case 81:
                case 82:
                case 83:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['SUM'] + $SumPoint;
                    break;
                case 96:
                case 97:
                case 98:
                    $ReturnArr['RewardPoints'] = $ResetConfig['RewardPoint']['RF'] + $SumPoint;
                    break;
            }
        }
        $ReturnArr['Take']['Valutes'] = $ResetConfig['Take']['Valutes'];
        $ReturnArr['Reward']['Valutes'] = $ResetConfig['Reward']['Valutes'];

        return $ReturnArr;
    }
    public function RunReset($Character)
    {
        if($this->AccountOnline)
        {
            return;
        }
        $ResetConfig = App::$Configs->mod_Reset;
        if (!$ResetConfig['Enable'])
            return false;

        $Block = false;
        if ($Character['cLevel'] < $ResetConfig['NeedLevel'])
        {
            $Block = true;
            Messages::Add("block_reset_need_level");
        }
        if ($ResetConfig['NeedEmptyInv'] && !Character::EmptyInventory($Character))
        {
            $Block = true;
            Messages::Add("block_reset_empty_inv");
        }
        if ($Block)
        {
            return false;
        }
        else
        {
            $Query = App::$DBs[$_SESSION['user_db']]
                ->set('cLevel',1)
                ->set('Strength',25)
                ->set('Dexterity',25)
                ->set('Vitality',25)
                ->set('Energy',25);
                
            if ($Character['Class'] == 64 || $Character['Class'] == 65 || $Character['Class'] == 66) {
                $Query->set('LeaderShip',25);
            }

            if ($ResetConfig['RewardPoint']['Type'] == 1) {
                switch ($Character['Class']) {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                        $RewardPoints = $ResetConfig['RewardPoint']['DW'] * $Character['Resets'];
                        break;
                    case 16:
                    case 17:
                    case 18:
                    case 19:
                        $RewardPoints = $ResetConfig['RewardPoint']['DK'] * $Character['Resets'];
                        break;
                    case 32:
                    case 34:
                    case 35:
                        $RewardPoints = $ResetConfig['RewardPoint']['ELF'] * $Character['Resets'];
                        break;
                    case 48:
                    case 49:
                    case 50:
                        $RewardPoints = $ResetConfig['RewardPoint']['MG'] * $Character['Resets'];
                        break;

                    case 64:
                    case 65:
                    case 66:
                        $RewardPoints = $ResetConfig['RewardPoint']['DL'] * $Character['Resets'];
                        break;
                    case 80:
                    case 81:
                    case 82:
                    case 83:
                        $RewardPoints = $ResetConfig['RewardPoint']['SUM'] * $Character['Resets'];
                        break;
                    case 96:
                    case 97:
                    case 98:
                        $RewardPoints = $ResetConfig['RewardPoint']['RF'] * $Character['Resets'];
                        break;
                }
            }
            else if ($ResetConfig['RewardPoint']['Type'] == 2) {
                $SumPoint = $Character['LevelUpPoint'] + $Character['Strength'] + $Character['Dexterity'] + $Character['Vitality'] + $Character['Energy'] + $Character['LeaderShip'];
                switch ($Character['Class']) {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                        $RewardPoints = $ResetConfig['RewardPoint']['DW'] + $SumPoint;
                        break;
                    case 16:
                    case 17:
                    case 18:
                    case 19:
                        $RewardPoints = $ResetConfig['RewardPoint']['DK'] + $SumPoint;
                        break;
                    case 32:
                    case 34:
                    case 35:
                        $RewardPoints = $ResetConfig['RewardPoint']['ELF'] + $SumPoint;
                        break;
                    case 48:
                    case 49:
                    case 50:
                        $RewardPoints = $ResetConfig['RewardPoint']['MG'] + $SumPoint;
                        break;

                    case 64:
                    case 65:
                    case 66:
                        $RewardPoints = $ResetConfig['RewardPoint']['DL'] + $SumPoint;
                        break;
                    case 80:
                    case 81:
                    case 82:
                    case 83:
                        $RewardPoints = $ResetConfig['RewardPoint']['SUM'] + $SumPoint;
                        break;
                    case 96:
                    case 97:
                    case 98:
                        $RewardPoints = $ResetConfig['RewardPoint']['RF'] + $SumPoint;
                        break;
                }
            }
            $Query->set('LevelUpPoint',$RewardPoints);
            if ($ResetConfig['ClearSkill'])
            {
                $Query->set('MagicList',0);
            }
            
            $Query->set(App::$DBs[$_SESSION['user_db']]->column_names['Resets'],$Character['Resets'] + 1);
            $Query->where('Name',$Character['Name']);
            if (ValuteManager::CheckFive($ResetConfig['Take']['Valutes'][0],$ResetConfig['Take']['Valutes'][1],$ResetConfig['Take']['Valutes'][2],$ResetConfig['Take']['Valutes'][3],$ResetConfig['Take']['Valutes'][4])) {
                if ($Query->get('Character')) {
                    ValuteManager::TakeFive($ResetConfig['Take']['Valutes'][0],$ResetConfig['Take']['Valutes'][1],$ResetConfig['Take']['Valutes'][2],$ResetConfig['Take']['Valutes'][3],$ResetConfig['Take']['Valutes'][4]);
                    ValuteManager::RewardFive($ResetConfig['Reward']['Valutes'][0],$ResetConfig['Reward']['Valutes'][1],$ResetConfig['Reward']['Valutes'][2],$ResetConfig['Reward']['Valutes'][3],$ResetConfig['Reward']['Valutes'][4]);

                    Messages::Add('success_reset');
                    Logs::WriteLog("Module: [Reset], Account [{$_SESSION['login']}]. Success Reset: ". $Character['Resets']+1, 'Modules.log');
                    return true;
                }
            }
            Messages::Add('fail_reset');
            Logs::WriteLog("Module: [Reset], Account [{$_SESSION['login']}]. Fail in sql query", 'Modules.log');
            return false;
        }

    }
}