<?php
if(!isLoggedIn()) redirect(1,'login');
echo '<div class="page-title"><span>'.lang('transfercharacter_title',true).'</span></div>';
try {
    $TransferCharacter=new \Plugin\TransferCharacter\TransferCharacter();
    $TransferCharacter->setUsername($_SESSION['username']);
    $characters=$TransferCharacter->getAccountCharacterList();
    $Character=new Character();

    if(isset($_GET['success']) && (int)$_GET['success']===1) message('success',lang('transfercharacter_success_1',true));

    echo '<div class="alert alert-info"><strong>'.htmlspecialchars(\NexoraRealm::label(),ENT_QUOTES,'UTF-8').'</strong> — '.lang('transfercharacter_txt_10',true).'<br>';
    echo '<label style="margin-top:10px">'.lang('transfercharacter_txt_6',true).'</label>';
    echo '<input type="text" class="form-control" readonly onclick="this.select()" value="'.htmlspecialchars($TransferCharacter->generateTransferId($_SESSION['userid']),ENT_QUOTES,'UTF-8').'">';
    echo '</div>';

    if(isset($_GET['character'])) {
        $characterName=base64url_decode($_GET['character']);
        if(!is_string($characterName) || !in_array($characterName,$characters,true)) throw new Exception(lang('transfercharacter_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('transfercharacter_error_6',true));
                $token=isset($_POST['transfer_token'])?(string)$_POST['transfer_token']:'';
                if(!\Plugin\TransferCharacter\TransferCharacter::consumeTransferToken($token)) throw new Exception(lang('transfercharacter_error_27',true));
                $TransferCharacter->setUserid($_SESSION['userid']);
                $TransferCharacter->setCharacter($characterName);
                $TransferCharacter->setTransferId($_POST['transfer_id']??'');
                $TransferCharacter->transfer();
            } catch(Exception $ex) { message('error',$ex->getMessage()); }
        }

        $data=$Character->CharacterData($characterName);
        if(!is_array($data)) throw new Exception(lang('transfercharacter_error_6',true));
        $img=$Character->GenerateCharacterClassAvatar($data[_CLMN_CHR_CLASS_]);
        $token=\Plugin\TransferCharacter\TransferCharacter::issueTransferToken();
        echo '<table class="table general-table-ui"><thead><tr><th></th><th>'.lang('transfercharacter_txt_1',true).'</th><th>'.lang('transfercharacter_txt_3',true).'</th><th></th></tr></thead><tbody>';
        echo '<form action="'.__TRANSFERCHARACTER_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="transfer_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="transfer_id" placeholder="NX-L-... / NX-S-..." maxlength="64" required autocomplete="off"></td>';
        echo '<td><button type="submit" name="submit" value="1" class="btn btn-primary" data-nexora-submit-text="'.htmlspecialchars(lang('transfercharacter_txt_11',true),ENT_QUOTES,'UTF-8').'">'.lang('transfercharacter_txt_2',true).'</button></td></tr>';
        echo '</form></tbody></table>';
    } else {
        echo '<table class="table general-table-ui"><thead><tr><th></th><th>'.lang('transfercharacter_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="'.__TRANSFERCHARACTER_HOME__.'character/'.base64url_encode($data[_CLMN_CHR_NAME_]).'/" class="btn btn-primary">'.lang('transfercharacter_txt_2',true).'</a></td></tr>';
        }
        echo '</tbody></table>';
    }

    echo '<div class="module-requirements text-center">';
    echo '<p>'.langf('transfercharacter_txt_4',array(number_format($TransferCharacter->getCost()))).'</p>';
    echo '<p>'.lang('transfercharacter_txt_5',true).'</p>';
    if($TransferCharacter->getRequiredLevel()>0) echo '<p>'.langf('transfercharacter_txt_7',array($TransferCharacter->getRequiredLevel())).'</p>';
    if($TransferCharacter->getRequiredMasterLevel()>0) echo '<p>'.langf('transfercharacter_txt_8',array($TransferCharacter->getRequiredMasterLevel())).'</p>';
    if($TransferCharacter->getRequiredResets()>0) echo '<p>'.langf('transfercharacter_txt_9',array($TransferCharacter->getRequiredResets())).'</p>';
    echo '<p><strong>'.lang('transfercharacter_txt_12',true).'</strong></p>';
    echo '</div>';
} catch(Exception $ex) { message('error',$ex->getMessage()); }
