<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Copy_Button extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-copy-button',
			'title'          => 'Sifency Copy Info Button',
			'icon'           => 'eicon-copy',
			'category'       => array( 'sifency-business-widgets' ),
			'script_depends' => array( 'sifency-copy-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'copy', 'clipboard', 'button', 'email', 'phone' ),
		);
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Button Content', 'sifency-addons' ) )
		);

		$this->add_control(
			'text_to_copy',
			array(
				'label'       => esc_html__( 'Text to Copy', 'sifency-addons' ),
				'type'        => Controls_Manager::WYSIWYG,
				'default'     => esc_html__( 'hello@youragency.com', 'sifency-addons' ),
				'description' => 'This exact text will be copied to the clipboard.',
			)
		);

		$this->add_control(
			'button_label',
			array(
				'label'   => esc_html__( 'Button Label', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'E-Mail', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'success_label',
			array(
				'label'       => esc_html__( 'Success Label', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Copied!', 'sifency-addons' ),
				'description' => 'Text shown briefly after clicking.',
			)
		);

		$this->add_control(
			'icon',
			array(
				'label'   => esc_html__( 'Default Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'fas fa-copy',
					'library' => 'fa-solid',
				),
			)
		);

		$this->add_control(
			'success_icon',
			array(
				'label'   => esc_html__( 'Success Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'fas fa-check',
					'library' => 'fa-solid',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_icon_style_controls(
			array(
				'label'    => esc_html__( 'Icon Override', 'sifency-addons' ),
				'selector' => '.sifency-copy-btn .sifency-copy-icon',
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector' => '.sifency-copy-btn',
			)
		);
	}

	protected function render_dom( array $context ) {
		$settings     = $this->get_settings_for_display();
		$wrapper_id   = esc_attr( $context['dom_id'] );
		$text_to_copy = $settings['text_to_copy'];
		if ( empty( $text_to_copy ) ) {
			return;
		}
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-copy-wrapper">
			
			<button class="sifency-copy-btn" data-clipboard="<?php echo esc_attr( $text_to_copy ); ?>">
				
				<div class="sifency-copy-state sifency-state-default">
					<?php if ( ! empty( $settings['icon']['value'] ) ) : ?>
						<span class="sifency-copy-icon sifency-default-icon">
							<?php Icons_Manager::render_icon( $settings['icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</span>
					<?php endif; ?>
					<span class="sifency-btn-text"><?php echo esc_html( $settings['button_label'] ); ?></span>
				</div>

				<div class="sifency-copy-state sifency-state-success">
					<?php if ( ! empty( $settings['success_icon']['value'] ) ) : ?>
						<span class="sifency-copy-icon sifency-success-icon">
							<?php Icons_Manager::render_icon( $settings['success_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</span>
					<?php endif; ?>
					<span class="sifency-btn-text"><?php echo esc_html( $settings['success_label'] ); ?></span>
				</div>

			</button>
			
		</div>
		<?php
	}
}
