<?php

namespace FooPlugins\FooConvert\Admin;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * FooConvert Admin Promotion Class
 * Adds all the promotion related info.
 */

if ( !class_exists( __NAMESPACE__ . '\Promotions' ) ) {

    /**
     * Class Promotions.
     */
    class Promotions {

        /**
         * Init constructor.
         */
        function __construct() {
            add_action( 'init', array( $this, 'init_promotions' ) );

            add_filter( 'fs_show_trial_fooconvert', array( $this, 'force_trial_hide' ) );
            add_action( 'admin_init', array( $this, 'force_hide_trial_notice' ), 99 );
        }

        /**
         * Determines whether promotions should be hidden or not.
         *
         * @return bool Whether promotions should be hidden.
         */
        private function must_hide_promos() {
            if ( 'on' === fooconvert_get_setting( 'hide_promos' ) ) {
                return true;
            }

            // If in trial mode, skip showing promotions
            if ( fooconvert_fs()->is_trial() ) {
                return true;
            }

            // If paying, skip showing promotions
            if ( !fooconvert_fs()->is_not_paying() ) {
                return true;
            }

            return false;
        }

        /**
         * Initialises all promotions.
         *
         * @since 1.0.0
         */
        public function init_promotions() {
            // If hide_promos is enabled, do not show any promotions!
            if ( $this->must_hide_promos() ) {
                return;
            }

            // Add the PRO Features panel to the dashboard
            add_action( 'fooconvert_admin_dashboard_right', function() {
                include_once __DIR__ . '/Views/dashboard-panel-pro-features.php';
            });

            add_action( 'fooconvert_admin_dashboard_right', array( $this, 'render_addons_panel' ) );
            add_action( 'fooconvert_popup_stats_html-metrics', array( $this, 'render_metrics' ), 10, 2 );
            add_filter( 'fooconvert_popup_metric_options', array( $this, 'adjust_popup_metric_options' ) );
        }


        /**
         * Make sure the trail banner admin notice is not shown
         *
         * @param $show_trial
         *
         * @return false
         */
        function force_trial_hide( $show_trial ) {
            if ( $this->must_hide_promos() ) {
                $show_trial = false;
            }

            return $show_trial;
        }

        /**
         * Force the trial promotion admin notice to be removed
         */
        function force_hide_trial_notice() {
            if ( $this->must_hide_promos() ) {
                $freemius_sdk = fooconvert_fs();
                $plugin_id    = $freemius_sdk->get_slug();
                $admin_notice_manager = \FS_Admin_Notice_Manager::instance( $plugin_id );
                $admin_notice_manager->remove_sticky( 'trial_promotion' );
            }
        }

        /**
         * Render the premium addon slider in the FooConvert admin dashboard
         *
         * @since 1.0.0
         */
        public function render_addons_panel() {
            if ( fooconvert_is_dashboard_panel_hidden( 'premium' ) ) {
                return;
            }

            $fs = fooconvert_fs();

            /**
             * @var \FS_Plugin[]
             */
            $addons = $fs->get_addons();

            $has_addons = ( is_array( $addons ) && 0 < count( $addons ) );

            if ( !$has_addons ) {
                return;
            }

            ?>
            <div class="fooconvert-panel" data-panel="premium">
                <div class="fooconvert-panel-section fooconvert-panel-header">
                    <h2>💎<?php esc_html_e( 'Premium Addons', 'fooconvert' ); ?></h2>
                    <div class="fooconvert-panel-section-right">
                        <div class="fooconvert-slider-nav">
                            <button class="fooconvert-slider-prev button button-small">
                                <span class="dashicons dashicons-arrow-left-alt2"></span>
                            </button>
                            <button class="fooconvert-slider-next button button-small">
                                <span class="dashicons dashicons-arrow-right-alt2"></span>
                            </button>
                        </div>
                        <a class="fooconvert-hide-panel" data-panel="premium" href="#hide"
                           title="<?php esc_html_e( 'Hide Panel', 'fooconvert' ); ?>">
                            <span class="dashicons dashicons-no-alt"></span>
                        </a>
                    </div>
                </div>
                <div class="fooconvert-slider">
                    <div class="fooconvert-slider-wrapper">


                        <?php

                        foreach ( $addons as $addon ) :

                            if ( is_null( $addon->info ) ) {
                                $addon->info = new \stdClass();
                            }

                            $addon_description = !empty( $addon->info->short_description ) ? $addon->info->short_description : '';
                            $add_features = [];

                            if ( !empty( $addon->info->selling_point_0 ) ) {
                                $add_features[] = $addon->info->selling_point_0;
                            }

                            if ( !empty( $addon->info->selling_point_1 ) ) {
                                $add_features[] = $addon->info->selling_point_1;
                            }

                            if ( !empty( $addon->info->selling_point_2 ) ) {
                                $add_features[] = $addon->info->selling_point_2;
                            }

                            ?>
                            <div class="fooconvert-slide">


                                <h3><?php echo esc_html( $addon->title ); ?></h3>

                                <p>
                                    <strong><?php echo esc_html( $addon_description ); ?></strong>
                                </p>
                                <?php if ( !empty( $add_features ) ) : ?>
                                    <ul>
                                        <?php foreach ( $add_features as $feature ) : ?>
                                            <li>
                                                <span class="dashicons dashicons-yes"></span>
                                                <?php echo esc_html( $feature ); ?>
                                            </li>
                                        <?php endforeach; ?>
                                    </ul>
                                <?php endif; ?>
                                <p class="fooconvert-last">
                                    <a href="<?php echo esc_url( fooconvert_admin_url_addons() ); ?>"
                                       class="button button-primary"
                                       target="_blank"><?php esc_html_e( 'Buy Now!', 'fooconvert' ); ?></a>
                                </p>

                            </div>

                        <?php endforeach;

                        ?>
                    </div>
                </div>
            </div>
            <?php
        }

        /**
         * Renders advanced PRO metrics for a popup.
         *
         * @param int $post_id The ID of the popup.
         * @param array $popup The popup data.
         */
        public function render_metrics( $post_id, $popup ) {
            ?>
            <div class="metric pro-feature">
                <p><?php echo esc_html( wp_rand( 0, 100 ) ); ?></p>
                <h2><?php esc_html_e( 'Total Clicks', 'fooconvert' ); ?></h2>
                <span data-balloon-pos="down"
                      aria-label="<?php esc_attr_e( 'Total number of clicks made.', 'fooconvert' ); ?>">
                    <i class="dashicons dashicons-editor-help"></i>
                </span>
            </div>
            <div class="metric pro-feature">
                <p><?php echo esc_html( wp_rand( 0, 100 ) ); ?>%</p>
                <h2><?php esc_html_e( 'Click Through Rate', 'fooconvert' ); ?></h2>
                <span data-balloon-pos="down"
                      aria-label="<?php esc_attr_e( 'Click Through Rate for the popup.', 'fooconvert' ); ?>">
                    <i class="dashicons dashicons-editor-help"></i>
                </span>
            </div>
            <div class="metric pro-feature">
                <p><?php echo esc_html( wp_rand( 0, 100 ) ); ?></p>
                <h2><?php esc_html_e( 'Total Conversions', 'fooconvert' ); ?></h2>
                <span data-balloon-pos="down"
                      aria-label="<?php esc_attr_e( 'Total number of successul conversions made.', 'fooconvert' ); ?>">
                    <i class="dashicons dashicons-editor-help"></i>
                </span>
            </div>
            <div class="metric pro-feature">
                <p><?php echo esc_html( wp_rand( 0, 100 ) ); ?>%</p>
                <h2><?php esc_html_e( 'Conversion Rate', 'fooconvert' ); ?></h2>
                <span data-balloon-pos="down"
                      aria-label="<?php esc_attr_e( 'Conversion Rate.', 'fooconvert' ); ?>">
                    <i class="dashicons dashicons-editor-help"></i>
                </span>
            </div>
            <div class="metric pro-feature">
                <p><?php echo esc_html( wp_rand( 0, 100 ) ); ?>%</p>
                <h2><?php esc_html_e( 'Engagement Rate', 'fooconvert' ); ?></h2>
                <span data-balloon-pos="down"
                      aria-label="<?php esc_attr_e( 'Engagement Rate.', 'fooconvert' ); ?>">
                    <i class="dashicons dashicons-editor-help"></i>
                </span>
            </div>
            <div class="metric pro-feature">
                <p><?php echo esc_html( wp_rand( 0, 100 ) ); ?></p>
                <h2><?php esc_html_e( 'Positive Engagements', 'fooconvert' ); ?></h2>
                <span data-balloon-pos="down"
                      aria-label="<?php esc_attr_e( 'Total number of positive engagements (like clicks).', 'fooconvert' ); ?>">
                    <i class="dashicons dashicons-editor-help"></i>
                </span>
            </div>
            <div class="metric pro-feature">
                <p><?php echo esc_html( wp_rand( 0, 100 ) ); ?></p>
                <h2><?php esc_html_e( 'Negative Engagements', 'fooconvert' ); ?></h2>
                <span data-balloon-pos="down"
                      aria-label="<?php esc_attr_e( 'Total number of negative engagements (like dismissals).', 'fooconvert' ); ?>">
                    <i class="dashicons dashicons-editor-help"></i>
                </span>
            </div>
            <div class="metric pro-feature">
                <p>Sentiment</p>
                <h2><?php esc_html_e( 'Overall Sentiment', 'fooconvert' ); ?></h2>
                <span data-balloon-pos="down"
                      aria-label="<?php esc_attr_e( 'Overall sentiment which ranges from very negative to very positive. It can also be unknown if there are no engagements, or neutral.', 'fooconvert' ); ?>">
                    <i class="dashicons dashicons-editor-help"></i>
                </span>
            </div>

            <!-- PRO Info Panel -->
            <div class="pro-info-panel">
                <i class="dashicons dashicons-star-filled"></i>
                <p><?php esc_html_e( 'These advanced metrics are available in PRO.', 'fooconvert' ); ?>
                    <a href="<?php echo esc_url( fooconvert_admin_url_pricing() ); ?>"><?php esc_html_e( 'Upgrade to PRO', 'fooconvert' ); ?></a>
                </p>
            </div>
            <?php
        }

        /**
         * Adjusts the popup metric options to include PRO features.
         *
         * @param array $options The current popup metric options.
         * @return array The updated popup metric options.
         */
        function adjust_popup_metric_options( $options ) {
            $options['engagement-rate'] = [
                'dropdown_option' => __( 'engagement rate (PRO)', 'fooconvert' ),
                'pro_feature'     => true,
                'pro_message'     => __( 'Top performers by engagement rate is only available in PRO.', 'fooconvert' )
            ];

            $options['clicks'] = [
                'dropdown_option' => __( 'clicks (PRO)', 'fooconvert' ),
                'pro_feature'     => true,
                'pro_message'     => __( 'Top performers by clicks is only available in PRO.', 'fooconvert' )
            ];

            $options['click-rate'] = [
                'dropdown_option' => __( 'click rate (PRO)', 'fooconvert' ),
                'pro_feature'     => true,
                'pro_message'     => __( 'Top performers by click rate is only available in PRO.', 'fooconvert' )
            ];

            $options['conversions'] = [
                'dropdown_option' => __( 'conversions (PRO)', 'fooconvert' ),
                'pro_feature'     => true,
                'pro_message'     => __( 'Top performers by conversions is only available in PRO.', 'fooconvert' )
            ];

            $options['conversion-rate'] = [
                'dropdown_option' => __( 'conversion rate (PRO)', 'fooconvert' ),
                'pro_feature'     => true,
                'pro_message'     => __( 'Top performers by conversion rate is only available in PRO.', 'fooconvert' )
            ];

            return $options;
        }
    }
}
