<?php
/**
 * Widget: Video Block
 *
 * Widget Show video block from widget
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */
if(!class_exists( 'Trainify_Video_Block_Widget' ) ):

	class Trainify_Video_Block_Widget extends TrainifyFramework_Master_Widget{

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {
			$widget_ops = array(
				'classname'   => 'trainify_video_block_widget tff-full-widget',
				'description' => esc_html__( 'Add video block from widget on frontend widgetarea.', 'trainify' )
			);
			parent::__construct( 'trainify_video_block_widget', esc_html__( 'TF - Video Block', 'trainify' ), $widget_ops );
		}

		/**
		 * Helper function that holds widget fields
		 * Array is used in update and form functions
		 */
		public function widget_fields( $instance = array() ) {

			$fields = array(
				'trainify_framework_widget_tab'       => array(
					'trainify_framework_widget_field_name'     => 'trainify_framework_widget_tab',
					'trainify_framework_widget_field_title'    => esc_html__( 'General', 'trainify' ),
					'trainify_framework_widget_field_default'  => 'general',
					'trainify_framework_widget_field_type'     => 'tabgroup',
					'trainify_framework_widget_field_options'  => array(
						'general'=>array(
							'trainify_framework_widget_field_title'=>esc_html__('General', 'trainify'),
							'trainify_framework_widget_field_options'=> array(
								'title' => array(
									'trainify_framework_widget_field_name'        => 'title',
									'trainify_framework_widget_field_title'       => esc_html__( 'Section Title', 'trainify' ),
									'trainify_framework_widget_field_default'	  => esc_html__( 'Learn from online', 'trainify' ),
									'trainify_framework_widget_field_description' => esc_html__( 'Enter your block title', 'trainify' ),
									'trainify_framework_widget_field_type'  => 'text'
								),
								'sub_title' => array(
									'trainify_framework_widget_field_name'        => 'sub_title',
									'trainify_framework_widget_field_title'       => esc_html__( 'Sub Title', 'trainify' ),
									'trainify_framework_widget_field_default'	  => esc_html__( 'We will provide you video tutorials.', 'trainify' ),
									'trainify_framework_widget_field_description' => esc_html__( 'Enter your sub title', 'trainify' ),
									'trainify_framework_widget_field_type'  => 'text'
								),
								'video_link' => array(
									'trainify_framework_widget_field_name'        => 'video_link',
									'trainify_framework_widget_field_title'       => esc_html__( 'Video Link', 'trainify' ),
									'trainify_framework_widget_field_default'	  => '',
									'trainify_framework_widget_field_description' => esc_html__( 'Please enter the video link here.', 'trainify' ),
									'trainify_framework_widget_field_type'  => 'url'
								),
								'custom_screenshot' => array(
									'trainify_framework_widget_field_name'        => 'custom_screenshot',
									'trainify_framework_widget_field_title'       => esc_html__( 'Custom Screenshot', 'trainify' ),
									'trainify_framework_widget_field_description' => esc_html__( 'Check to put custom video screenshot image.', 'trainify' ),
									'trainify_framework_widget_field_type'  => 'checkbox',
									'trainify_framework_widget_field_default'     => 0,
								),
								'background_image'         => array(
									'trainify_framework_widget_field_name'     => 'background_image',
									'trainify_framework_widget_field_title'    => esc_html__( 'Video Screenshot', 'trainify' ),
									'trainify_framework_widget_field_type'     => 'upload',
									'trainify_framework_widget_description'    => esc_html__('Please add a background image for this section.', 'trainify'),
								),
							),
						),
					)
				)
			);

			$widget_fields_key = 'fields_'.$this->id_base;
			$widgets_fields = apply_filters( $widget_fields_key, $fields );
			return $widgets_fields;

		}

		/**
		 * Front-end display of widget.
		 *
		 * @see WP_Widget::widget()
		 *
		 * @param array $args Widget arguments.
		 * @param array $instance Saved values from database.
		 */
		public function widget( $args, $instance ) {

			extract( $args );
			if ( empty( $instance ) ) {
				return;
			}

			$title  = isset( $instance['title'] ) ? esc_html( $instance['title'] ) : '';
			$title 	= apply_filters( 'widget_title', $title, $instance, $this->id_base );
			$sub_title = isset($instance['sub_title']) ? $instance['sub_title'] : '';
			$video_link = isset($instance['video_link']) ? $instance['video_link'] : '';
			$background_image = isset($instance['background_image']) ? $instance['background_image'] : '';
	        trainify_framework_before_widget($args);

	        $title_tag_name = 'h4';
			$main_widget_areas = array(
				'trainify_home_content_area',
				'trainify_home_middle_section_area',
				'trainify_home_bottom_section_area',
			);
			$widget_area_id = ($args['id']) ? $args['id'] : '';
			if(in_array($widget_area_id, $main_widget_areas)){
				$title_tag_name = 'h2';
			}
	        ?>
	        <div class="video-sections" style="background-image:url('<?php echo esc_url($background_image); ?>'); background-size: cover">
	        	<div class="section-wrap">
	        		<div class="section-heading">
	        			<?php if($sub_title){ ?>
	        				<h4 class="section-sub_title"><?php echo esc_html($sub_title); ?></h4>
	        			<?php } ?>
	        			<?php if($title){ ?>
	        				<<?php echo esc_attr($title_tag_name); ?> class="section-title"><?php echo esc_html($title); ?></<?php echo esc_attr($title_tag_name); ?>>
	        			<?php } ?>
	        		</div>
	        		<?php if($video_link){ ?> 
	        			<div class="video-button">
	        				<a href="<?php echo esc_url($video_link); ?>"><i class="fa fa-play" aria-hidden="true"></i></a>
	        			</div>
	        		<?php } ?>
	        	</div>
	        </div>
	        <?php
	        trainify_framework_after_widget($args);
	    }

	}

endif;