* @copyright   Copyright (c) 2015 Amazon.com
 * @license     http://opensource.org/licenses/Apache-2.0  Apache License, Version 2.0
 */
require 'includes/application_top.php';
include_once DIR_WS_MODULES . 'loginwithamazon/tools.php';
include_once DIR_WS_MODULES . 'payment/paywithamazon/lib/Client.php';
// Start...
if (LoginWithAmazon_Tools::get_access_token()) {
    $user_data = LoginWithAmazon_Tools::get_userdata();
    $user_email = $user_data->email;
    $user_id = LoginWithAmazon_Tools::get_customer_id_by_email($user_email);
    $new_account = false;
    if ($user_id) {
        if (!LoginWithAmazon_Tools::is_amazon_user_any($user_id)) {
            // If user exists, but is NOT an Amazon user, then start the merge prompt.
            tep_redirect(LoginWithAmazon_Tools::get_merge_url());
            exit;
        }
    } else {
        $name = explode(' ', $user_data->name, 2);
        $user_id = LoginWithAmazon_Tools::create_user($name[0], $name[1], $user_email);
        $new_account = true;
    }
    LoginWithAmazon_Tools::login_user($user_id);
    if ($new_account) {
        tep_redirect(tep_href_link(FILENAME_CREATE_ACCOUNT_SUCCESS));
        tep_redirect(tep_href_link(FILENAME_DEFAULT));
    } else {
        tep_redirect(tep_href_link(FILENAME_DEFAULT));
    }
}
        /**
         * Alter the login form. Prompt for password to link Amazon and native accounts.
         */
        private function renderMerge()
        {
            global $oscTemplate;
            global $messageStack;
            $lwa_config = LoginWithAmazon_Tools::get_config();
            $messageStack->add('login', MODULE_HEADER_TAGS_LOGINWITHAMAZON_SHOULD_MERGE, 'success');
            $action_url = LoginWithAmazon_Tools::get_merge_url(array('action=process'));
            ob_start();
            ?>

            <script type="text/javascript">
            // document ready
            function amazonDocumentReady(fn) {
                if (document.readyState != 'loading'){
                    fn();
                } else if (document.addEventListener) {
                    document.addEventListener('DOMContentLoaded', fn);
                } else {
                    document.attachEvent('onreadystatechange', function() {
                        if (document.readyState != 'loading')
                            fn();
                    });
                }
            }

            amazonDocumentReady(function() {
                // Hide the email field
                var loginForm = document.querySelector('#loginModules form[name="login"]');
                if (!loginForm) return;
                loginForm.setAttribute('action', '<?php 
            echo $action_url;
            ?>
');

                var emailInput = loginForm.querySelector('input[name="email_address"]');
                if (!emailInput) return;

                var emailInputRow = emailInput.parentNode.parentNode;
                if (!emailInputRow) return;
                emailInputRow.style.display = 'none';

                // Change the form button's text
                var buttonSpan = loginForm.querySelector('button[type="submit"] .ui-button-text');
                if (!buttonSpan) return;
                var buttonText = 'Link My Amazon Account';

                if (typeof buttonSpan.textContent !== 'undefined') {
                    buttonSpan.textContent = buttonText;
                } else {
                    buttonSpan.innerText = buttonText;
                }
            });
            </script>

            <?php 
            $output = ob_get_contents();
            ob_end_clean();
            $oscTemplate->addBlock($output . "\n", $this->group);
        }