<?php
namespace CSSIgniter\Ignition\Elementor\Widget;

class Pricing_List extends \Elementor\Widget_Base {
	public function get_style_depends() {
		wp_register_style( 'ignition-elementor-pricing-list', plugin_dir_url( __DIR__ ) . 'assets/css/pricing-list.min.css', array(), IGNITION_ELEMENTOR_WIDGETS_VERSION );

		return [
			'ignition-elementor-pricing-list',
		];

	}

	public function get_name() {
		return 'ignition-pricing-list';
	}

	public function get_title() {
		return __( 'Pricing List', 'ignition-elementor-widgets' );
	}

	public function get_icon() {
		return 'eicon-price-list';
	}

	public function get_categories() {
		return [ 'ignition-elementor-widgets' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'content_section',
			[
				'label' => __( 'Element Content', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'item_title',
			[
				'label'       => __( 'Title', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => __( 'Item Title', 'ignition-elementor-widgets' ),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'item_price',
			[
				'label'       => __( 'Price', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => __( 'Item Price', 'ignition-elementor-widgets' ),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'item_content',
			[
				'label'       => __( 'Content', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXTAREA,
				'default'     => '',
				'placeholder' => __( 'List item description', 'ignition-elementor-widgets' ),
				'show_label'  => false,
			]
		);

		$repeater->add_control(
			'item_image',
			[
				'label'   => __( 'Choose Image', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::MEDIA,
				'default' => [
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				],
			]
		);

		$repeater->add_group_control(
			\Elementor\Group_Control_Image_Size::get_type(),
			[
				'name'    => 'image',
				'exclude' => [ 'custom' ],
				'include' => [],
				'default' => 'thumbnail',
			]
		);

		$this->add_control(
			'pricing_list',
			[
				'label'       => __( 'Pricing List', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{{ item_title }}}',
			]
		);

		$this->add_control(
			'image_appearance',
			[
				'label'   => __( 'Image Appearance', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'left',
				'options' => [
					'left'  => __( 'Left', 'ignition-elementor-widgets' ),
					'right' => __( 'Right', 'ignition-elementor-widgets' ),
					'none'  => __( 'None', 'ignition-elementor-widgets' ),
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_image_style',
			[
				'label' => __( 'Image Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'border_radius',
			[
				'label'      => __( 'Border Radius', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'range'      => [
					'px' => [
						'min'  => 0,
						'max'  => 500,
						'step' => 1,
					],
					'%'  => [
						'min' => 0,
						'max' => 100,
					],
				],
				'default'    => [
					'unit' => '%',
					'size' => 0,
				],
				'selectors'  => [
					'{{WRAPPER}} .ignition-elementor-pricing-list-item-thumb img' => 'border-radius: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_title_style',
			[
				'label' => __( 'Title Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'title_color',
			[
				'label'     => __( 'Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-pricing-list-item-title' => 'color: {{VALUE}};',
				],
				'scheme'    => [
					'type'  => \Elementor\Core\Schemes\Color::get_type(),
					'value' => \Elementor\Core\Schemes\Color::COLOR_3,
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'     => 'title_typography',
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_3,
				'selector' => '{{WRAPPER}} .ignition-elementor-pricing-list-item-title',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			[
				'name'      => 'title_border',
				'label'     => __( 'Border', 'ignition-elementor-widgets' ),
				'selector'  => '{{WRAPPER}} .ignition-elementor-pricing-list-item-title',
				'separator' => 'before',
			]
		);

		$this->add_responsive_control(
			'title_padding',
			[
				'label'      => __( 'Padding', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .ignition-elementor-pricing-list-item-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'title_margin',
			[
				'label'      => __( 'Margin', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .ignition-elementor-pricing-list-item-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_price_style',
			[
				'label' => __( 'Price Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'price_color',
			[
				'label'     => __( 'Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-pricing-list-item-title .ignition-elementor-pricing-list-item-price' => 'color: {{VALUE}};',
				],
				'scheme'    => [
					'type'  => \Elementor\Core\Schemes\Color::get_type(),
					'value' => \Elementor\Core\Schemes\Color::COLOR_3,
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'     => 'price_typography',
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_3,
				'selector' => '{{WRAPPER}} .ignition-elementor-pricing-list-item-title .ignition-elementor-pricing-list-item-price',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_description_style',
			[
				'label' => __( 'Description Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'description_color',
			[
				'label'     => __( 'Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-pricing-list-item-description' => 'color: {{VALUE}};',
				],
				'scheme'    => [
					'type'  => \Elementor\Core\Schemes\Color::get_type(),
					'value' => \Elementor\Core\Schemes\Color::COLOR_3,
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'     => 'description_typography',
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_3,
				'selector' => '{{WRAPPER}} .ignition-elementor-pricing-list-item-description',
			]
		);

		$this->end_controls_section();

	}

	protected function render() {
		$settings     = $this->get_settings();
		$pricing_list = $settings['pricing_list'];

		if ( ! $pricing_list ) {
			return;
		}

		?>
		<div class="ignition-elementor-pricing-list">
		<?php
		foreach ( $pricing_list as $list_item ) {
			?>
			<div class="ignition-elementor-pricing-list-item ignition-elementor-image-<?php echo esc_attr( $settings['image_appearance'] ); ?>">
				<?php if ( 'none' !== $settings['image_appearance'] ) : ?>
					<div class="ignition-elementor-pricing-list-item-thumb ignition-elementor-image-<?php echo esc_attr( $settings['image_appearance'] ); ?>">
						<?php echo wp_get_attachment_image( $list_item['item_image']['id'], $list_item['image_size'] ); ?>
					</div>
				<?php endif; ?>
				<div class="ignition-elementor-pricing-list-item-content">
					<p class="ignition-elementor-pricing-list-item-title">
						<?php echo wp_kses_post( $list_item['item_title'] ); ?>
						<?php if ( $list_item['item_price'] ) : ?>
							<span class="ignition-elementor-pricing-list-item-price">
								<?php echo wp_kses_post( $list_item['item_price'] ); ?>
							</span>
						<?php endif; ?>
					</p>
					<?php if ( $list_item['item_content'] ) : ?>
						<p class="ignition-elementor-pricing-list-item-description">
							<?php echo wp_kses_post( $list_item['item_content'] ); ?>
						</p>
					<?php endif; ?>
				</div>
			</div>
			<?php
		}
		?>
		</div>
		<?php

	}
}
