<?php

/**
 * Created by PhpStorm.
 * User: MainUser
 * Date: 18.07.16
 * Time: 18:27
 */
class ChangeName extends CharacterModule
{
    /**
     * @param $Name string
     * @return bool
     * true - available
     * false - not available
     */
    public function CheckStatusName($Name)
    {
        if ($this->AccountOnline)
            return false;
        if (App::$DBs[$_SESSION['user_db']]->select('Name')->from('Character')->where('Name', App::Validate($Name))->count_all_results()) {
            Messages::Add('name_not_available');
            return false;
        }
        else
            return true;
    }
    public function Change($OldName = '', $NewName)
    {
        $Config = App::$Configs->mod_ChangeName;
        if ($OldName == '')
            $OldName = $_SESSION['ActivePlayer'];
        if (!$Config['Enable']){
            Messages::Add('disable_module');
            return false;
        }
        if (!App::$DBs[$_SESSION['user_db']]->select('Name')->from('Character')->where('Name', $OldName)->where('AccountID', $_SESSION['login'])->count_all_results()) {
            Messages::Add('error_change_name');
            return false;
        }

        $NewName = App::Validate($NewName);
        $OldName = App::Validate($OldName);

        if (strlen($NewName) < 3 || strlen($NewName) >= 10) {
            Messages::Add('incorect_name');
            return false;
        }
        if (!$this->CheckStatusName($NewName))
            return false;
        $Character = Character::GetActivePlayerInfo($OldName);
        $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;
        }

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

            if (
                App::$DBs[$_SESSION['user_db']]->set('Name', $NewName)->where('Name', $OldName)->update('Character') &&
                App::$DBs[$_SESSION['user_db']]->set('GameID1', $NewName)->where('GameID1', $OldName)->update('AccountCharacter') &&
                App::$DBs[$_SESSION['user_db']]->set('GameID2', $NewName)->where('GameID2', $OldName)->update('AccountCharacter') &&
                App::$DBs[$_SESSION['user_db']]->set('GameID3', $NewName)->where('GameID3', $OldName)->update('AccountCharacter') &&
                App::$DBs[$_SESSION['user_db']]->set('GameID4', $NewName)->where('GameID4', $OldName)->update('AccountCharacter') &&
                App::$DBs[$_SESSION['user_db']]->set('GameID5', $NewName)->where('GameID5', $OldName)->update('AccountCharacter') &&
                App::$DBs[$_SESSION['user_db']]->set('GameIDC', $NewName)->where('GameIDC', $OldName)->update('AccountCharacter') &&
               // App::$DBs[$_SESSION['user_db']]->set('Name', $NewName)->where('Name', $OldName)->update('SCFS5Quest') &&
                App::$DBs[$_SESSION['user_db']]->set('FriendName', $NewName)->where('FriendName', $OldName)->update('T_FriendList') &&
                App::$DBs[$_SESSION['user_db']]->set('FriendName', $NewName)->where('FriendName', $OldName)->update('T_FriendMail') &&
                App::$DBs[$_SESSION['user_db']]->set('Name', $NewName)->where('Name', $OldName)->update('T_FriendMain') &&
                App::$DBs[$_SESSION['user_db']]->set('CHAR_NAME', $NewName)->where('CHAR_NAME', $OldName)->update('T_SkillTree_Info') &&
                App::$DBs[$_SESSION['user_db']]->set('Name', $NewName)->where('Name', $OldName)->update('T_MacroInfo') &&
                App::$DBs[$_SESSION['user_db']]->set('CHAR_NAME', $NewName)->where('CHAR_NAME', $OldName)->update('T_3rd_Quest_Info') &&
                App::$DBs[$_SESSION['user_db']]->set('Name', $NewName)->where('Name', $OldName)->update('OptionData') &&
                App::$DBs[$_SESSION['user_db']]->set('G_Master', $NewName)->where('G_Master', $OldName)->update('Guild') &&
                App::$DBs[$_SESSION['user_db']]->set('Name', $NewName)->where('Name', $OldName)->update('GuildMember')
            ) {
                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: [Change Name], Account: [{$_SESSION['login']}] Nick: [{$OldName}]. Success change nick to ({$NewName})", 'Modules.log');
                Messages::Add('success_change_nick');
                $_SESSION['update_acc_char'] = true;
                return true;
            } else {
                Logs::WriteLog("Module: [Change Name], Account: [{$_SESSION['login']}] Nick: [{$OldName}]. Fail change nick to ({$NewName}), Error in Query", 'Modules.log');
                Messages::Add('fail_change_nick');
                return true;
            }
        }
    }
}