<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Page' ) ) {

    require_once (dirname( __FILE__ ) . '/checkout-prices-section/checkout-prices-section.php');
    require_once (dirname( __FILE__ ) . '/checkout-prices-section/checkout-prices-panel.php');
    require_once (dirname( __FILE__ ) . '/checkout-prices-section/checkout-prices-panel-options.php');
    require_once (dirname( __FILE__ ) . '/checkout-prices-section/checkout-prices-panel-coupon.php');
    require_once (dirname( __FILE__ ) . '/checkout-prices-section/checkout-prices-panel-max.php');
    require_once (dirname( __FILE__ ) . '/checkout-prices-section/checkout-prices-panel-conditions.php');

    require_once (dirname( __FILE__ ) . '/product-prices-section/product-prices-section.php');
    require_once (dirname( __FILE__ ) . '/product-prices-section/product-prices-panel.php');
    require_once (dirname( __FILE__ ) . '/product-prices-section/product-prices-panel-options.php');
    require_once (dirname( __FILE__ ) . '/product-prices-section/product-prices-panel-products.php');
    require_once (dirname( __FILE__ ) . '/product-prices-section/product-prices-panel-max.php');
    require_once (dirname( __FILE__ ) . '/product-prices-section/product-prices-panel-conditions.php');

    require_once (dirname( __FILE__ ) . '/cart-discounts-section/cart-discounts-section.php');
    require_once (dirname( __FILE__ ) . '/cart-discounts-section/cart-discounts-panel.php');
    require_once (dirname( __FILE__ ) . '/cart-discounts-section/cart-discounts-panel-discounts.php');
    require_once (dirname( __FILE__ ) . '/cart-discounts-section/cart-discounts-panel-coupon.php');
    require_once (dirname( __FILE__ ) . '/cart-discounts-section/cart-discounts-panel-max.php');
    require_once (dirname( __FILE__ ) . '/cart-discounts-section/cart-discounts-panel-conditions.php');

    require_once (dirname( __FILE__ ) . '/product-promotions-section/product-promotions-section.php');
    require_once (dirname( __FILE__ ) . '/product-promotions-section/product-promotions-panel.php');
    require_once (dirname( __FILE__ ) . '/product-promotions-section/product-promotions-panel-promotions.php');
    require_once (dirname( __FILE__ ) . '/product-promotions-section/product-promotions-panel-conditions.php');

    require_once (dirname( __FILE__ ) . '/settings-ui-badge-section/settings-ui-badge-section.php');
    require_once (dirname( __FILE__ ) . '/settings-ui-countdown-sections/settings-ui-countdown-sections.php');
    require_once (dirname( __FILE__ ) . '/settings-ui-sections/settings-ui-sections.php');
    require_once (dirname( __FILE__ ) . '/settings-section/settings-section.php');

    class Pricely_Admin_Page {

        private static $instance = null;
        private $option_name = PRICELY_OPTION_NAME;
        private $menu_slug = PRICELY_MENU_SLUG;
        private $product_prices_page;
        private $checkout_prices_page;
        private $cart_discounts_page;
        private $locale;

        public static function get_instance(): self {

            if ( null == self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        private function __construct() {

            $this->init_page();
            $this->init_data_store();

            $this->product_prices_page = new Pricely_Admin_Product_Prices_Page();
            $this->checkout_prices_page = new Pricely_Admin_Checkout_Prices_Page();
            $this->cart_discounts_page = new Pricely_Admin_Cart_Discounts_Page();

            new Pricely_Admin_Product_Promotions_Page();

            new Pricely_Admin_Settings_Badge_Styles_Page();
            new Pricely_Admin_Settings_CountDown_Styles_Page();
            new Pricely_Admin_Settings_Styles_Section_Page();
            new Pricely_Admin_Settings_Section_Page();

            add_filter( 'reon_register-data-sources', array( $this, 'init_data_list_sources' ), 10 );

            add_filter( 'reon_get-option-page-' . $this->option_name . '-sections', array( $this, 'config_all_sections' ), 10 );

            add_filter( 'reon_process-save-options-' . $this->option_name, array( $this, 'save_options' ), 10 );


            add_filter( 'reon_pricely_admin_get-disabled-list', array( $this, 'get_disabled_list' ), 10, 2 );
            add_filter( 'reon_pricely_admin_get-disabled-grouped-list', array( $this, 'get_grouped_disabled_list' ), 10, 2 );

            add_filter( 'plugin_action_links_' . plugin_basename( PRICELY_MAIN_FILE ), array( $this, 'get_plugin_links' ), 10, 1 );
        }

        public function init_page() {

            /* translators: 1:  plugin version */
            $version_text = sprintf( esc_html__( 'v%s', 'pricely-dynamic-pricing-cart-discounts' ), PRICELY_VERSION );

            if ( defined( 'PRICELY_PRO_VERSION' ) ) {

                /* translators: 1:  plugin version */
                $version_text = sprintf( esc_html__( 'Pro v%s', 'pricely-dynamic-pricing-cart-discounts' ), PRICELY_PRO_VERSION );
            }

            $args = array(
                'option_name' => $this->option_name,
                'database' => 'option',
                'sanitize_mode' => 'recursive',
                'slug' => $this->menu_slug,
                'url_base' => 'admin.php',
                'default_min_height' => '700px',
                'width' => 'auto',
                'page_id' => 'pricely_page',
                'enable_section_title' => true,
                'aside_width' => '210px',
                'user_capability' => 'manage_woocommerce',
                'display' => array(
                    'enabled' => true,
                    'image' => PRICELY_ASSETS_URL . 'images/aside_logo.png',
                    'title' => esc_html__( 'Dynamic Pricing', 'pricely-dynamic-pricing-cart-discounts' ),
                    'sub_title' => esc_html__( 'Pricing &amp; Discounts + Promotions', 'pricely-dynamic-pricing-cart-discounts' ),
                    'version' => $version_text,
                    'styles' => array(
                        'bg_image' => PRICELY_ASSETS_URL . 'images/aside_bg.png',
                        'bg_color' => '#0073aa',
                        'color' => '#fff',
                        'height' => '195px',
                    ),
                ),
                'ajax' => array(
                    'save_msg' => esc_html__( 'Done!!', 'pricely-dynamic-pricing-cart-discounts' ),
                    'save_error_msg' => esc_html__( 'Error saving the settings', 'pricely-dynamic-pricing-cart-discounts' ),
                    'reset_msg' => esc_html__( 'Done!!', 'pricely-dynamic-pricing-cart-discounts' ),
                    'reset_error_msg' => esc_html__( 'Error resetting the settings', 'pricely-dynamic-pricing-cart-discounts' ),
                    'reset_prompt_msg' => esc_html__( "All settings will be removed, Press 'OK' to continue", 'pricely-dynamic-pricing-cart-discounts' ),
                    'reset_prompt_section_msg' => esc_html__( "Settings in this section will be removed, Press 'OK' to continue", 'pricely-dynamic-pricing-cart-discounts' ),
                    'nonce_error_msg' => esc_html__( 'invalid nonce', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'menu' => array(
                    'enable' => true,
                    'title' => esc_html__( 'Pricing &amp; Discounts+', 'pricely-dynamic-pricing-cart-discounts' ),
                    'page_title' => esc_html__( 'Pricing &amp; Discounts+', 'pricely-dynamic-pricing-cart-discounts' ),
                    'icon' => 'dashicons-admin-generic',
                    'priority' => 4,
                    'parent' => 'woocommerce-marketing',
                ),
                'import_export' => array(
                    'enable' => true,
                    'min_height' => '565px',
                    'title' => esc_html__( 'Import / Export', 'pricely-dynamic-pricing-cart-discounts' ),
                    'header_title' => esc_html__( 'Import / Export', 'pricely-dynamic-pricing-cart-discounts' ),
                    'import' => array(
                        'title' => esc_html__( 'Import Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                        'desc' => esc_html__( 'Here you can import new settings. Simply paste the settings url or data on the field below.', 'pricely-dynamic-pricing-cart-discounts' ),
                        'url_button_text' => esc_html__( 'Import from url', 'pricely-dynamic-pricing-cart-discounts' ),
                        'url_textbox_desc' => esc_html__( "Paste the url to another site's settings below and click the 'Import Now' button.", 'pricely-dynamic-pricing-cart-discounts' ),
                        'url_textbox_hint' => esc_html__( "Paste the url to another site's settings here...", 'pricely-dynamic-pricing-cart-discounts' ),
                        'data_button_text' => esc_html__( 'Import Data', 'pricely-dynamic-pricing-cart-discounts' ),
                        'data_textbox_desc' => esc_html__( "Paste your backup settings below and click the 'Import Now' button.", 'pricely-dynamic-pricing-cart-discounts' ),
                        'data_textbox_hint' => esc_html__( 'Paste your backup settings here...', 'pricely-dynamic-pricing-cart-discounts' ),
                        'import_button_text' => esc_html__( 'Import Now', 'pricely-dynamic-pricing-cart-discounts' ),
                        'warn_text' => esc_html__( 'Warning! This will override all existing settings. proceed with caution!', 'pricely-dynamic-pricing-cart-discounts' ),
                    ),
                    'export' => array(
                        'title' => esc_html__( 'Export Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                        'desc' => esc_html__( 'Here you can backup your current settings. You can later use it to restore your settings.', 'pricely-dynamic-pricing-cart-discounts' ),
                        'download_button_text' => esc_html__( 'Download Data', 'pricely-dynamic-pricing-cart-discounts' ),
                        'url_button_text' => esc_html__( 'Export url', 'pricely-dynamic-pricing-cart-discounts' ),
                        'url_textbox_desc' => esc_html__( 'Copy the url below, use it to transfer the settings from this site.', 'pricely-dynamic-pricing-cart-discounts' ),
                        'data_button_text' => esc_html__( 'Export Data', 'pricely-dynamic-pricing-cart-discounts' ),
                        'data_textbox_desc' => esc_html__( 'Copy the data below, use it as your backup.', 'pricely-dynamic-pricing-cart-discounts' ),
                    ),
                ),
                'header_buttons' => array(
                    'reset_all_text' => esc_html__( 'Reset All', 'pricely-dynamic-pricing-cart-discounts' ),
                    'reset_section_text' => esc_html__( 'Reset Section', 'pricely-dynamic-pricing-cart-discounts' ),
                    'save_section_text' => esc_html__( 'Save Section', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'footer_buttons' => array(
                    'reset_all_text' => esc_html__( 'Reset All', 'pricely-dynamic-pricing-cart-discounts' ),
                    'reset_section_text' => esc_html__( 'Reset Section', 'pricely-dynamic-pricing-cart-discounts' ),
                    'save_section_text' => esc_html__( 'Save Section', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'page_links' => $this->get_page_links(),
                'social_links' => $this->get_social_links(),
            );

            Reon::set_option_page( $args );
        }

        public function config_all_sections( $in_sections ) {

            $in_sections[] = array(
                'title' => esc_html__( 'Products Pricing', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'Pricing At Checkout', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'products',
                'group' => 2,
            );

            $in_sections[] = array(
                'title' => esc_html__( 'Pricing At Checkout', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'Pricing At Checkout', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'checkout_prices',
                'group' => 2,
                'subsection' => true
            );

            $in_sections[] = array(
                'title' => esc_html__( 'Price Adjustments', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'Price Adjustments', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'product_prices',
                'group' => 3,
                'subsection' => true
            );

            $in_sections[] = array(
                'title' => esc_html__( 'Cart Discounts', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'Cart Discounts', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'cart_discounts',
                'group' => 4,
            );

            //Implement shipping module here with id of #shipping

            if ( class_exists( 'Pricely_Pro_Admin_Page' ) ) {

                $in_sections[] = array(
                    'title' => esc_html__( 'Checkout Fees', 'pricely-dynamic-pricing-cart-discounts' ),
                    'header_title' => esc_html__( 'Checkout Fees', 'pricely-dynamic-pricing-cart-discounts' ),
                    'id' => 'cart_fees',
                    'group' => 5,
                );

                $in_sections[] = array(
                    'title' => esc_html__( 'Cart Notifications', 'pricely-dynamic-pricing-cart-discounts' ),
                    'header_title' => esc_html__( 'Cart Notifications', 'pricely-dynamic-pricing-cart-discounts' ),
                    'id' => 'cart_messages',
                    'group' => 6,
                );
            }

            $in_sections[] = array(
                'title' => esc_html__( 'Promotion Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'Promotion Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'product_promotions',
                'group' => 7,
            );

            $in_sections[] = array(
                'title' => esc_html__( 'UI Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'Countdown Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'styles',
                'group' => 8,
            );

            $in_sections[] = array(
                'title' => esc_html__( 'Countdown Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'Countdown Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'countdown_styles_settings',
                'group' => 8,
                'subsection' => true
            );
            
            if ( class_exists( 'Pricely_Pro_Admin_Page' ) ) {

                $in_sections[] = array(
                    'title' => esc_html__( 'Product Badge Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                    'header_title' => esc_html__( 'Product Badge Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                    'id' => 'badge_styles_settings',
                    'group' => 9,
                    'subsection' => true
                );
            }

            $in_sections[] = array(
                'title' => esc_html__( 'Other UI Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'Other UI Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'styles_settings',
                'group' => 10,
                'subsection' => true
            );

            $in_sections[] = array(
                'title' => esc_html__( 'General Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                'header_title' => esc_html__( 'General Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                'id' => 'settings',
                'group' => 11,
            );

            return $in_sections;
        }

        public function init_data_store() {

            global $pricely_settings;

            $pricely_settings = get_option( $this->option_name, array() );
        }

        public function init_data_list_sources( $data_sources ) {

            $data_sources[ 'pricely' ] = 'Pricely_Admin_Data_List';

            return $data_sources;
        }

        public function sanitize_kses( $option ) {

            $allowed_html = Pricely_Main::get_instance()->get_allow_html();

            return wp_kses( $option, $allowed_html );
        }

        public function save_options( $options ) {

            //set the settings revision
            $options[ 'revision' ] = current_time( 'U' );

            $options[ 'using_external_css' ] = 'yes';

            if ( !$this->save_css( $options ) ) {

                $options[ 'using_external_css' ] = 'no';
            }

            return $this->process_locale_options( $this->process_options( $options ) );
        }

        public function get_plugin_links( $actions ) {

            $links = array();

            $settings_url = admin_url( 'admin.php?page=' . $this->menu_slug );

            $links[ 'pricely_settings' ] = '<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'Settings', 'pricely-dynamic-pricing-cart-discounts' ) . '</a>';

            $links[ 'pricely_doc' ] = '<a href="' . esc_url( 'https://support.zendcrew.cc/portal/en/kb/woocommerce-dynamic-pricing-discounts' ) . '" target="_blank">' . esc_html__( 'Documentation', 'pricely-dynamic-pricing-cart-discounts' ) . '</a>';

            return array_merge( $links, $actions );
        }

        public function get_pro_messages( $message_id = '' ) {

            $pro_url = "#";
            $message = esc_html__( 'This feature is exclusive to the premium version', 'pricely-dynamic-pricing-cart-discounts' );
            $link_text = esc_html__( 'Premium Feature', 'pricely-dynamic-pricing-cart-discounts' );

            switch ( $message_id ) {

                case 'short':

                    return '<a href="' . $pro_url . '" target="_blank">' . $link_text . '</a>';
                default:

                    return '<a href="' . $pro_url . '" target="_blank">' . $link_text . '</a> - ' . $message . ' ' . $message_id;
            }
        }

        public function get_disabled_list( $list, $options ) {
            
            if ( class_exists( 'Pricely_Pro_Admin_Page' ) ) {

                return array();
            }

            $d_list = array();

            $prem_max = count( $options );

            for ( $i = 1; $i <= $prem_max; $i++ ) {

                $d_key = 'prem_' . $i;

                if ( isset( $options[ $d_key ] ) ) {

                    $d_list[] = $d_key;
                }
            }

            return $d_list;
        }

        public function get_grouped_disabled_list( $list, $grouped_options ) {
            
            if ( class_exists( 'Pricely_Pro_Admin_Page' ) ) {

                return array();
            }

            $options = array();

            foreach ( $grouped_options as $grouped_option ) {

                if ( !isset( $grouped_option[ 'options' ] ) ) {

                    continue;
                }

                foreach ( $grouped_option[ 'options' ] as $key => $option ) {

                    $options[ $key ] = $option;
                }
            }

            return $this->get_disabled_list( $list, $options );
        }

        private function process_options( $options ) {

            return $this->process_checkout_prices_options( $options );
        }

        private function process_locale_options( $options ) {

            if ( is_null( $this->locale ) ) {

                $this->locale = Pricely_Admin_Locale::get_instance();
            }

            return $this->locale->process_options( $options );
        }

        private function process_checkout_prices_options( $options ) {

            return $this->process_cart_discounts_options( $this->checkout_prices_page->process_options( $options ) );
        }

        private function process_cart_discounts_options( $options ) {

            return $this->cart_discounts_page->process_options( $options );
        }

        private function save_css( $options ) {

            $views_css = Pricely_Views_CSS::get_instance();

            return $views_css->save_css( $options );
        }

        private function get_page_links() {

            $page_links = array(
                array(
                    'id' => 'pricely_documentation',
                    'title' => esc_html__( 'Documentation', 'pricely-dynamic-pricing-cart-discounts' ),
                    'icon' => 'fa fa-file-text',
                    'href' => esc_url( 'https://support.zendcrew.cc/portal/en/kb/woocommerce-dynamic-pricing-discounts' ),
                    'target' => '_blank',
                    'show_in' => 'both'
                ),
            );
            
            if ( !class_exists( 'Pricely_Init_Pro' ) ) {

                $page_links[] = array(
                    'id' => 'pricely_get_pro',
                    'title' => esc_html__( 'Pro Version', 'pricely-dynamic-pricing-cart-discounts' ),
                    'icon' => 'fa fa-file-text-o',
                    'href' => esc_url( '#' ),
                    'target' => '_blank',
                    'show_in' => 'both'
                );
            }
            
            if ( defined( 'PRICELY_SUPPORTS_ENABLED' ) ) {

                $page_links[] = array(
                    'id' => 'pricely_help',
                    'title' => esc_html__( 'Help', 'pricely-dynamic-pricing-cart-discounts' ),
                    'icon' => 'fa fa-question-circle',
                    'href' => esc_url( 'https://support.zendcrew.cc/portal/en/newticket' ),
                    'target' => '_blank',
                    'show_in' => 'both'
                );
            }

            return $page_links;
        }

        private function get_social_links() {

            return array(
                array(
                    'id' => 'pricely_facebook',
                    'title' => esc_html__( 'Facebook', 'pricely-dynamic-pricing-cart-discounts' ),
                    'icon' => 'fa fa-facebook',
                    'href' => esc_url( 'http://www.facebook.com/zendcrew' ),
                    'target' => '_blank',
                ),
                array(
                    'id' => 'pricely_linkedin',
                    'title' => esc_html__( 'LinkedIn', 'pricely-dynamic-pricing-cart-discounts' ),
                    'icon' => 'fa fa-linkedin',
                    'href' => esc_url( 'https://www.linkedin.com/company/zendcrew' ),
                    'target' => '_blank',
                ),
                array(
                    'id' => 'pricely_stack_overflow',
                    'title' => esc_html__( 'Stack Overflow', 'pricely-dynamic-pricing-cart-discounts' ),
                    'icon' => 'fa fa-stack-overflow',
                    'href' => esc_url( 'https://stackoverflow.com/users/8692713/zendcrew' ),
                    'target' => '_blank',
                ),
                array(
                    'id' => 'pricely_instagram',
                    'title' => esc_html__( 'Instagram', 'pricely-dynamic-pricing-cart-discounts' ),
                    'icon' => 'fa fa-instagram',
                    'href' => esc_url( 'https://www.instagram.com/zendcrew/' ),
                    'target' => '_blank',
                ),
            );
        }

    }

    Pricely_Admin_Page::get_instance();
}
