<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Main' ) ) {

    class Pricely_Main {

        private static $instance;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            if ( !defined( 'PRICELY_ASSETS_URL' ) ) {

                define( 'PRICELY_ASSETS_URL', plugins_url( 'assets/', __FILE__ ) );
            }

            if ( is_admin() ) {

                add_action( 'init', array( $this, 'load_admin_page' ) );
                add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 20 );
            } else {

                add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_public_scripts' ), 9999 );
            }

            add_action( 'woocommerce_init', array( $this, 'init_public' ) );
        }

        public function load_admin_page() {

            require_once (dirname( __FILE__ ) . '/admin/admin.php');
        }

        public function init_public() {

            require_once (dirname( __FILE__ ) . '/compatibility/compatibility.php');
            require_once dirname( __FILE__ ) . '/public/views/views.php';
            require_once dirname( __FILE__ ) . '/public/views/view-css/view-css.php';

            if ( !is_admin() || (defined( 'DOING_AJAX' ) && DOING_AJAX) ) {

                require_once dirname( __FILE__ ) . '/public/pricely.php';
                require_once dirname( __FILE__ ) . '/public/pipelines/pipeline.php';
                require_once dirname( __FILE__ ) . '/public/views/views-data/views-data.php';
            }
        }

        public function enqueue_admin_scripts( $hook ) {

            wp_enqueue_style( 'pricely-admin', PRICELY_ASSETS_URL . 'admin-styles.min.css', array(), '1.0', 'all' );
        }

        public function enqueue_public_scripts() {

            //Include CSS
            //-----------
            wp_enqueue_style( 'pricely-public', PRICELY_ASSETS_URL . 'public-styles.min.css', array(), '1.0', 'all' );


            //Include Custom CSS
            //------------------
            $views_css = Pricely_Views_CSS::get_instance();

            if ( $views_css->is_using_external_css() ) {

                $custom_css_url = $views_css->get_custom_css_url();
                $custom_css_ver = $views_css->get_custom_css_ver();
                $custom_css_dep = class_exists( 'Pricely_Main_Pro' ) ? array( 'pricely-pro-public' ) : array( 'pricely-public' );

                wp_enqueue_style( 'pricely-custom', $custom_css_url, $custom_css_dep, $custom_css_ver, 'all' );
            } else {

                $custom_css = $views_css->get_css();

                if ( $custom_css != '' ) {

                    wp_add_inline_style( 'pricely-public', $custom_css );
                }
            }

            //Include Scripts
            //------------------
            wp_enqueue_script( 'pricely-countdown', PRICELY_ASSETS_URL . 'pricely-countdown.min.js', array( 'jquery' ), '2.2', true );
            wp_enqueue_script( 'pricely-public', PRICELY_ASSETS_URL . 'public-scripts.min.js', array( 'pricely-countdown' ), '1.0', true );

            //Include Script Params
            //----------------------
            $this->get_script_params();
        }

        private function get_script_params() {

            $script_params = apply_filters( 'pricely_get-script-params', array() );

            wp_localize_script( 'pricely-public', 'pricely', $script_params );
        }

        public function get_allow_html() {

            $allowed_html = array(
                'br' => array(),
                'span' => array(
                    'id' => true,
                    'title' => true,
                    'class' => true,
                ),
                'small' => array(
                    'id' => true,
                    'title' => true,
                    'class' => true,
                ),
                'a' => array(
                    'id' => true,
                    'href' => true,
                    'title' => true,
                    'class' => true,
                    'target' => true,
                ),
                'strong' => array(
                    'id' => true,
                    'title' => true,
                    'class' => true,
                ),
                'b' => array(
                    'id' => true,
                    'title' => true,
                    'class' => true,
                ),
                'p' => array(
                    'id' => true,
                    'title' => true,
                    'class' => true,
                ),
                'i' => array(
                    'id' => true,
                    'title' => true,
                    'class' => true,
                ),
                'bdi' => array(),
                'del' => array(),
            );

            return $allowed_html;
        }

    }

}
