<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Text_Shadow;

use EcafeAddons\EcafeElementLoad;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class EcafeCopyright extends Widget_Base {

	public function get_name() {
		return 'ecafe-copyright';
	}

	public function get_title() {
		return esc_html__( 'Copyright', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-copyright-icon';
	}

	public function get_categories() {
		return array( 'essential' );
	}

	public function get_keywords() {
		return array( 'year', 'copyright', 'copy right', 'footer' );
	}

	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}
	protected function register_controls() {

		$this->start_controls_section(
			'contentSection',
			array(
				'label' => esc_html__( 'Copyright', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$repeater = new \Elementor\Repeater();
		$repeater->add_control(
			'crType',
			array(
				'label'   => esc_html__( 'Type', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'text',
				'options' => array(
					'year' => esc_html__( 'Year', 'essential-classy-addons-for-elementor' ),
					'text' => esc_html__( 'Text', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$repeater->add_control(
			'crIcon',
			array(
				'label'     => esc_html__( 'Icon', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'far fa-copyright',
					'library' => 'solid',
				),
				'condition' => array(
					'crType' => 'year',
				),
			)
		);
		$repeater->add_control(
			'crText',
			array(
				'label'       => esc_html__( 'Text', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => esc_html__( 'All rights reserved.', 'essential-classy-addons-for-elementor' ),
				'dynamic'     => array( 'active' => true ),
				'label_block' => true,
				'condition'   => array(
					'crType' => 'text',
				),
			)
		);
		$repeater->add_responsive_control(
			'crLoopMargin',
			array(
				'label'      => esc_html__( 'Loop Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}}' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$repeater->add_responsive_control(
			'crLoopIMargin',
			array(
				'label'      => esc_html__( 'Loop Icon Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}} i,
					{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}} svg' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'condition'  => array(
					'crType' => 'year',
				),
			)
		);
		$repeater->add_control(
			'crColor',
			array(
				'label'     => esc_html__( 'Normal Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}}' => 'color: {{VALUE}};',
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}} svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$repeater->add_control(
			'crColorH',
			array(
				'label'     => esc_html__( 'Hover Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}}:hover' => 'color: {{VALUE}};',
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}}:hover svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$repeater->add_control(
			'crLColor',
			array(
				'label'     => esc_html__( 'Link Normal Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}} a' => 'color: {{VALUE}};',
				),
				'condition' => array(
					'crType' => 'text',
				),
			)
		);
		$repeater->add_control(
			'crLColorH',
			array(
				'label'     => esc_html__( 'Link Hover Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop{{CURRENT_ITEM}} a:hover' => 'color: {{VALUE}};',
				),
				'condition' => array(
					'crType' => 'text',
				),
			)
		);
		$this->add_control(
			'crLists',
			array(
				'label'       => '',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'crType' => 'year',
					),
					array(
						'crType' => 'text',
						'crText' => esc_html__( 'All rights reserved.', 'essential-classy-addons-for-elementor' ),
					),
				),
				'title_field' => '{{{ crType }}}',
			)
		);
		$this->add_control(
			'crlistsoneline',
			array(
				'label'     => esc_html__( 'Text in One Line', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::SWITCHER,
				'label_on'  => __( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off' => __( 'Disable', 'essential-classy-addons-for-elementor' ),
				'separator' => 'before',
				'default'   => 'no',
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdemolink = esc_url( 'https://ecaddons.com/elements/copyright-year-dynamically-in-elementor-widget/' );
		$ecwidgetdoclink  = esc_url( 'https://ecaddons.com/docs/copyright-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		$this->start_controls_section(
			'crStyling',
			array(
				'label' => esc_html__( 'Copyright', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'crAlign',
			array(
				'label'     => esc_html__( 'Alignment', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .ec-copyright-wrapper' => 'justify-content:{{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'crMargin',
			array(
				'label'      => esc_html__( 'Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-copyright-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'crLMargin',
			array(
				'label'      => esc_html__( 'Loop Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-copyright-wrapper .ec-cr-loop' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'crTypography',
				'label'    => esc_html__( 'General Typography', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-copyright-wrapper',
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'crLinkTypography',
				'label'    => esc_html__( 'Link Typography', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-copyright-wrapper a',
			)
		);
		$this->add_responsive_control(
			'crSize',
			array(
				'label'      => esc_html__( 'Size', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 500,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 20,
				),
				'selectors'  => array(
					'{{WRAPPER}} .ec-copyright-wrapper i' => 'font-size:{{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .ec-copyright-wrapper svg' => 'width:{{SIZE}}{{UNIT}};height:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_control(
			'crColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-copyright-wrapper,{{WRAPPER}} .ec-copyright-wrapper a' => 'color: {{VALUE}};',
					'{{WRAPPER}} .ec-copyright-wrapper svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
	}


	protected function render() {
		$settings = $this->get_settings_for_display();
		$output   = '';
		if ( $settings['crLists'] ) {
			$i       = 0;
			$oneline = $settings['crlistsoneline'] === 'yes' ? 'ec-cp-one-line' : '';
			$output .= '<div class="ec-copyright-wrapper ' . esc_attr( $oneline ) . '">';
			foreach ( $settings['crLists'] as $item ) {
				$output .= '<div class="ec-cr-loop elementor-repeater-item-' . esc_attr( $item['_id'] ) . '">';
				if ( $item['crType'] == 'year' ) {
					if ( $item['crIcon'] ) {
						ob_start();
						\Elementor\Icons_Manager::render_icon( $item['crIcon'], array( 'aria-hidden' => 'true' ) );
						$output .= ob_get_contents();
						ob_end_clean();
					}
					$output .= date( 'Y' );
				}
				if ( $item['crType'] == 'text' && ! empty( $item['crText'] ) ) {
					$output .= wp_kses_post( $item['crText'] );
				}
					$output .= '</div>';
					++$i;
			}
			$output .= '</div>';
		}
		echo wp_kses_normalize_entities( $output );
	}
}
