<?php
namespace admin\Elementor;

use admin\Pro_El_Widget_Map;

class Forumax_Widgets {
    public function __construct() {

        // Register Widgets
        add_action( 'elementor/widgets/register', array( $this, 'register_widgets' ) );

        // Register Category
        add_action( 'elementor/elements/categories_registered', array( $this, 'register_category' ) );
        add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'register_elementor_editor_assets' ) );

        // Register Elementor Preview Editor Scripts
        $current_theme = wp_get_theme()->get( 'Name' ) == 'Ama' ? true : false;
        $return_type   = forumax_is_premium() == true ? true : $current_theme;
        if ($return_type != 1) {
            add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'enqueue_editor_scripts' ) );
        }
    }


    /**
     * @return void
     *
     */
    public function enqueue_editor_scripts() {

        wp_enqueue_script( 'bbpc-el-editor', FORUMAX_ASSETS . 'admin/js/bbpc-el-editor.js', array(), '1.0.0', true );

        $localize_data                        = array();
        $pro_widget_map                       = new Pro_El_Widget_Map();
        $localize_data['promotional_widgets'] = $pro_widget_map->get_pro_widget_map();
        wp_localize_script( 'bbpc-el-editor', 'BbpcConfig', $localize_data );
    }


    // Register Widgets
    public function register_widgets( $widgets_manager ) {

        $theme = wp_get_theme();

        // Include Widget files
        if ($theme == 'Ama' || forumax_is_premium()) {
            require_once __DIR__ . '/Single_forum.php';
            require_once __DIR__ . '/Forum_Ajax.php';
            require_once __DIR__ . '/Forum_Tab.php';
            require_once __DIR__ . '/Search.php';

            $widgets_manager->register( new Single_forum() );
            $widgets_manager->register( new Forum_Ajax() );
            $widgets_manager->register( new Forum_Tab() );
            $widgets_manager->register( new Search() );
        }

        if ($theme == 'Docy') {
            require_once __DIR__ . '/Single_forum.php';
            $widgets_manager->register( new Single_forum() );
        }

        // Docy theme widgets - always load (work with any theme)
        require_once __DIR__ . '/Forumax_Forums.php';
        require_once __DIR__ . '/Forumax_Topics.php';

        $widgets_manager->register( new Forumax_Forums() );
        $widgets_manager->register( new Forumax_Topics() );
    }

    // Register category
    public function register_category( $elements_manager ) {
        $elements_manager->add_category(
            'forumax',
            array(
                'title' => __( 'Forumax', 'forumax' ),
            )
        );
    }

    public function register_elementor_editor_assets() {
        wp_enqueue_style( 'bbpc-el-editor', FORUMAX_ADMIN_ASS . 'css/elementor-editor.css' );
    }
}
