<?php
if(!isLoggedIn()) redirect(1,'login');
echo '<div class="page-title"><span>'.lang('renamecharacter_title',true).'</span></div>';
try {
    $RenameCharacter=new \Plugin\RenameCharacter\RenameCharacter();
    $RenameCharacter->setUsername($_SESSION['username']);
    $characters=$RenameCharacter->getAccountCharacterList();
    $Character=new Character();
    if(isset($_GET['success']) && (int)$_GET['success']===1) message('success',lang('renamecharacter_success_1',true));

    if(isset($_GET['character'])) {
        $characterName=base64url_decode($_GET['character']);
        if(!is_string($characterName) || !in_array($characterName,$characters,true)) throw new Exception(lang('renamecharacter_error_6',true));
        if(($_SERVER['REQUEST_METHOD']??'GET')==='POST' && isset($_POST['submit'])) {
            try {
                if(!isset($_POST['character']) || (string)$_POST['character']!==$characterName) throw new Exception(lang('renamecharacter_error_6',true));
                $token=isset($_POST['rename_token'])?(string)$_POST['rename_token']:'';
                if(!\Plugin\RenameCharacter\RenameCharacter::consumeRenameToken($token)) throw new Exception(lang('renamecharacter_error_17',true));
                $RenameCharacter->setUserid($_SESSION['userid']);
                $RenameCharacter->setCharacter($characterName);
                $RenameCharacter->setNewCharacterName($_POST['new_name']??'');
                $RenameCharacter->changeName();
            } catch(Exception $ex) { message('error',$ex->getMessage()); }
        }
        $data=$Character->CharacterData($characterName);
        if(!is_array($data)) throw new Exception(lang('renamecharacter_error_6',true));
        $img=$Character->GenerateCharacterClassAvatar($data[_CLMN_CHR_CLASS_]);
        $token=\Plugin\RenameCharacter\RenameCharacter::issueRenameToken();
        echo '<p><strong>'.htmlspecialchars(\NexoraRealm::label(),ENT_QUOTES,'UTF-8').'</strong> · '.lang('renamecharacter_txt_7',true).'</p>';
        echo '<table class="table general-table-ui"><thead><tr><th></th><th>'.lang('renamecharacter_txt_1',true).'</th><th>'.lang('renamecharacter_txt_3',true).'</th><th></th></tr></thead><tbody>';
        echo '<form action="'.__RENAMECHARACTER_HOME__.'character/'.base64url_encode($data[_CLMN_CHR_NAME_]).'/" method="post">';
        echo '<input type="hidden" name="character" value="'.htmlspecialchars($data[_CLMN_CHR_NAME_],ENT_QUOTES,'UTF-8').'">';
        echo '<input type="hidden" name="rename_token" value="'.htmlspecialchars($token,ENT_QUOTES,'UTF-8').'">';
        echo '<tr><td>'.$img.'</td><td>'.htmlspecialchars($data[_CLMN_CHR_NAME_],ENT_QUOTES,'UTF-8').'</td>';
        echo '<td><input type="text" class="form-control" name="new_name" minlength="'.$RenameCharacter->getMinLen().'" maxlength="'.$RenameCharacter->getMaxLen().'" required autofocus autocomplete="off"></td>';
        echo '<td><button type="submit" name="submit" value="1" class="btn btn-primary" data-nexora-submit-text="'.htmlspecialchars(lang('renamecharacter_txt_8',true),ENT_QUOTES,'UTF-8').'">'.lang('renamecharacter_txt_2',true).'</button></td></tr>';
        echo '</form></tbody></table>';
    } else {
        echo '<p><strong>'.htmlspecialchars(\NexoraRealm::label(),ENT_QUOTES,'UTF-8').'</strong> · '.lang('renamecharacter_txt_7',true).'</p>';
        echo '<table class="table general-table-ui"><thead><tr><th></th><th>'.lang('renamecharacter_txt_1',true).'</th><th></th></tr></thead><tbody>';
        foreach($characters as $row) {
            $data=$Character->CharacterData($row); if(!is_array($data)) continue;
            $img=$Character->GenerateCharacterClassAvatar($data[_CLMN_CHR_CLASS_]);
            echo '<tr><td>'.$img.'</td><td>'.htmlspecialchars($data[_CLMN_CHR_NAME_],ENT_QUOTES,'UTF-8').'</td><td><a href="'.__RENAMECHARACTER_HOME__.'character/'.base64url_encode($data[_CLMN_CHR_NAME_]).'/" class="btn btn-primary">'.lang('renamecharacter_txt_2',true).'</a></td></tr>';
        }
        echo '</tbody></table>';
    }
    echo '<div class="module-requirements text-center">';
    echo '<p>'.langf('renamecharacter_txt_4',array(number_format($RenameCharacter->getCost()))).'</p>';
    echo '<p>'.langf('renamecharacter_txt_6',array($RenameCharacter->getMinLen(),$RenameCharacter->getMaxLen())).'</p>';
    echo '<p>'.lang('renamecharacter_txt_5',true).'</p></div>';
} catch(Exception $ex) { message('error',$ex->getMessage()); }
