<?php
/**
 * @widget_name: Social Icons Widget
 * @description: This class handles everything that needs to be handled with the widget:the settings, form, display, and update.  Nice!
 * @package: MatrixAddons
 * @subpackage: trainifyframework
 * @author: MatrixAddons
 * @author_uri: https://matrixaddons.com
 * @since 1.0.0
 */
class TrainifyFramework_Social_Icons_Widget extends TrainifyFramework_Master_Widget{
	
	public  function __construct(){

		$widget_options = array(
			'classname' => 'trainify-framework-social-icons',
			'description' => esc_html__( 'A Widget to display social icons.', 'trainify' ));
		parent::__construct('trainify-framework-social-icons', esc_html__( 'TF - Social Icons', 'trainify' ), $widget_options);

	}

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

        $trainify_framework_link_target = trainify_framework_link_target();

        $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_wraper'   => 'title',
                                'trainify_framework_widget_field_title'    => esc_html__( 'Title', 'trainify' ),
                                'trainify_framework_widget_field_default'  => esc_html__( 'Connect with us', 'trainify'),
                                'trainify_framework_widget_field_type'     => 'text',
                            ),
                            'title_target'    => array(
                                'trainify_framework_widget_field_name'     => 'title_target',
                                'trainify_framework_widget_field_wraper'   => 'title-target',
                                'trainify_framework_widget_field_title'    => esc_html__( 'Link Target', 'trainify' ),
                                'trainify_framework_widget_field_default'  => '_self',
                                'trainify_framework_widget_field_type'     => 'select',
                                'trainify_framework_widget_field_options'  => $trainify_framework_link_target,
                                'trainify_framework_widget_field_relation' => array(
                                    'exist' => array(
                                        'show_fields'   => array(
                                            'title-link', 
                                        ),
                                    ),
                                    'empty' => array(
                                        'hide_fields'   => array(
                                            'title-link', 
                                        ),
                                    ),
                                ),
                            ),
                            'title_link'    => array(
                                'trainify_framework_widget_field_name'     => 'title_link',
                                'trainify_framework_widget_field_wraper'   => 'title-link',
                                'trainify_framework_widget_field_title'    => esc_html__( 'Title link', 'trainify' ),
                                'trainify_framework_widget_field_default'  => '',
                                'trainify_framework_widget_field_type'     => 'text',
                            ),
                            'social_icon_size'        => array(
                                'trainify_framework_widget_field_name'         => 'social_icon_size',
                                'trainify_framework_widget_field_title'        => esc_html__( 'Icons Size', 'trainify' ),
                                'trainify_framework_widget_field_default'      => '',
                                'trainify_framework_widget_field_type'         => 'select',
                                'trainify_framework_widget_field_options'      => trainify_framework_faicon_sizes(),
                            ),
                            'social_media_target'        => array(
                                'trainify_framework_widget_field_name'         => 'social_media_target',
                                'trainify_framework_widget_field_title'        => esc_html__( 'Social icon open with', 'trainify' ),
                                'trainify_framework_widget_field_default'      => '_blank',
                                'trainify_framework_widget_field_type'         => 'select',
                                'trainify_framework_widget_field_options'      => $trainify_framework_link_target,
                            ),
                            'social_icon_list'         => array(
                                'trainify_framework_widget_field_name'     => 'social_icon_list',
                                'trainify_framework_widget_field_title'    => esc_html__( 'Social Icon List', 'trainify' ),
                                'trainify_framework_widget_field_type'     => 'repeater',
                                'trainify_framework_widget_description'    => esc_html__('To add social icon click to add icon.', 'trainify'),
                                'tff_repeater_row_title'    => esc_html__('Social Icon', 'trainify'),
                                'tff_repeater_addnew_label' => esc_html__('Add Icon', 'trainify'),
                                'trainify_framework_widget_field_default' => array(
                                    array(
                                        'social_media_link' => 'https://facebook.com/',
                                        'social_media_icon' => 'fa-facebook',
                                        'social_media_color' => '#0056D2',
                                    ),
                                    array(
                                        'social_media_link' => 'https://twitter.com/',
                                        'social_media_icon' => 'fa-twitter',
                                        'social_media_color' => '#214d74',
                                    ),
                                    array(
                                        'social_media_link' => 'https://linkedin.com/',
                                        'social_media_icon' => 'fa-linkedin',
                                        'social_media_color' => '#214d74',
                                    ),
                                ),
                                'trainify_framework_widget_field_options'  => array(
                                    'social_media_icon'  => array(
                                        'trainify_framework_widget_field_name'     => 'social_media_icon',
                                        'trainify_framework_widget_field_title'    => esc_html__( 'Social Media Icon', 'trainify' ),
                                        'trainify_framework_widget_field_default'  => '',
                                        'trainify_framework_widget_field_type'     => 'icon',
                                    ),
                                    'social_media_link' => array(
                                        'trainify_framework_widget_field_name'     => 'social_media_link',
                                        'trainify_framework_widget_field_title'    => esc_html__( 'Social Media Link', 'trainify' ),
                                        'trainify_framework_widget_field_default'  => '',
                                        'trainify_framework_widget_field_type'     => 'url',
                                    ),
                                    'social_media_color' => array(
                                        'trainify_framework_widget_field_name'     => 'social_media_color',
                                        'trainify_framework_widget_field_title'    => esc_html__( 'Social Icon Color', 'trainify' ),
                                        'trainify_framework_widget_field_default'  => '#00a0d2',
                                        'trainify_framework_widget_field_type'     => 'color',
                                    ),
                                ),
                            ),
                            
                        )
),
),
),
);

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

}

	/**
	 * Display the widget
	 */	
	function widget( $args, $instance ) {

        /*
         * Args Values
         */
        $before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
        $after_title  = isset( $args['after_title'] ) ? $args['after_title'] : '';

        $title = isset( $instance['title'] ) ? esc_attr($instance['title']) : '';
        $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
        $title_link = isset( $instance['title_link'] ) ? esc_url($instance['title_link']) : '';
        $title_target = isset( $instance['title_target'] ) ? esc_attr($instance['title_target']) : '';
        $social_icon_size = isset( $instance['social_icon_size'] ) ? esc_attr($instance['social_icon_size']) : '';
        $social_media_target = isset( $instance['social_media_target'] ) ? esc_attr($instance['social_media_target']) : '';
        $social_icon_list = isset( $instance['social_icon_list'] ) ? $instance['social_icon_list'] : array();

		/* Before widget (defined by themes).
		 * Display the widget title if one was input (before and after defined by themes). 
		 */
		trainify_framework_before_widget($args);

        $title_args = array(
            'title' => $title,
            'title_target'=> $title_target,
            'title_link' => $title_link,
            'before_title'=>$before_title,
            'after_title'=>$after_title
        );
        do_action('trainify_framework_widget_title', $title_args);
        ?>
        <div class="social-icons">
            <?php
            foreach($social_icon_list as $index=>$social_media_details){

                $social_media_link = (isset($social_media_details['social_media_link'])) ? esc_attr($social_media_details['social_media_link']) : '';
                $social_media_icon = (isset($social_media_details['social_media_icon'])) ? esc_attr($social_media_details['social_media_icon']) : '';
                $social_media_color = (isset($social_media_details['social_media_color'])) ? esc_attr($social_media_details['social_media_color']) : '';
                ?><a 
                title="<?php esc_attr_e('Trainify Social Media Icons', 'trainify'); ?>"
                target="<?php echo esc_attr($social_media_target); ?>" 
                <?php if($social_media_target){ ?>
                    href="<?php echo esc_attr($social_media_link); ?>" 
                <?php } ?> 
                style="background-color:<?php echo esc_attr( $social_media_color ); ?>"
                ><i 
                class="fa <?php echo esc_attr($social_media_icon).' '.esc_attr($social_icon_size); ?>" 
                ></i></a><?php
            }
            ?>
        </div>
        <!-- End  social-icons -->
        <?php	
        /* After widget (defined by themes). */
        trainify_framework_after_widget($args);

    }

}
