<?php
namespace Elementor;

class ThemeDraft_Contact_Info_Widget extends Widget_Base {

	public function get_name() {

		return 'themedraft_contact_info';
	}

	public function get_title() {
		return esc_html__( 'Contact Info', 'themedraft-core' );
	}

	public function get_icon() {

		return 'eicon-alert';
	}

	public function get_categories() {
		return [ 'themedraft_elements' ];
	}


	protected function register_controls() {

		//Content tab start
		$this->start_controls_section(
			'contact_info_settings',
			[
				'label' => esc_html__( 'Contact Info', 'themedraft-core' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
		    'title',
		    [
		        'label'       => __('Title', 'themedraft-core'),
		        'type'        => Controls_Manager::TEXT,
		        'default'     => 'Call Us',
		        'label_block' => true,
		    ]
		);


		$repeater->add_control(
		    'desc',
		    [
		        'label'   => __('Description', 'themedraft-core'),
		        'type'    => Controls_Manager::WYSIWYG,
		        'default' => 'Description Text Here',
		    ]
		);


		$repeater->add_control(
		    'type',
		    [
		        'label'       => __( 'Icon Type', 'themedraft-core' ),
		        'type'        => Controls_Manager::CHOOSE,
		        'label_block' => false,
		        'options'     => [
		            'icon'  => [
		                'title' => __( 'Icon', 'themedraft-core' ),
		                'icon'  => 'fa fa-smile-o',
		            ],
		            'image' => [
		                'title' => __( 'Image', 'themedraft-core' ),
		                'icon'  => 'fa fa-image',
		            ],
		        ],
		        'default'     => 'icon',
		        'toggle'      => false,
		    ]
		);

		$repeater->add_control(
		    'selected_icon',
		    [
		        'label'       => __( 'Select Icon', 'themedraft-core' ),
		        'type'             => Controls_Manager::ICONS,
		        'fa4compatibility' => 'icon',
		        'label_block'      => true,
		        'default'          => [
		            'value'   => 'flaticon-business-support',
		            'library' => 'themedraft-flaticon',
		        ],
		        'condition'        => [
		            'type' => 'icon'
		        ]
		    ]
		);

		$repeater->add_control(
		    'image',
		    [
		        'label'     => __( 'Image', 'themedraft-core' ),
		        'type'      => Controls_Manager::MEDIA,
		        'default'   => [
		            'url' => Utils::get_placeholder_image_src(),
		        ],
		        'condition' => [
		            'type' => 'image'
		        ],
		        'dynamic'   => [
		            'active' => true,
		        ]
		    ]
		);


		$this->add_control(
		    'info_list',
		    [
		        'label'       => __('Info List', 'themedraft-core'),
		        'type'        => Controls_Manager::REPEATER,
		        'fields'      => $repeater->get_controls(),
		        'default'     => [
		            [
		                'title'        => 'Call Us',
		                'desc' => 'Description Text Here',
		            ],
		        ],
		        'title_field' => '{{{ title }}}',
		    ]
		);

		$this->add_responsive_control(
		    'item_width',
		    [
		        'label' => __( 'Item Column Width (%)', 'themedraft-core' ),
		        'type' => Controls_Manager::SLIDER,
		        'size_units' => ['%'],
		        'range' => [
		            '%' => [
		                'min' => 0,
		                'max' => 100,
		            ],
		        ],
		        'devices' => [ 'desktop', 'tablet', 'mobile' ],

		        'selectors' => [
		            '{{WRAPPER}} .col-lg-4.col-md-6.info-item-column' => 'flex: 0 0 {{SIZE}}%;max-width: {{SIZE}}%',
		        ],
		    ]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'other_colors',
			[
				'label' => esc_html__( 'Colors', 'themedraft-core' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_control(
			'icon_color',
			[
				'label'       => esc_html__('Icon Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-contact-info-icon i' => 'color: {{VALUE}};',
					'{{WRAPPER}} .td-contact-info-icon svg' => 'fill: {{VALUE}};',
				],
			]
		);

		$this->add_control(
		    'title_color',
		    [
		        'label'       => esc_html__('Title Color', 'themedraft-core'),
		        'type'        => Controls_Manager::COLOR,
		        'selectors'   => [
		            '{{WRAPPER}} .info-title' => 'color: {{VALUE}};',
		        ],
		    ]
		);

		$this->add_control(
			'text_color',
			[
				'label'       => esc_html__('Text Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .info-content,{{WRAPPER}} .info-content a,{{WRAPPER}} .info-content a:hover' => 'color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
		    'wrapper_settings',
		    [
		        'label' => esc_html__( 'Wrapper', 'themedraft-core' ),
		        'tab'   => Controls_Manager::TAB_STYLE,
		    ]
		);

		$this->add_group_control(
		    Group_Control_Background::get_type(),
		    [
		        'name' => 'wrapper_background',
		        'label' => esc_html__( 'Background', 'themedraft-core' ),
		        'types' => [ 'classic', 'gradient'],
		        'selector' => '{{WRAPPER}} .td-contact-info-container',
		    ]
		);

		$this->add_responsive_control(
		    'wrapper_padding',
		    [
		        'label'      => esc_html__( 'Wrapper Padding', 'themedraft-core' ),
		        'type'       => Controls_Manager::DIMENSIONS,
		        'size_units' => [ 'px', '%', 'em' ],
		        'selectors'  => [
		            '{{WRAPPER}} .td-contact-info-container' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
		        ],
		    ]
		);


		$this->add_control(
			'wrapper_animation',
			[
				'label' => __( 'Entrance Animation', 'themedraft-core' ),
				'type' => Controls_Manager::ANIMATION,
				'prefix_class' => 'animated ',
                'separator' => 'before',
			]
		);

		$this->add_control(
			'wrapper_animation_duration',
			[
				'label'     => __( 'Animation Duration', 'themedraft-core' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'data-wow-duration="1.25s"',
				'options'   => [
					'data-wow-duration="2s"'    => __( 'Slow', 'themedraft-core' ),
					'data-wow-duration="1.25s"' => __( 'Normal', 'themedraft-core' ),
					'data-wow-duration=".75s"'  => __( 'Fast', 'themedraft-core' ),
				],
				'condition' => [
					'wrapper_animation!' => '',
				]
			]
		);
		$this->end_controls_section();


	}

	//Render
	protected function render() {
		$settings = $this->get_settings_for_display();
		if($settings['wrapper_animation']){
			$td_animation = 'wow'.' ' . $settings['wrapper_animation'];
			$td_animation_duration = $settings['wrapper_animation_duration'];
		}else{
			$td_animation ='';
			$td_animation_duration ='';
		}
		?>
		<div class="td-contact-info-container <?php echo $td_animation;?>" <?php echo $td_animation_duration;?>>
			<div class="row">
				<?php if ( $settings['info_list'] ) {
				    $i = 1;
					foreach ( $settings['info_list'] as $info ) { 
					if(($i%3)==0)
					{
					?>
						<div class="col-lg-4 col-md-6 info-item-column">
				    <?php
					}else
					{
					    if(($i%3)==1)
					    {
					    ?>
					    <div class="col-lg-3 col-md-6 info-item-column">
					    <?php
					    }else
					    {
					    ?>
					    <div class="col-lg-5 col-md-6 info-item-column">
					    <?php
					    }
					}
					?>
							<div class="td-single-info">
								<div class="td-contact-info-icon">
									<?php if ( $info['type'] === 'image' ) :
									    if ( $info['image']['url'] || $info['image']['id'] ) :
									        ?>
									        <div class="td-image-icon">
									            <img src="<?php echo $info['image']['url']; ?>"
									                 alt="<?php echo get_post_meta( $info['image']['id'], '_wp_attachment_image_alt', true ); ?>">
									        </div>
									    <?php endif;
									elseif ( ! empty( $info['icon'] ) || ! empty( $info['selected_icon'] ) ) : ?>
									    <div class="td-font-icon">
									        <?php themedraft_custom_icon_render( $info, 'icon', 'selected_icon' ); ?>
									    </div>
									<?php endif; ?>
								</div>
								<h6 class="info-title"><?php echo $info['title'];?></h6>
								<div class="info-content">
									<?php echo $info['desc'];?>
								</div>
							</div>
						</div>

						<?php
						$i++;
					}
				} ?>
			</div>
		</div>
		<?php

	}
}

Plugin::instance()->widgets_manager->register( new ThemeDraft_Contact_Info_Widget );