<?php

/**
 * Class For Builder
 */
class DaltechBuilder
{

	function __construct()
	{
		// register admin menus
		add_action('admin_menu', [$this, 'register_settings_menus']);

		// Custom Footer Builder With Post Type
		add_action('init', [$this, 'post_type'], 0);

		add_action('elementor/frontend/after_enqueue_scripts', [$this, 'widget_scripts']);

		add_filter('single_template', [$this, 'load_canvas_template']);

		add_action('elementor/element/wp-page/document_settings/after_section_end', [$this, 'daltech_add_elementor_page_settings_controls'], 10, 2);
	}

	public function widget_scripts()
	{
		wp_enqueue_script('daltech-core', DALTECH_PLUGDIRURI . 'assets/js/daltech-core.js', array('jquery'), '1.0', true);
	}


	public function daltech_add_elementor_page_settings_controls(\Elementor\Core\DocumentTypes\Page $page)
	{

		$page->start_controls_section(
			'daltech_header_option',
			[
				'label'     => __('Header Option', 'daltech'),
				'tab'       => \Elementor\Controls_Manager::TAB_SETTINGS,
			]
		);


		$page->add_control(
			'daltech_header_style',
			[
				'label'     => __('Header Option', 'daltech'),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'options'   => [
					'prebuilt'             => __('Pre Built', 'daltech'),
					'header_builder'       => __('Header Builder', 'daltech'),
				],
				'default'   => 'prebuilt',
			]
		);

		$page->add_control(
			'daltech_header_builder_option',
			[
				'label'     => __('Header Name', 'daltech'),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'options'   => $this->daltech_header_choose_option(),
				'condition' => ['daltech_header_style' => 'header_builder'],
				'default'	=> ''
			]
		);

		$page->end_controls_section();

		$page->start_controls_section(
			'daltech_footer_option',
			[
				'label'     => __('Footer Option', 'daltech'),
				'tab'       => \Elementor\Controls_Manager::TAB_SETTINGS,
			]
		);
		$page->add_control(
			'daltech_footer_choice',
			[
				'label'         => __('Enable Footer?', 'daltech'),
				'type'          => \Elementor\Controls_Manager::SWITCHER,
				'label_on'      => __('Yes', 'daltech'),
				'label_off'     => __('No', 'daltech'),
				'return_value'  => 'yes',
				'default'       => 'yes',
			]
		);
		$page->add_control(
			'daltech_footer_style',
			[
				'label'     => __('Footer Style', 'daltech'),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'options'   => [
					'prebuilt'             => __('Pre Built', 'daltech'),
					'footer_builder'       => __('Footer Builder', 'daltech'),
				],
				'default'   => 'prebuilt',
				'condition' => ['daltech_footer_choice' => 'yes'],
			]
		);
		$page->add_control(
			'daltech_footer_builder_option',
			[
				'label'     => __('Footer Name', 'daltech'),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'options'   => $this->daltech_footer_choose_option(),
				'condition' => ['daltech_footer_style' => 'footer_builder', 'daltech_footer_choice' => 'yes'],
				'default'	=> ''
			]
		);

		$page->end_controls_section();
	}

	public function register_settings_menus()
	{
		add_menu_page(
			esc_html__('Daltech Builder', 'daltech'),
			esc_html__('Daltech Builder', 'daltech'),
			'manage_options',
			'daltech',
			[$this, 'register_settings_contents__settings'],
			'dashicons-admin-site',
			2
		);

		add_submenu_page('daltech', esc_html__('Footer Builder', 'daltech'), esc_html__('Footer Builder', 'daltech'), 'manage_options', 'edit.php?post_type=daltech_footer');
		add_submenu_page('daltech', esc_html__('Header Builder', 'daltech'), esc_html__('Header Builder', 'daltech'), 'manage_options', 'edit.php?post_type=daltech_header');
		add_submenu_page('daltech', esc_html__('Tab Builder', 'daltech'), esc_html__('Tab Builder', 'daltech'), 'manage_options', 'edit.php?post_type=daltech_tab_builder');
	}

	// Callback Function
	public function register_settings_contents__settings()
	{
		echo '<h2>';
		echo esc_html__('Welcome To Header And Footer Builder Of This Theme', 'daltech');
		echo '</h2>';
	}

	public function post_type()
	{

		$labels = array(
			'name'               => __('Footer', 'daltech'),
			'singular_name'      => __('Footer', 'daltech'),
			'menu_name'          => __('daltech Footer Builder', 'daltech'),
			'name_admin_bar'     => __('Footer', 'daltech'),
			'add_new'            => __('Add New', 'daltech'),
			'add_new_item'       => __('Add New Footer', 'daltech'),
			'new_item'           => __('New Footer', 'daltech'),
			'edit_item'          => __('Edit Footer', 'daltech'),
			'view_item'          => __('View Footer', 'daltech'),
			'all_items'          => __('All Footer', 'daltech'),
			'search_items'       => __('Search Footer', 'daltech'),
			'parent_item_colon'  => __('Parent Footer:', 'daltech'),
			'not_found'          => __('No Footer found.', 'daltech'),
			'not_found_in_trash' => __('No Footer found in Trash.', 'daltech'),
		);

		$args = array(
			'labels'              => $labels,
			'public'              => true,
			'rewrite'             => false,
			'show_ui'             => true,
			'show_in_menu'        => false,
			'show_in_nav_menus'   => false,
			'exclude_from_search' => true,
			'capability_type'     => 'post',
			'hierarchical'        => false,
			'supports'            => array('title', 'elementor'),
		);

		register_post_type('daltech_footer', $args);

		$labels = array(
			'name'               => __('Header', 'daltech'),
			'singular_name'      => __('Header', 'daltech'),
			'menu_name'          => __('daltech Header Builder', 'daltech'),
			'name_admin_bar'     => __('Header', 'daltech'),
			'add_new'            => __('Add New', 'daltech'),
			'add_new_item'       => __('Add New Header', 'daltech'),
			'new_item'           => __('New Header', 'daltech'),
			'edit_item'          => __('Edit Header', 'daltech'),
			'view_item'          => __('View Header', 'daltech'),
			'all_items'          => __('All Header', 'daltech'),
			'search_items'       => __('Search Header', 'daltech'),
			'parent_item_colon'  => __('Parent Header:', 'daltech'),
			'not_found'          => __('No Header found.', 'daltech'),
			'not_found_in_trash' => __('No Header found in Trash.', 'daltech'),
		);

		$args = array(
			'labels'              => $labels,
			'public'              => true,
			'rewrite'             => false,
			'show_ui'             => true,
			'show_in_menu'        => false,
			'show_in_nav_menus'   => false,
			'exclude_from_search' => true,
			'capability_type'     => 'post',
			'hierarchical'        => false,
			'supports'            => array('title', 'elementor'),
		);

		register_post_type('daltech_header', $args);

		$labels = array(
			'name'               => __('Tab Builder', 'daltech'),
			'singular_name'      => __('Tab Builder', 'daltech'),
			'menu_name'          => __('daltech Tab Builder', 'daltech'),
			'name_admin_bar'     => __('Tab Builder', 'daltech'),
			'add_new'            => __('Add New', 'daltech'),
			'add_new_item'       => __('Add New Tab Builder', 'daltech'),
			'new_item'           => __('New Tab Builder', 'daltech'),
			'edit_item'          => __('Edit Tab Builder', 'daltech'),
			'view_item'          => __('View Tab Builder', 'daltech'),
			'all_items'          => __('All Tab Builder', 'daltech'),
			'search_items'       => __('Search Tab Builder', 'daltech'),
			'parent_item_colon'  => __('Parent Tab Builder:', 'daltech'),
			'not_found'          => __('No Tab Builder found.', 'daltech'),
			'not_found_in_trash' => __('No Tab Builder found in Trash.', 'daltech'),
		);

		$args = array(
			'labels'              => $labels,
			'public'              => true,
			'rewrite'             => false,
			'show_ui'             => true,
			'show_in_menu'        => false,
			'show_in_nav_menus'   => false,
			'exclude_from_search' => true,
			'capability_type'     => 'post',
			'hierarchical'        => false,
			'supports'            => array('title', 'elementor'),
		);

		register_post_type('daltech_tab_builder', $args);
	}

	function load_canvas_template($single_template)
	{

		global $post;

		if ('daltech_footer' == $post->post_type || 'daltech_header' == $post->post_type || 'daltech_tab_builder' == $post->post_type) {

			$elementor_2_0_canvas = ELEMENTOR_PATH . '/modules/page-templates/templates/canvas.php';

			if (file_exists($elementor_2_0_canvas)) {
				return $elementor_2_0_canvas;
			} else {
				return ELEMENTOR_PATH . '/includes/page-templates/canvas.php';
			}
		}

		return $single_template;
	}

	public function daltech_footer_choose_option()
	{

		$daltech_post_query = new WP_Query(array(
			'post_type'			=> 'daltech_footer',
			'posts_per_page'	    => -1,
		));

		$daltech_builder_post_title = array();
		$daltech_builder_post_title[''] = __('Select a Footer', 'daltech');

		while ($daltech_post_query->have_posts()) {
			$daltech_post_query->the_post();
			$daltech_builder_post_title[get_the_ID()] =  get_the_title();
		}
		wp_reset_postdata();

		return $daltech_builder_post_title;
	}

	public function daltech_header_choose_option()
	{

		$daltech_post_query = new WP_Query(array(
			'post_type'			=> 'daltech_header',
			'posts_per_page'	    => -1,
		));

		$daltech_builder_post_title = array();
		$daltech_builder_post_title[''] = __('Select a Header', 'daltech');

		while ($daltech_post_query->have_posts()) {
			$daltech_post_query->the_post();
			$daltech_builder_post_title[get_the_ID()] =  get_the_title();
		}
		wp_reset_postdata();

		return $daltech_builder_post_title;
	}
}

$builder_execute = new DaltechBuilder();
