<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Combine_Button_Controls;

class Sifency_Addons_Widget_Message_Box extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_Combine_Button_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-message-box',
			'title'          => 'Sifency Message Box',
			'icon'           => 'eicon-alert',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'sifency-message-box-script' ),
			'style_depends'  => array( 'sifency-message-box-style', 'sifency-icon' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Message Content', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'message_type',
			array(
				'label'   => esc_html__( 'Message Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'info',
				'options' => array(
					'info'    => esc_html__( 'Info (Blue)', 'sifency-addons' ),
					'success' => esc_html__( 'Success (Green)', 'sifency-addons' ),
					'warning' => esc_html__( 'Warning (Yellow)', 'sifency-addons' ),
					'error'   => esc_html__( 'Error (Red)', 'sifency-addons' ),
					'custom'  => esc_html__( 'Custom', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'view_style',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'solid',
				'options' => array(
					'solid'       => esc_html__( 'Solid Background', 'sifency-addons' ),
					'left-border' => esc_html__( 'Left Border Accent', 'sifency-addons' ),
					'outline'     => esc_html__( 'Outline / Minimal', 'sifency-addons' ),
				),
			)
		);
		$this->add_control( 'hr_c1', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'selected_icon',
			array(
				'label'   => esc_html__( 'Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-warning',
					'library' => 'sifency-icons',
				),
			)
		);
		$this->add_control(
			'title',
			array(
				'label'   => esc_html__( 'Title', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Notice', 'sifency-addons' ),
				'dynamic' => array( 'active' => true ),
			)
		);
		$this->add_control(
			'content_source',
			array(
				'label'   => esc_html__( 'Content Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'editor',
				'options' => array(
					'editor'   => esc_html__( 'Text Editor', 'sifency-addons' ),
					'template' => esc_html__( 'Elementor Template', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'content',
			array(
				'label'     => esc_html__( 'Description', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => esc_html__( 'This is an important notification message.', 'sifency-addons' ),
				'condition' => array( 'content_source' => 'editor' ),
			)
		);
		$this->add_control(
			'template_id',
			array(
				'label'     => esc_html__( 'Choose Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '0',
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array( 'content_source' => 'template' ),
			)
		);
		$this->add_control( 'hr_c2', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'is_dismissible',
			array(
				'label'     => esc_html__( 'Dismissible?', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'label_on'  => 'Yes',
				'label_off' => 'No',
			)
		);
		$this->add_control(
			'close_icon',
			array(
				'label'     => esc_html__( 'Close Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'eicon-close',
					'library' => 'eicons',
				),
				'condition' => array( 'is_dismissible' => 'yes' ),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_combine_button_content_controls(
			array(
				'prefix' => 'msg_btn_',
				'label'  => esc_html__( 'Button', 'sifency-addons' ),
			)
		);

		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'box_style',
				'label'    => 'Box Container',
				'selector' => '.sifency-msg-box',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'heading_style',
				'label'     => 'Title',
				'selector'  => '.sifency-heading-style',
				'condition' => array( 'title!' => '' ),
				'exclude'   => array( 'tabs', 'transition', 'bg', 'radius', 'shadow', 'stroke' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'content_style',
				'label'           => 'Description',
				'selector'        => '.sifency-content-style',
				'target_children' => true,
				'show_max_width'  => true,
				'exclude'         => array( 'tabs', 'transition', 'bg', 'radius', 'shadow', 'stroke' ),
				'condition'       => array(
					'content_source' => 'editor',
					'content!'       => '',
				),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'msg_icon_',
				'selector'        => '.sifency-msg-icon-wrap',
				'hover_selector'  => 'sifency-msg-box:hover .sifency-msg-icon-wrap',
				'condition'       => array(
					'selected_icon[library]!' => '',
				),
				'show_dimensions' => true,
				'exclude'         => array( 'transition', 'tabs' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'close_icon_',
				'selector'        => '.sifency-msg-close',
				'condition'       => array( 'is_dismissible' => 'yes' ),
				'show_dimensions' => true,
				'label'           => esc_html__( 'Close Icon', 'sifency-addons' ),
			)
		);

		$this->add_Sifency_Addons_Combine_Button_Controls(
			array(
				'prefix'   => 'msg_btn_',
				'label'    => esc_html__( 'Button', 'sifency-addons' ),
				'selector' => '.sifency-msg-btn',
			)
		);

		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id              = esc_attr( $context['dom_id'] );
		$settings                = $this->get_settings_for_display();
		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-message-box-wrapper' );

		$this->add_render_attribute( 'box', 'class', array( 'sifency-msg-box' ) );
		$this->add_render_attribute( 'box', 'class', 'sifency-msg-type-' . $settings['message_type'] );
		$this->add_render_attribute( 'box', 'class', 'sifency-msg-view-' . $settings['view_style'] );

		if ( 'yes' === $settings['is_dismissible'] ) {
			 $this->add_render_attribute( 'box', 'class', 'sifency-dismissible' );
		}

		$animation_attrs = $animation_manager->get_wrapper_attributes();
		if ( ! empty( $animation_attrs ) ) {
			preg_match( '/class="([^"]+)"/', $animation_attrs, $class_matches );
			if ( isset( $class_matches[1] ) ) {
				$this->add_render_attribute( 'wrapper', 'class', $class_matches[1] );
			}
		}

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_wrapper_attrs ); ?>>
			
			<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'box' ) ); ?>>
				
				<?php if ( ! empty( $settings['selected_icon']['value'] ) ) : ?>
				<div class="sifency-msg-icon-wrap sifency-icon-style">
					<?php Icons_Manager::render_icon( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				</div>
				<?php endif; ?>

				<div class="sifency-msg-content">
					<?php if ( ! empty( $settings['title'] ) ) : ?>
						<h4 class="sifency-msg-title sifency-heading-style"><?php echo esc_html( $settings['title'] ); ?></h4>
					<?php endif; ?>
					
					<div class="sifency-msg-desc sifency-content-style">
						<?php
						if ( 'template' === $settings['content_source'] && ! empty( $settings['template_id'] ) ) {
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
							echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $settings['template_id'] );
						} elseif ( ! empty( $settings['content'] ) ) {
							echo wp_kses_post( $settings['content'] );
						}
						?>
					</div>
					<?php $this->add_sifency_addons_combine_button_render( $settings, 'msg_btn_', 'sifency-msg-btn' ); ?>
				</div>

				<?php if ( 'yes' === $settings['is_dismissible'] ) : ?>
					<button class="sifency-msg-close" aria-label="Close">
						<?php Icons_Manager::render_icon( $settings['close_icon'], array( 'aria-hidden' => 'true' ) ); ?>
					</button>
				<?php endif; ?>

			</div>

		</div>
		<?php
	}
}
