<?php

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

if ( !class_exists( 'Reon_UI' ) ) {

    class Reon_UI {

        private static $instance;
        private static $dyn_contents = array();
        
        public static function get_instance(): self {

            if ( is_null( self::$instance ) ) {

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

            return self::$instance;
        }

        public static function init() {
            add_action( 'admin_enqueue_scripts', array( new self(), 'enqueue_admin_assets' ), 30 );
        }

        public static function enqueue_admin_assets() {
            self::register_styles();
            self::register_scripts();
            self::enqueue_styles();
            self::enqueue_scripts();
        }

        public static function set_dynamic_contents( $contents, $key ) {

            if ( !isset( self::$dyn_contents[ $key ] ) ) {

                self::$dyn_contents[ $key ] = $contents;
            }
        }

        public static function get_dynamic_contents() {

            $contents = '';

            foreach ( self::$dyn_contents as $key => $dyn_content ) {

                $contents .= $dyn_content;
            }

            return $contents;
        }

        public static function get_dynamic_allowed_htmls() {

            $default_tag = array(
                'id' => true,
                'class' => true,
                'style' => true,
                'data-*' => true
            );

            $allowed_htmls = array(
                'div' => $default_tag,
                'span' => $default_tag,
                'label' => $default_tag,
                'input' => array(
                    'type' => true,
                    'id' => true,
                    'class' => true,
                    'style' => true,
                    'name' => true,
                    'value' => true,
                    'min' => true,
                    'max' => true,
                    'step' => true,
                    'placeholder' => true,
                    'data-*' => true
                ),
                'select' => array(
                    'id' => true,
                    'class' => true,
                    'style' => true,
                    'name' => true,
                    'multiple' => true,
                    'placeholder' => true,
                    'data-*' => true
                ),
                'optgroup' => array(
                    'label' => true
                ),
                'option' => array(
                    'value' => true,
                    'selected' => true,
                    'disabled' => true
                ),
            );

            return $allowed_htmls;
        }

        private static function register_styles() {

            $style_path = REON_ASSETS . '/css/';
            $script_path = REON_ASSETS . '/js/';

            wp_register_style( 'fontawsome', $style_path . 'fontawesome/css/all.min.css', array(), '6.1.2' );
            wp_register_style( 'spectrum', $style_path . 'spectrum.css', array(), '1.8.0' );
            wp_register_style( 'nouislider', $style_path . 'nouislider.min.css', array(), '8.5.1' );
            wp_register_style( 'reon-datetime-picker-ui', $style_path . 'datetime-picker-ui.min.css', array(), '1.14.2' );
            wp_register_style( 'timepicker_addon', $script_path . 'timepicker-addon/timepicker-addon.min.css', array(), '1.6.3' );

            wp_register_style( 'reon-admin', $style_path . 'admin.css', array(), '1.0' );

            do_action( 'reon_register-styles' );
        }

        private static function register_scripts() {

            $scripts_path = REON_ASSETS . '/js/';

            wp_register_script( 'serializeform', $scripts_path . 'serializeform.min.js', array( 'jquery' ), '1.2.1', true );
            wp_register_script( 'reon-ui-option-page', $scripts_path . 'reon-ui-option-page.js', array( 'jquery' ), '1.0', true );
            wp_register_script( 'reon-ui-meta-box', $scripts_path . 'reon-ui-meta-box.js', array( 'jquery' ), '1.0', true );
            wp_register_script( 'spectrum', $scripts_path . 'spectrum.js', array( 'jquery' ), '1.0', true );
            wp_register_script( 'wnumb', $scripts_path . 'wNumb.js', array( 'jquery' ), '1.1', true );
            wp_register_script( 'nouislider', $scripts_path . 'nouislider.min.js', array( 'jquery' ), '8.5.1', true );
            wp_register_script( 'reon-noui', $scripts_path . 'reon-noui.js', array( 'jquery' ), '1.1', true );
            wp_register_script( 'buttonset', $scripts_path . 'buttonset.js', array( 'jquery' ), '1.0', true );
            wp_register_script( 'repeater', $scripts_path . 'repeater.js', array( 'jquery', 'jquery-ui-sortable' ), '1.0', true );
            wp_register_script( 'foldjs', $scripts_path . 'foldjs.js', array( 'jquery' ), '1.0', true );
            wp_register_script( 'timepicker-addon', $scripts_path . 'timepicker-addon/timepicker-addon.min.js', array( 'jquery' ), '1.0', true );
            wp_register_script( 'reon-datetime', $scripts_path . 'reon-datetime.js', array( 'jquery' ), '1.0', true );
            wp_register_script( 'reon-autoid', $scripts_path . 'reon-autoid.js', array( 'jquery' ), '1.0', true );

            do_action( 'reon_register-scripts' );
        }

        private static function enqueue_styles() {

            wp_enqueue_style( 'fontawsome' );
            wp_enqueue_style( 'spectrum' );
            wp_enqueue_style( 'select2' );
            wp_enqueue_style( 'reon-datetime-picker-ui' );
            wp_enqueue_style( 'timepicker_addon' );
            wp_enqueue_style( 'nouislider' );

            wp_enqueue_style( 'reon-admin' );

            do_action( 'reon_enqueue-styles' );
        }

        private static function enqueue_scripts() {
            $deps = array(
                'jquery-ui-spinner',
                'jquery-ui-slider',
                'serializeform',
                'reon-ui-option-page',
                'reon-ui-meta-box',
                'spectrum',
                'wnumb',
                'nouislider',
                'reon-noui',
                'selectWoo',
                'buttonset',
                'jquery-ui-datepicker',
                'repeater',
                'timepicker-addon',
                'wc-jquery-tiptip',
                'reon-datetime',
                'reon-autoid',
                'foldjs',
            );


            $scripts_path = REON_ASSETS . '/js/';
            wp_enqueue_script( 'reon-ui', $scripts_path . 'reon-ui.js', $deps, '1.2', array( 'in_footer' => true ) );

            $client_variables = array(
                'ajax_url' => admin_url( 'admin-ajax.php?reon-admin-ajax=1' ),
                'admin_nonce' => Reon_Option_Page::get_nonce(),
                'culture' => apply_filters( 'reon_get-culture', array() ),
            );

            wp_localize_script( 'reon-ui', 'reon_ui', $client_variables );
        }

    }

}