<?php
if (!defined('ABSPATH')) {
    exit;
}


/**
 * Class WpmsHandlerWizard
 */
class WpmsHandlerWizard
{
    /**
     * WpmsHandlerWizard constructor.
     */
    public function __construct()
    {
    }

    /**
     * Save Environment handle
     *
     * @param string $current_step Current step
     *
     * @return void
     */
    public static function saveEvironment($current_step)
    {
        check_admin_referer('wpms-setup-wizard', 'wizard_nonce');
        /*
         * Do no thing
         */
        $wizard = new WpmsInstallWizard();
        wp_safe_redirect(esc_url_raw($wizard->getNextLink($current_step)));
        exit;
    }

    /**
     * Save social
     *
     * @param string $current_step Current step
     *
     * @return void
     */
    public static function saveSocial($current_step)
    {
        check_admin_referer('wpms-setup-wizard', 'wizard_nonce');

        $options = array(
            'metaseo_showfacebook' => '',
            'metaseo_showfbappid'  => '',
            'metaseo_showtwitter'  => '',
            'metaseo_twitter_card' => 'summary'
        );

        foreach ($options as $name => $value) {
            if (isset($_POST[$name])) {
                wpmsProSetOption($name, $_POST[$name]);
            }
        }
        $wizard = new WpmsInstallWizard();
        wp_safe_redirect(esc_url_raw($wizard->getNextLink($current_step)));
        exit;
    }

    /**
     * Save home meta
     *
     * @param string $current_step Current step
     *
     * @return void
     */
    public static function saveMetaInfos($current_step)
    {
        check_admin_referer('wpms-setup-wizard', 'wizard_nonce');
        $options = array(
            'home_meta_active'       => 1,
            'metaseo_showtmetablock' => 1,
            'metaseo_title_home'     => '',
            'metaseo_desc_home'      => ''
        );

        foreach ($options as $name => $value) {
            if (isset($_POST[$name])) {
                wpmsProSetOption($name, $_POST[$name]);
            }
        }
        $wizard = new WpmsInstallWizard();
        wp_safe_redirect(esc_url_raw($wizard->getNextLink($current_step)));
        exit;
    }

    /**
     * Save Google Analytics
     *
     * @param string $current_step Current step
     *
     * @return void
     */
    public static function saveGoogleAnalytics($current_step)
    {
        check_admin_referer('wpms-setup-wizard', 'wizard_nonce');
        if (!empty($_POST['wpms_ga_code'])) {
            $wpms_ga_code  = $_POST['wpms_ga_code'];
            $midnight      = strtotime('tomorrow 00:00:00'); // UTC midnight
            $midnight      = $midnight + 8 * 3600; // UTC 8 AM
            $error_timeout = $midnight - time();

            require_once WPMETASEO_ADDON_PLUGIN_DIR . 'inc/google_analytics/wpmstools.php';
            $client = WpmsGaTools::initClient(WPMS_CLIENTID, WPMS_CLIENTSECRET);
            $service          = new WPMSGoogle_Service_Analytics($client);
            $google_alanytics = array();
            if (!stripos('x' . $wpms_ga_code, 'UA-', 1)) {
                WpmsGaTools::deleteCache('gapi_errors');
                WpmsGaTools::deleteCache('last_error');
                WpmsGaTools::clearCache();
                try {
                    $client->authenticate($wpms_ga_code);
                    $getAccessToken = $client->getAccessToken();
                    if ($getAccessToken) {
                        try {
                            $client->setAccessToken($getAccessToken);
                            $google_alanytics['googleCredentials']
                                = $client->getAccessToken();
                        } catch (Google\Service\Exception $e) {
                            WpmsGaTools::setCache(
                                'wpmsga_dash_lasterror',
                                date('Y-m-d H:i:s') . ': ' . esc_html('(' . $e->getCode() . ') ' . $e->getMessage()),
                                $error_timeout
                            );
                            WpmsGaTools::setCache(
                                'wpmsga_dash_gapi_errors',
                                $e->getCode(),
                                $error_timeout
                            );
                        } catch (Exception $e) {
                            WpmsGaTools::setCache(
                                'wpmsga_dash_lasterror',
                                date('Y-m-d H:i:s') . ': ' . esc_html($e),
                                $error_timeout
                            );
                        }
                    }

                    if (!empty($google_alanytics['profile_list'])) {
                        $profiles = $google_alanytics['profile_list'];
                    } else {
                        $profiles = WpmsGaTools::refreshProfiles($service, $getAccessToken['access_token'], $error_timeout);
                    }

                    $google_alanytics['code']              = $wpms_ga_code;
                    $google_alanytics['googleCredentials'] = $getAccessToken;
                    $google_alanytics['profile_list']      = $profiles;
                    update_option('wpms_google_alanytics', $google_alanytics);
                } catch (Google\Service\Exception $e) {
                    echo '';
                } catch (Exception $e) {
                    echo '';
                }
            } else {
                echo '<div class="error"><p>' . esc_html__('The access code is 
<strong>NOT</strong> your <strong>Tracking ID</strong>
 (UA-XXXXX-X). Try again, and use the red link to get your access code', 'wpmsPro') . '.</p></div>';
            }

            update_option('wpms_google_alanytics', $google_alanytics);
        }

        if (isset($_POST['wpms_ga_uax_reference']) && isset($_POST['wpms_gg_service_tracking_type'])) {
            $currentTracking = array();
            $latestTrackingId = 0;
            if (!empty(get_option('wpms_tracking_list'))) {
                foreach (get_option('wpms_tracking_list') as $tracking) {
                    if ($latestTrackingId < $tracking['tracking_record_id']) {
                        $latestTrackingId = $tracking['tracking_record_id'];
                    }
                    $trackingRow = [
                        'tracking_record_id' => $tracking['tracking_record_id'],
                        'tracking_type' => $tracking['tracking_type'],
                        'tracking_identifier' => $tracking['tracking_identifier'],
                        'tracking_insert_in_head' => $tracking['tracking_insert_in_head'],
                        'tracking_head_code' => $tracking['tracking_head_code'],
                        'tracking_insert_in_body' => $tracking['tracking_insert_in_body'],
                        'tracking_body_code' => $tracking['tracking_body_code'],
                    ];
                    array_push($currentTracking, $trackingRow);
                }
            }
            $newTrackingRow = [
                'tracking_record_id' => $latestTrackingId + 1,
                'tracking_type' => '',
                'tracking_identifier' => '',
                'tracking_insert_in_head' => false,
                'tracking_head_code' => '',
                'tracking_insert_in_body' => false,
                'tracking_body_code' => '',
            ];
            switch ($_POST['wpms_gg_service_tracking_type']) {
                case 'analytics4':
                case 'tagmanager':
                    $newTrackingRow['tracking_type'] = $_POST['wpms_gg_service_tracking_type'];
                    $newTrackingRow['tracking_identifier'] = $_POST['wpms_ga_uax_reference'];
                    array_push($currentTracking, $newTrackingRow);
                    break;
                default:
                    break;
            }
            update_option('wpms_tracking_list', $currentTracking);
        }

        $wizard = new WpmsInstallWizard();
        wp_safe_redirect(esc_url_raw($wizard->getNextLink($current_step)));
        exit;
    }
}
