<?php
namespace Sifency\Traits;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Controls_Manager;
/**
 * Trait Sifency_Addons_Widget_Config_And_Help
 * This trait handles both Widget Configuration (name, title, icon, etc.)
 * and the Elementor Help Tab controls.
 */
trait Sifency_Addons_Help_Controls {

	/**
	 * Widget configuration array.
	 * Must be defined in the __construct() of the widget class.
	 *
	 * @var array
	 */
	protected $wg_config = array();

	/*
	=========================================================
	 * WIDGET CONFIGURATION METHODS
	 * ========================================================= */

	public function get_name() {
		return $this->wg_config['name'] ?? 'widget-default';
	}

	public function get_title() {
		$title = $this->wg_config['title'] ?? '';
		if ( is_callable( $title ) ) {
			return $title();
		}
		return ! empty( $title )
			? esc_html( $title )
			: esc_html__( 'Widget', 'sifency-addons' );
	}
	public function get_icon() {
		return $this->wg_config['icon'] ?? 'eicon-slide';
	}

	public function get_categories() {
		$cat = $this->wg_config['category'] ?? 'sifency-addons-widgets';
		return is_array( $cat ) ? $cat : array( $cat );
	}

	public function get_keywords() {
		return $this->wg_config['keywords'] ?? array();
	}

	public function get_script_depends() {
		$scripts = $this->wg_config['script_depends'] ?? array();
		return is_array( $scripts ) ? $scripts : array( $scripts );
	}

	public function get_style_depends() {
		$styles = $this->wg_config['style_depends'] ?? array();
		return is_array( $styles ) ? $styles : array( $styles );
	}

	/*
	=========================================================
	 * WIDGET HELP TAB CONTROLS
	 * ========================================================= */

	/**
	 * Add Help Section in Elementor Controls
	 *
	 * @param array $args Optional override arguments for URLs.
	 */
	protected function add_sifency_addons_help_controls( array $args = array() ) {

		$widget_name = $this->get_name();
		$widget_meta = $this->get_sifency_widget_meta( $widget_name );

		$this->start_controls_section(
			'section_sifency_help_tab',
			array(
				'label' => esc_html__( 'Help', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'sifency_help_links_html',
			array(
				'type' => Controls_Manager::RAW_HTML,
				'raw'  => $this->generate_sifency_help_html( $widget_name, $widget_meta, $args ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Get widget meta data from the centralized PHP file.
	 * Uses static caching to prevent multiple file reads.
	 *
	 * @param string $name Widget slug
	 * @return array
	 */
	private function get_sifency_widget_meta( $name ) {
		if ( class_exists( '\Sifency_Addons\Includes\Elementor\Sifency_Addons_Widget_Registry' ) ) {
			$meta = \Sifency_Addons\Includes\Elementor\Sifency_Addons_Widget_Registry::get_widget( $name );
			return is_array( $meta ) ? $meta : array();
		}

		static $widgets_data = null;

		if ( null === $widgets_data ) {
			$file = SIFENCY_ADDONS_DIR . 'includes/elementor/sifency-widgets.php';

			if ( file_exists( $file ) ) {
				$widgets_data = include $file;
			} else {
				$widgets_data = array();
			}
		}

		if ( ! is_array( $widgets_data ) ) {
			return array();
		}

		foreach ( $widgets_data as $category_title => $widgets ) {
			if ( isset( $widgets[ $name ] ) ) {
				return $widgets[ $name ];
			}
		}

		return array();
	}

	/**
	 * Generate HTML content for the Help tab.
	 *
	 * @param string $widget_name
	 * @param array  $widget_meta
	 * @param array  $args
	 * @return string
	 */
	private function generate_sifency_help_html( $widget_name, $widget_meta, $args ) {
		$defaults = array(
			'base_url'           => 'https://sifency.com/sifency-addons/',
			'live_demo_base_url' => 'https://sifency.com/sifency-addons/',
			'doc_base_url'       => 'https://sifency.com/sifency-addons/',
			'video_base_url'     => 'https://sifency.com/sifency-addons/',
			'support_url'        => 'https://sifency.com/sifency-addons/help-center/',
		);

		$get_final_url = function ( $type ) use ( $widget_name, $widget_meta, $args, $defaults ) {
			if ( ! empty( $args[ $type . '_url' ] ) ) {
				return $args[ $type . '_url' ];
			}

			if ( ! empty( $widget_meta[ $type . '_url' ] ) ) {
				return $widget_meta[ $type . '_url' ];
			}

			$base_url = ! empty( $args[ $type . '_base_url' ] ) ? $args[ $type . '_base_url' ] : $defaults[ $type . '_base_url' ];
			$slug     = ! empty( $args[ $type . '_slug' ] ) ? $args[ $type . '_slug' ] : ( ! empty( $widget_meta[ $type . '_slug' ] ) ? $widget_meta[ $type . '_slug' ] : $widget_name );

			if ( 'live_demo' === $type && empty( $widget_meta[ $type . '_slug' ] ) && ! empty( $widget_meta['live_demo'] ) ) {
				return $widget_meta['live_demo'];
			}

			return rtrim( $base_url, '/' ) . '/' . ltrim( $slug, '/' );
		};

		$links = array(
			array(
				'label' => 'Widget Showcase',
				'url'   => $get_final_url( 'live_demo' ),
			),
			array(
				'label' => 'Live Demos',
				'url'   => $get_final_url( 'live_demo' ),
			),
			array(
				'label' => 'Documentation',
				'url'   => $get_final_url( 'live_demo' ),
			),
			array(
				'label' => 'Video Tutorial',
				'url'   => $get_final_url( 'live_demo' ),
			),
			array(
				'label' => 'Help Center',
				'url'   => $defaults['support_url'],
			),
		);

		$html = '<div class="sf-help-list">';
		foreach ( $links as $link ) {
			$html .= sprintf(
				'<a href="%s" target="_blank" class="sf-help-item">
					<span>%s</span>
					<i class="eicon-editor-external-link"></i>
				</a>',
				esc_url( $link['url'] ),
				esc_html( $link['label'] )
			);
		}
		$html .= '</div>';

		$is_sync = false;
		if ( class_exists( '\Sifency_Addons_Theme_Builder_Master' ) ) {
			$is_sync = \Sifency_Addons_Theme_Builder_Master::sync_environment_state();
		}

		if ( ! $is_sync ) {
			$button_link = 'https://sifency.com/sifency-addons/pricing/';

			$html .= sprintf(
				'<a href="%s" target="_blank" class="sf-upgrade-sync-btn">
					%s
				</a>',
				esc_url( $button_link ),
				esc_html__( 'UNLOCK FULL FUNCTIONALITY', 'sifency-addons' )
			);
		}

		return $html;
	}
}
