<?php

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

class SyrusAssetsHandler { 

    public function initialize() {
        $this->register_css();
        $this->register_js();
    }

    /**
     * 
     * Methods on initialize
     * 
     */
    private function register_css() {
        global $syrus;

        foreach($this->get_css_scheme() as $handle => $info) {
            wp_register_style($handle, $syrus->get_css_dir() . $info['partial_url'], $info['dep'], $info['ver']);
        }
    }

    private function register_js() {
        global $syrus;


        foreach($this->get_js_scheme() as $handle => $info) {
            wp_register_script($handle, $syrus->get_js_dir() . $info['partial_url'], $info['dep'], $info['ver']);

            if(!isset($info['var_name'])) 
                continue;

            wp_localize_script($handle,$info['var_name'],$info['vars']);
        }
    }

    /**
     * 
     * Callable methods
     * 
     */
    public function enqueue_css() {
        global $syrus;
        $current_layout = $syrus->get_current_layout();
        $wa_settings = get_option("syrus_theme_whatsapp_settings");
        $wa_settings = $wa_settings ? unserialize($wa_settings) : [];

        $bootstrap_css = $syrus->get_option('generali', 'enqueue_css_bootstrap-css');
        $bootstrap_icons_css = $syrus->get_option('generali', 'enqueue_css_bootstrap-icons-css');
        $header_css = $syrus->get_option('generali', 'enqueue_css_header-css');
        $footer_css = $syrus->get_option('generali', 'enqueue_css_footer-css');
        $sidebar_css = $syrus->get_option('generali', 'enqueue_css_sidebar-css');
        $default_css = $syrus->get_option('generali', 'enqueue_css_default-css');
        $post_css = $syrus->get_option('generali', 'enqueue_css_post-css');
        $single_post_css = $syrus->get_option('generali', 'enqueue_css_single-post-css');
        $layout_2_css = $syrus->get_option('generali', 'enqueue_css_layout-2-css');

        if($bootstrap_css == '' || $bootstrap_css == 'y')
            wp_enqueue_style('bootstrap-css');

        if($bootstrap_icons_css == '' || $bootstrap_icons_css == 'y')
            wp_enqueue_style('bootstrap-icons-css');

        if($header_css == '' || $header_css == 'y')
            wp_enqueue_style('header-css');

        if($footer_css == '' || $footer_css == 'y')
            wp_enqueue_style('footer-css');

        if($sidebar_css == '' || $sidebar_css == 'y')
            wp_enqueue_style('sidebar-css');

        wp_enqueue_style('dashicons');

        if($default_css == '' || $default_css == 'y') {
            wp_enqueue_style('default-css');

            $wa_inline_css = '
                #syrus-chat-container {
                    display: ' . (!isset($wa_settings['enable_popup_desktop']) || !$wa_settings['enable_popup_desktop'] ? 'none' : 'block') . ' !important;
                }
    
                @media only screen and (max-width: 670px) {
                    #syrus-chat-container {
                        display: ' . (!isset($wa_settings['enable_popup_mobile']) || !$wa_settings['enable_popup_mobile'] ? 'none' : 'block') . ' !important;
                    }
                }
            ';
    
            wp_add_inline_style('default-css', $wa_inline_css);
        }

        if(($post_css == '' || $post_css == 'y') && (is_home() || is_category() || is_search())) {
            wp_enqueue_style('post-css'); 
        }

        if(($single_post_css == '' || $single_post_css == 'y') && (is_single() || is_page() || is_404())) {
            wp_enqueue_style('single-post-css'); 
        }

        if(($layout_2_css == '' || $layout_2_css == 'y') && $current_layout == 2) {
            wp_enqueue_style('layout-2-css');

            $main_menu_color = sanitize_text_field(get_theme_mod('main-menu-color','#ffffff'));
            $main_menu_hover_color = sanitize_text_field(get_theme_mod('main-menu-hover-color','#feb200'));
            $site_title_color = sanitize_text_field(get_theme_mod('site-title-color','#feb200'));
            $tagline_color = sanitize_text_field(get_theme_mod('tagline-color','#ffffff'));
            $home_page_banner_bg_color = sanitize_text_field(get_theme_mod('home-page-banner-bg-color','#000000'));
            $home_page_banner_color = sanitize_text_field(get_theme_mod('home-page-banner-color','#ffffff'));

            
            $inline_css = "
                :root {
                    --syrus-main-menu-color: $main_menu_color;
                    --syrus-main-menu-hover-color: $main_menu_hover_color;
                    --syrus-site-title-color: $site_title_color;
                    --syrus-tagline-color: $tagline_color;
                    --syrus-home-page-banner-bg-color: $home_page_banner_bg_color;
                    --syrus-home-page-banner-color: $home_page_banner_color;
                }
            ";

            wp_add_inline_style('layout-2-css',$inline_css);
        }

    }

    public function enqueue_js() {
        global $syrus;
        $current_layout = $syrus->get_current_layout();
        $wa_settings = get_option("syrus_theme_whatsapp_settings");
        $wa_settings = $wa_settings ? unserialize($wa_settings) : [];

        $infinite_scroll_home = $syrus->get_option("generali","infinite_scroll_home");
        $infinite_scroll_single = $syrus->get_option("generali","infinite_scroll");

        $bootstrap_js = $syrus->get_option('generali', 'enqueue_js_bootstrap-js');
        $footer_js = $syrus->get_option('generali', 'enqueue_js_footer-js');
        $default_js = $syrus->get_option('generali', 'enqueue_js_default-js');
        $infinite_scroll_js = $syrus->get_option('generali', 'enqueue_js_infinite-scroll-js');
        $options_ads_single_post_js = $syrus->get_option('generali', 'enqueue_js_options-ads-single-post-js');
        $layout_2_js = $syrus->get_option('generali', 'enqueue_js_layout-2-js');

        if($bootstrap_js == '' || $bootstrap_js == 'y')
            wp_enqueue_script('bootstrap-js');

        if($footer_js == '' || $footer_js == 'y')
            wp_enqueue_script('footer-js');

        if($default_js == '' || $default_js == 'y') {
            wp_enqueue_script('default-js');
    
            $wa_inline_js = '
                const WA_SETTINGS = ' . json_encode($wa_settings) . ';
            ';
    
            wp_add_inline_script('default-js', $wa_inline_js, 'before');
        }

        if($infinite_scroll_home == 1 && is_home() && ($infinite_scroll_js == '' || $infinite_scroll_js == 'y')) {
            wp_localize_script('infinite-scroll-js','args_infinite_scroll',[
                'current_version' => "0.1.2",
                'ajax_url' => admin_url( 'admin-ajax.php' ),
                'home_page' => 1
            ]);
            wp_enqueue_script('infinite-scroll-js');
        }

        if(is_single() && ($options_ads_single_post_js == '' || $options_ads_single_post_js == 'Y')) {
            $syrus_theme_options_ads = get_post_meta(get_the_ID(), 'syrus_theme_options_ads', true) ?: serialize([]);
            $syrus_theme_options_ads = unserialize($syrus_theme_options_ads);

            wp_enqueue_script('options-ads-single-post-js');

            wp_add_inline_script('options-ads-single-post-js', '
                const SYRUS_THEME_OPTIONS_ADS = ' . json_encode($syrus_theme_options_ads) . '
            ', 'before');
        }

        if($infinite_scroll_single == 1 && is_single() && ($infinite_scroll_js == '' || $infinite_scroll_js == 'y')) {
            wp_enqueue_script('infinite-scroll-js');
        }

        if($current_layout == 2 && ($layout_2_js == '' || $layout_2_js == 'y'))
            wp_enqueue_script('layout-2-js');
    }

    public function enqueue_customizer_js() {
        wp_enqueue_script('syrus-customizer-js');
    }

    public function admin_enqueue_css() {
        global $syrus;
        $current_layout = $syrus->get_current_layout();
        $page = isset($_GET['page']) ? $_GET['page'] : "";
        
        wp_dequeue_style('wp-admin-css');
        wp_enqueue_style('admin-css');

        if(strpos($page,'syrus-theme-') !== false) {
            wp_enqueue_style('bootstrap-css');
        }

        if(is_customize_preview()) {
            if($current_layout == 1)
                wp_enqueue_style('header-css');


            if($current_layout == 2)
                wp_enqueue_style('layout-2-css');

        }

        
        if($page === "syrus-theme-page") {
            // NEW
            wp_enqueue_style('bootstrap-icons-css');
            wp_enqueue_style('wp-admin-css');
        }

    }

    public function admin_enqueue_js() {
        global $pagenow;
        global $syrus;
        $admin_page = isset($_GET['page']) && $_GET['page'] ? $_GET['page'] : null;
        $admin_tab = isset($_GET['tab']) && $_GET['tab'] ? $_GET['tab'] : null;

        $current_layout = $syrus->get_current_layout();

        wp_enqueue_script('custom-image-uploader-js');

        if($pagenow == "post-new.php" || $pagenow == "post.php") {
            wp_enqueue_script('admin-post-page-js');
        }

        if(is_customize_preview()) {
            if($current_layout == 2)
                wp_enqueue_script('layout-2-js');
        }


        if($admin_page == 'syrus-theme-page') {
            wp_enqueue_script('syrus-admin-js');
        }
    }


    /**
     * 
     * Private methods
     * 
     */
    public function get_css_scheme() {
        return [
            'bootstrap-css' => [
                'partial_url' => "bootstrap.min.css",
                'dep' => [],
                'ver' => "5.3.2",
            ],
            'header-css' => [
                'partial_url' => "header.css",
                'dep' => [],
                'ver' => "0.1.3",
            ],
            'post-css' => [
                'partial_url' => "home-posts.css",
                'dep' => [],
                'ver' => "0.2.4",
            ],
            'single-post-css' => [
                'partial_url' => "single-post.css",
                'dep' => [],
                'ver' => "0.2.2", 
            ],
            'footer-css' => [
                'partial_url' => "footer.css",
                'dep' => [],
                'ver' => "0.1.0",
            ],
            'sidebar-css' => [
                'partial_url' => "sidebar.css",
                'dep' => [],
                'ver' => "0.2.0",
            ],
            'admin-css' => [
                'partial_url' => "admin.css",
                'dep' => ['dashicons'],
                'ver' => "0.3.8",
            ],
            'default-css' => [
                'partial_url' => "default.css",
                'dep' => [],
                'ver' => "0.0.6"
                // 'ver' => md5(uniqid())
            ],
            'layout-2-css' => [
                'partial_url' => 'layout-2.css',
                'dep' => [],
                'ver' => "0.0.2"
            ],
            'wp-admin-css' => [
                'partial_url' => 'wp-admin.css',
                'dep' => [],
                'ver' => "0.3.8"
            ],
            'bootstrap-icons-css' => [
                'partial_url' => 'bootstrap-icons.min.css',
                'dep' => [],
                'ver' => "1.11.1" 
            ]
        ];
    }

    public function get_js_scheme() {
        global $syrus;

        $blocchi_ads = [
            $syrus->get_option('ads','post_blocco_1'),
            $syrus->get_option('ads','post_blocco_2'),
            $syrus->get_option('ads','post_blocco_3')
        ];

        return [

            'bootstrap-js' => [
                'partial_url' => "bootstrap.bundle.min.js",
                'dep' => ['jquery'],
                'ver' => "5.3.2",
            ],

            'footer-js' => [
                'partial_url' => "footer.js",
                'dep' => ['jquery'],
                'ver' => "0.0.8",
                'var_name' => "args_footer",
                'vars' => [
                    'current_version' => "0.0.8"
                ] 
            ],

            'options-ads-single-post-js' => [
                'partial_url' => "options-ads-single-post.js",
                'dep' => ['jquery'],
                'ver' => "0.0.7",
                'var_name' => 'args_single_post',
                'vars' => [
                    'blocchi' => $blocchi_ads
                ]
            ],

            'infinite-scroll-js' => [
                'partial_url' => "infinite-scroll.js",
                'dep' => ['jquery'],
                'ver' => "0.1.2",
                'var_name' => 'args_infinite_scroll',
                'vars' => [
                    'current_version' => "0.1.2",
                    'ajax_url' => admin_url('admin-ajax.php'),
                ]
            ],

            'custom-image-uploader-js' => [
                'partial_url' => "custom-image-uploader.js",
                'dep' => ['jquery'],
                'ver' => "0.1.0",
                'var_name' => "args_uploader",
                'vars' => [
                    'current_version' => "0.1.0",
                    'can_user_upload' => current_user_can('syrus_theme_upload_files')
                ]
            ],

            'admin-post-page-js' => [
                'partial_url' => "admin-post-page.js",
                'dep' => ['jquery'],
                'ver' => "0.0.2",
            ],

            'layout-2-js' => [
                'partial_url' => 'layout-2.js',
                'dep' => ['jquery'],
                'ver' => "0.0.4"
            ],

            'syrus-admin-js' => [
                'partial_url' => 'admin.js',
                'dep' => ['jquery'],
                'ver' => "0.1.3"
            ],

            'default-js' => [
                'partial_url'   => 'default.js',
                'dep'           => ['jquery'],
                'ver'           => '0.0.6',
                // 'ver'           => md5(uniqid()),
            ],

            'syrus-customizer-js' => [
                'partial_url' => 'customizer.js',
                'dep' => ['jquery', 'customize-controls'],
                'ver' => "0.0.2"
            ]
          
        ];
    }
}