<?php
namespace dexen\Widget;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Controls_Manager;
use Elementor\Plugin;
use Elementor\Utils;
use Elementor\Group_Control_Image_Size;
use Elementor\Widget_Base;

class Brands extends Widget_Base {

	public function get_name() {
		return 'dexen_brands';
	}

	public function get_title() {
		return esc_html__( 'Brands', 'dexen-core' );
	}

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

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

	protected function _register_controls() {

		$this->start_controls_section(
			'content_settings',
			array(
				'label' => esc_html__( 'Content Settings', 'dexen-core' ),
			)
		);

		$this->add_control(
			'brand_tabs_tab',
			array(
				'type'        => Controls_Manager::REPEATER,
				'seperator'   => 'before',
				'default'     => array(
					array( 'tab_title' => esc_html__( 'Item', 'dexen-core' ) ),
					array( 'tab_title' => esc_html__( 'Item', 'dexen-core' ) ),
					array( 'tab_title' => esc_html__( 'Item', 'dexen-core' ) ),
					array( 'tab_title' => esc_html__( 'Item', 'dexen-core' ) ),
				),
				'fields'      => array(
					array(
						'name'    => 'tab_title',
						'label'   => esc_html__( 'Tab Title', 'dexen-core' ),
						'type'    => Controls_Manager::TEXT,
						'default' => esc_html__( 'Tab Title', 'dexen-core' ),
					),
					array(
						'name'    => 'image',
						'label'   => __( 'Image', 'dexen-core' ),
						'type'    => Controls_Manager::MEDIA,
						'default' => array(
							'url' => Utils::get_placeholder_image_src(),
						),
					),
					array(
						'name'    => 'size',
						'label'   => esc_html__( 'Select Size', 'dexen-core' ),
						'type'    => Controls_Manager::SELECT,
						'default' => 'full',
						'options' => array(
							'full'   => esc_html__( 'Full', 'dexen-core' ),
							'custom' => esc_html__( 'Custom', 'dexen-core' ),
						),
					),
					array(
						'name'      => 'width',
						'label'     => esc_html__( 'Width', 'dexen-core' ),
						'type'      => Controls_Manager::TEXT,
						'condition' => array(
							'size' => 'custom',
						),
					),
					array(
						'name'      => 'height',
						'label'     => esc_html__( 'Height', 'dexen-core' ),
						'type'      => Controls_Manager::TEXT,
						'condition' => array(
							'size' => 'custom',
						),
					),
					array(
						'name'          => 'action_link',
						'label'         => __( 'Action Button', 'dexen-core' ),
						'type'          => Controls_Manager::URL,
						'default'       => array(
							'url'         => '#',
							'is_external' => '',
						),
						'show_external' => true,
					),
					array(
						'name'  => 'extra_class',
						'label' => esc_html__( 'Extra Class', 'dexen-core' ),
						'type'  => Controls_Manager::TEXT,
					),
				),
				'title_field' => '{{tab_title}}',
			)
		);
		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings();
		?>
		   <section class="brand-one">
			<div class="container">
				<div class="brand-one__carousel owl-carousel owl-theme">
					<?php
					foreach ( $settings['brand_tabs_tab'] as $tab ) {
						$attachment_size = array();
						$has_custom_size = false;
						if ( ! empty( $tab['width'] ) && ! empty( $tab['height'] ) ) {
							$has_custom_size    = true;
							$attachment_size[0] = (int) $tab['width'];
							$attachment_size[1] = (int) $tab['height'];
						}

						if ( ! $has_custom_size ) {
							$attachment_size = 'full';
						}

						$url    = '#';
						$target = '';
						if ( ! empty( $tab['action_link'] ) ) {
							$link   = $tab['action_link'];
							$url    = $link['url'];
							$target = $link['is_external'] ? 'target="_blank"' : '';
						}
						if ( $url == 'http://' || $url == 'https://' ) {
							$url = '#';
						}
						if ( is_array( $attachment_size ) ) {
							$instance  = array(
								'image_size'             => 'custom',
								'image_custom_dimension' => array(
									'width'  => (int) $tab['width'],
									'height' => (int) $tab['height'],
								),
							);
							$image_url = Group_Control_Image_Size::get_attachment_image_src( $tab['image']['id'], 'image', $instance );
						} else {
							$image_url = ( $tab['image']['id'] != '' ) ? wp_get_attachment_url( $tab['image']['id'], $attachment_size ) : $tab['image']['url'];
						}
						?>
						<div class="item">
						 <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_html__( 'Image', 'dexen-core' ); ?>"/>
						</div><!-- /.item -->
						 <?php } ?>
				</div><!-- /.brand-one__carousel -->
			</div><!-- /.container -->
		</section><!-- /.brand-one -->
		<?php
	}

	protected function content_template() {

	}

}

Plugin::instance()->widgets_manager->register_widget_type( new Brands() );
