<?php
    /**
     * User profile view.
     *
     * Contains the view layout and ome code to present the data.
     *
     * @category View
     * @property_read Profile_Page $page The page model.
     * @property_read int $UID Player ID.
     */

    namespace swdb;
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta content='text/html; charset=utf-8' http-equiv='content-type'/>
        <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1'/>
        <title><?=$page->title?></title>
        <link rel='shortcut icon' href='<?=$page->favicon?>'/>
        <!-- CSS files -->
        <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
        <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>profile.css'/>
        <!-- Meta tags -->
        <link rel='canonical' href='<?=$page->canonical?>'/>
        <link rel='author' href='<?=$page->author?>'/>
        <link rel='publisher' href='<?=$page->author?>'/>
        <meta name='description' content='<?=$page->description?>'/>
        <meta property='og:title' content='<?=$page->title?>'/>
        <meta property='og:url' content='<?=$page->canonical?>'/>
        <meta property='og:description' content='<?=$page->description?>'/>
        <meta property='og:image' content='<?=$page->icon?>'/>
        <meta property='og:site_name' content='<?=$page->name?>'/>
        <meta property='og:type' content='website'/>
        <meta property='og:locale' content='en'/>
        <meta name='twitter:card' content='summary'/>
        <meta name='twitter:title' content='<?=$page->title?>'/>
        <meta name='twitter:description' content='<?=$page->description?>'/>
        <meta name='twitter:image' content='<?=$page->icon?>'/>
        <meta name='twitter:url' content='<?=$page->canonical?>'/>
        <meta name='robots' content='index follow'/>
        <script>

            /**
             * Redirects to the logout action.
             */
            function logout(){
                window.location = '/action/logout/';
            }

            /**
             * Shows an error window.
             *
             * @param msg Error message
             */
            function showError(msg){
                document.getElementById('error_window').style.display = 'block';
                document.getElementById('error_msg').innerHTML = msg;
            }

            /**
             * Closes the error window.
             */
            function closeError(){
                document.getElementById('error_window').style.display = 'none';
                document.getElementById('error_msg').innerHTML = ' ';
            }

            /**
             * Shows the mail change form.
             */
            function changeMail(){
                document.getElementById('static_mail').style.display = 'none';
                document.getElementById('change_mail').style.display = 'none';
                document.getElementById('edit_mail').style.display = 'block';
                document.getElementById('save_mail').style.display = 'block';
            }

            /**
             * Saves an email change.
             * 
             * Validates the new email format and makes an AJAX request. If
             * succesfull, hides the email change form.
             */
            function saveMail(){
                var http = new XMLHttpRequest();
                var mail = document.getElementById('edit_mail').value;
                if (mail.length < 1){
                    showError('Please enter a new email.');
                    return;
                }
                if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail) == false){
                    showError('Invalid email');
                    return;
                }
                http.open('POST', '/action/edit_profile', true);
                http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = function(){
                    if (this.readyState == 4){
                        if (this.status == 200 || this.status == 204){
                            document.getElementById('static_mail').innerHTML = mail;
                            document.getElementById('static_mail').style.display = 'block';
                            document.getElementById('change_mail').style.display = 'block';
                            document.getElementById('edit_mail').style.display = 'none';
                            document.getElementById('save_mail').style.display = 'none';
                        }
                        else{
                            showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
                        }
                    }
                }
                http.send('uid=<?=$UID?>&mail=' + mail);
            }

            /**
             * Shows the password change form.
             */
            function changePass(){
                document.getElementById('static_pass').style.display = 'none';
                document.getElementById('change_pass').style.display = 'none';
                document.getElementById('edit_pass_current').value = '';
                document.getElementById('edit_pass_current').value = '';
                document.getElementById('edit_pass_new').value = '';
                document.getElementById('edit_pass_repeat').style.display = 'block';
                document.getElementById('edit_pass_new').style.display = 'block';
                document.getElementById('edit_pass_repeat').style.display = 'block';
                document.getElementById('save_pass').style.display = 'block';
            }

            /**
             * Saves a password change.
             * 
             * Validates the new password and makes an AJAX request. If
             * succesfull, hides the password change form.
             */
            function savePass(){
                var http = new XMLHttpRequest();
                var pass = document.getElementById('edit_pass_new').value;
                var currentPass = document.getElementById('edit_pass_current').value;
                var repeatPass = document.getElementById('edit_pass_repeat').value;
                if (pass.length < 1){
                    showError('Please enter a new password.');
                    return;
                }
                if (mail.length < 6){
                    showError('Minimum password length is 6 characters.');
                    return;
                }
                if (currentPass.length < 1){
                    showError('Please enter your current password.');
                    return;
                }
                if (pass != repeatPass){
                    showError('Entered passwords do not match.');
                    return;
                }
                http.open('POST', '/action/edit_profile', true);
                http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = function(){
                    if (this.readyState == 4){
                        if (this.status == 200 || this.status == 204){
                            document.getElementById('static_pass').style.display = 'block';
                            document.getElementById('change_pass').style.display = 'block';
                            document.getElementById('edit_pass_current').style.display = 'none';
                            document.getElementById('edit_pass_new').style.display = 'none';
                            document.getElementById('edit_pass_repeat').style.display = 'none';
                            document.getElementById('save_pass').style.display = 'none';
                        }
                        else{
                            showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
                        }
                    }
                }
                http.send('uid=<?=$UID?>&pass=' + pass + '&currentPass=' + currentPass);
            }

            /**
             * Generates a new random API key.
             *
             * Makes a request. On success, updates the API in the UI.
             */
            function regenerateApi(){
                var http = new XMLHttpRequest();
                http.open('POST', '/action/edit_profile', true);
                http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = function(){
                    if (this.readyState == 4){
                        if (this.status == 200 || this.status == 204){
                            document.getElementById('api_key').innerHTML = this.responseText;
                        }
                        else{
                            showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
                        }
                    }
                }
                http.send('uid=<?=$UID?>&api=1');
            }
        </script>
    </head>
    <body>
<?php
        include __DIR__ . "/inc/header.php";
?>
        <section id='monster' class='content'>
            <h2>
                <span>
                    <?=$page->name?>
                </span>
            </h2>
            <article>
                <table id='profile'>
                    <tr>
                        <td class='title'>
                            Username:
                        </td>
                        <td class='value'>
                            <span>
                                <?=$page->name?>
                            </span>
                        </td>
                        <td class='action'>
                            <input onclick='logout();' type='button' value='Logout'/>
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            eMail:
                        </td>
                        <td class='value'>
                            <span id='static_mail'>
                                <?=$page->mail?>
                            </span>
                            <input id='edit_mail' type='text' value='<?=$page->mail?>'/>
                        </td>
                        <td class='action'>
                            <input id='change_mail' onclick='changeMail();' type='button' value='Change'/>
                            <input id='save_mail' onclick='saveMail();' type='button' value='Save'/>
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            API key:
                        </td>
                        <td class='value'>
                            <span id='api_key'>
                                <?=$page->api_key?>
                            </span>
                        </td>
                        <td class='action'>
                            <input id='regenerate_api' onclick='regenerateApi();' type='button' value='Regenerate'/>
                        </td>
                    </tr>
                    <tr>
                        <td class='title'>
                            Password:
                        </td>
                        <td class='value'>
                            <span id='static_pass'>
                                ********
                            </span>
                            <input id='edit_pass_current' type='password' placeholder='Currrent password'/>
                            <input id='edit_pass_new' type='password' placeholder='New password'/>
                            <input id='edit_pass_repeat' type='password' placeholder='Repeat password'/>
                        </td>
                        <td class='action'>
                            <input id='change_pass' onclick='changePass();' type='button' value='Change'/>
                            <input id='save_pass' onclick='savePass();' type='button' value='Save'/>
                        </td>
                    </tr>
                </table>
            </article>
        </section> <!-- runes -->
        <div id='error_window'>
            <section class='content'>
                <h2>
                    <span>
                        Error
                    </span>
                </h2>
                <article>
                    <p id='error_msg'>
                    </p>
                    <input type='button' value='Close' onclick='closeError();'/>
                </article>
            </section>
        </div>

<?php
        include __DIR__ . "/inc/footer.php";
?>
    </body>
</html>

