<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Sitelogo extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sitelogo',
			'title'          => 'Sifency Site Logo',
			'icon'           => 'eicon-site-logo',
			'category'       => array( 'sifency-addons-widgets' ),
			'style_depends'  => array( 'sifency-frontend-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'content_section',
			array( 'label' => esc_html__( 'Logo', 'sifency-addons' ) )
		);
		$this->add_responsive_control(
			'align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-sitelogo-wrapper' => 'text-align: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'link_to',
			array(
				'label'   => esc_html__( 'Link', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'home',
				'options' => array(
					'home'   => esc_html__( 'Home Page', 'sifency-addons' ),
					'custom' => esc_html__( 'Custom URL', 'sifency-addons' ),
					'none'   => esc_html__( 'None', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'custom_link',
			array(
				'label'      => esc_html__( 'Custom Link', 'sifency-addons' ),
				'type'       => Controls_Manager::URL,
				'dynamic'    => array( 'active' => true ),
				'condition'  => array( 'link_to' => 'custom' ),
				'show_label' => false,
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_image_style_controls(
			array(
				'label'     => esc_html__( 'Logo Style', 'sifency-addons' ),
				'selector'  => '.sifency-sitelogo img',
				'selectors' => array(
					'image'      => '.sifency-sitelogo img',
					'container'  => '.sifency-sitelogo img',
					'dimensions' => '.sifency-sitelogo img',
				),
			)
		);

		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	protected function render() {
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$logo_html         = get_custom_logo();
		if ( empty( $logo_html ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				?>
				<div class="sifency-sitelogo-placeholder">
					<?php esc_html_e( 'No Site Logo is set. Please set it in Appearance > Customize > Site Identity.', 'sifency-addons' ); ?>
				</div>
				<?php
			}
			return;
		}

		$this->add_render_attribute( 'link', 'class', 'sifency-sitelogo-link' );
		if ( 'none' !== $settings['link_to'] ) {
			if ( 'custom' === $settings['link_to'] && ! empty( $settings['custom_link']['url'] ) ) {
				$this->add_link_attributes( 'link', $settings['custom_link'] );
			} else {
				$this->add_render_attribute( 'link', 'href', esc_url( home_url( '/' ) ) );
			}
			$logo_html = preg_replace( '/<a\s(.+?)>/i', '<a ' . $this->get_render_attribute_string( 'link' ) . '>', $logo_html );
		} else {
			$logo_html = preg_replace( array( '/<a\s(.+?)>/i', '/<\/a>/i' ), '', $logo_html );
		}
		$inner_wrapper_classes = array( 'sifency-sitelogo' );
		$inner_wrapper_attrs   = $animation_manager->get_wrapper_attributes( $inner_wrapper_classes );
		?>
		<div class="sifency-sitelogo-wrapper">
			<div <?php echo wp_kses_post( $inner_wrapper_attrs ); ?>>
				<?php echo wp_kses_post( $logo_html ); ?>
			</div>
		</div>
		<?php
	}
}
