<?php

namespace ThemesflatAddons\ThemeBuilder;

use Elementor\Plugin;
use ElementorPro\Modules\ThemeBuilder\Classes\Locations_Manager;
use Elementor\Core\Documents_Manager;

use ThemesflatAddons\ThemeBuilder\Widgets\Grid_Item_Widget;

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

if (!class_exists('TFL_Theme_Builder_Init')):

    class TFL_Theme_Builder_Init {

        public function __construct() {

            $this->setup_constants();

            $this->includes();

            $this->hooks();
        }

        private function setup_constants() {

            // Plugin Folder Path
            if (!defined('TFL_THEME_BUILDER_DIR')) {
                define('TFL_THEME_BUILDER_DIR', TFL_PLUGIN_DIR . 'includes/theme-builder/');
            }

        }

        private function includes() {

            require_once TFL_THEME_BUILDER_DIR . 'functions/utils.php';

        }

        private function hooks() {
            
            add_action('elementor/theme/register_locations', array($this, 'register_locations'));
            add_action('elementor/widgets/widgets_registered', array($this, 'register_widgets'));
        }

        public function register_documents(Documents_Manager $documents_manager) {

            /* TODO: Look for a different hook to initialize documents - elementor_pro/init hook is not being called for maintenance tasks */
            if (!class_exists('\ThemesflatAddons\ThemeBuilder\Themesflat_Item') || !class_exists('\ThemesflatAddons\ThemeBuilder\Themesflat_Grid')) {

                $this->initialize_documents();
            }

            $documents_manager->register_document_type('themesflat_item', Themesflat_Item::get_class_full_name());

            $documents_manager->register_document_type('themesflat_grid', Themesflat_Grid::get_class_full_name());

        }

        public function register_locations(Locations_Manager $location_manager) {

            $location_manager->register_location(
                'themesflat_item',
                [
                    'label' => __('Themesflat Item', 'themesflat'),
                    'multiple' => true,
                    'edit_in_content' => true,
                ]
            );

            $location_manager->register_location(
                'themesflat_grid',
                [
                    'label' => __('Themesflat Grid', 'themesflat'),
                    'multiple' => true,
                    'edit_in_content' => true,
                ]
            );

        }

        public function register_widgets() {

            require_once TFL_THEME_BUILDER_DIR . 'widgets/grid-item.php';

            Plugin::instance()->widgets_manager->register_widget_type(new Grid_Item_Widget());

        }

    }

endif;

new TFL_Theme_Builder_Init();
