/**
  * Hook: Page Top (Callback)
  **/
 public function hookTop()
 {
     // Load the context.
     $this->context = Context::getContext();
     // Only of the user is not logged in.
     if (!$this->context->customer->isLogged()) {
         // Check for callback arguments.
         if (Tools::getIsset('oa_action') === true and Tools::getIsset('connection_token') === true) {
             // Extract the callback arguments.
             $oa_action = trim(Tools::getValue('oa_action'));
             $connection_token = trim(Tools::getValue('connection_token'));
             //Verify arguments
             if ($oa_action == 'social_login' and strlen($connection_token) > 0) {
                 // Read the API credentials.
                 $api_key = Configuration::get('OASL_API_KEY');
                 $api_password = Configuration::get('OASL_API_PASSWORD');
                 $api_subdomain = Configuration::get('OASL_API_SUBDOMAIN');
                 // Read the API settings.
                 $api_handler = Configuration::get('OASL_API_HANDLER');
                 $api_handler = $api_handler == 'fsockopen' ? 'fsockopen' : 'curl';
                 $api_port = Configuration::get('OASL_API_PORT');
                 $api_port = $api_port == 80 ? 80 : 443;
                 // Set API resource uri.
                 $api_resource = ($api_port === 443 ? 'https' : 'http') . '://' . $api_subdomain . '.api.oneall.com/connections/' . $connection_token . '.json';
                 // Setup API parameters.
                 $api_params = array();
                 $api_params['api_key'] = $api_key;
                 $api_params['api_secret'] = $api_password;
                 // Retrieve connection details.
                 $result = oneall_social_login_tools::do_api_request($api_handler, $api_resource, $api_params, 15);
                 // Parse data.
                 $data = oneall_social_login_tools::extract_social_network_profile($result);
                 // Handle data.
                 if (is_array($data)) {
                     // Get the customer identifier for a given token.
                     $id_customer_tmp = oneall_social_login_tools::get_id_customer_for_user_token($data['user_token']);
                     // This customer already exists.
                     if (is_numeric($id_customer_tmp)) {
                         // Update the identity.
                         oneall_social_login_tools::update_identity_logins($data['identity_token']);
                         // Login this customer.
                         $id_customer = $id_customer_tmp;
                     } else {
                         // Account linking is enabled.
                         if (Configuration::get('OASL_LINK_ACCOUNT_DISABLE') != 1) {
                             // Account linking only works if the email address has been verified.
                             if (!empty($data['user_email']) && $data['user_email_is_verified'] === true) {
                                 // Try to read the existing customer account.
                                 if (($id_customer_tmp = oneall_social_login_tools::get_id_customer_for_email_address($data['user_email'])) !== false) {
                                     // Tie the user_token to the customer.
                                     if (oneall_social_login_tools::link_tokens_to_id_customer($id_customer_tmp, $data['user_token'], $data['identity_token'], $data['identity_provider']) === true) {
                                         // Update the identity.
                                         oneall_social_login_tools::update_identity_logins($data['identity_token']);
                                         // Login this customer.
                                         $id_customer = $id_customer_tmp;
                                     }
                                 }
                             }
                         }
                     }
                     // Create a user new account.
                     if (empty($id_customer)) {
                         // Notify the customer ?
                         $customer_email_notify = true;
                         // Redirection url.
                         $redirect_to = $this->context->link->getPageLink('oneallsociallogin', true, null, array('back' => oneall_social_login_tools::get_current_url()));
                         // How do we have to proceed?
                         switch (Configuration::get('OASL_DATA_HANDLING')) {
                             // Automatic Completion.
                             case 'auto':
                                 // Generate a random email if none is provided or if it's already taken.
                                 if (empty($data['user_email']) or oneall_social_login_tools::get_id_customer_for_email_address($data['user_email']) !== false) {
                                     // Generate a random email.
                                     $data['user_email'] = oneall_social_login_tools::generate_random_email_address();
                                     // But do not send notifications to this email
                                     $customer_email_notify = false;
                                 }
                                 // Generate a lastname if none is provided.
                                 if (empty($data['user_last_name'])) {
                                     $data['user_last_name'] = 'Doe';
                                 }
                                 // Generate a firstname if none is provided.
                                 if (empty($data['user_first_name'])) {
                                     $data['user_first_name'] = 'John';
                                 }
                                 break;
                                 //Ask for manual completion if any of the fields is empty or if the email is already taken.
                             //Ask for manual completion if any of the fields is empty or if the email is already taken.
                             case 'ask':
                                 if (empty($data['user_email']) or empty($data['user_first_name']) or empty($data['user_last_name']) or oneall_social_login_tools::get_id_customer_for_email_address($data['user_email']) !== false) {
                                     // Save the data in the session.
                                     $this->context->cookie->oasl_data = base64_encode(serialize($data));
                                     //Redirect to the request form
                                     header('Location: ' . $redirect_to);
                                     exit;
                                 }
                                 break;
                                 //Always verify the fields
                             //Always verify the fields
                             default:
                                 // Save the data in the session.
                                 $this->context->cookie->oasl_data = base64_encode(serialize($data));
                                 //Redirect to the request form
                                 header('Location: ' . $redirect_to);
                                 exit;
                                 break;
                         }
                         // Email flags.
                         $send_email_to_admin = Configuration::get('OASL_EMAIL_ADMIN_DISABLE') != 1 ? true : false;
                         $send_email_to_customer = ($customer_email_notify == true and Configuration::get('OASL_EMAIL_CUSTOMER_DISABLE') != 1);
                         // Create a new account.
                         $id_customer = oneall_social_login_tools::create_customer_from_data($data, $send_email_to_admin, $send_email_to_customer);
                     }
                     // Login.
                     if (!empty($id_customer) and oneall_social_login_tools::login_customer($id_customer)) {
                         //Remove the data (Should not be set here)
                         if (isset($this->context->cookie->oasl_data)) {
                             unset($this->context->cookie->oasl_data);
                         }
                         //A refresh is required to update the page
                         $back = trim(Tools::getValue('back'));
                         $back = !empty($back) ? $back : oneall_social_login_tools::get_current_url();
                         Tools::redirect($back);
                     }
                 }
             }
         }
     }
 }