<?php
class TheAisleCoreElementorSocialShare extends \Elementor\Widget_Base {
	
	private $socialNetworks;
	
	public function __construct(  $data = [], $args = null ) {
		parent::__construct( $data, $args );
		
		$this->socialNetworks = array(
			'facebook',
			'twitter',
			'google_plus',
			'linkedin',
			'tumblr',
			'pinterest',
			'vk'
		);
	}

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

	public function get_title() {
		return esc_html__( 'Social Share', 'theaisle-core' );
	}

	public function get_icon() {
		return 'theaisle-elementor-custom-icon theaisle-elementor-social-share';
	}

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

	protected function _register_controls() {

		$this->start_controls_section(
			'general',
			[
				'label' => esc_html__( 'General', 'theaisle-core' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);
		$this->add_control(
			'type',
			[
				'label'     => esc_html__( 'Type', 'theaisle-core' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'options' => array(
					'list' => esc_html__( 'List', 'theaisle-core'), 
					'dropdown' => esc_html__( 'Dropdown', 'theaisle-core'), 
					'text' => esc_html__( 'Text', 'theaisle-core')
				),
				'default' => 'list'
			]
		);

		$this->add_control(
			'dropdown_behavior',
			[
				'label'     => esc_html__( 'DropDown Hover Behavior', 'theaisle-core' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'options' => array(
					'bottom' => esc_html__( 'On Bottom Animation', 'theaisle-core'), 
					'right' => esc_html__( 'On Right Animation', 'theaisle-core'), 
					'left' => esc_html__( 'On Left Animation', 'theaisle-core')
				),
				'default' => 'bottom',
				'condition' => [
					'type' => array( 'dropdown' )
				]
			]
		);

		$this->add_control(
			'icon_type',
			[
				'label'     => esc_html__( 'Icons Type', 'theaisle-core' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'options' => array(
					'font-awesome' => esc_html__( 'Font Awesome', 'theaisle-core'), 
					'font-elegant' => esc_html__( 'Font Elegant', 'theaisle-core')
				),
				'default' => 'font-elegant',
				'condition' => [
					'type' => array( 'list', 'dropdown' )
				]
			]
		);

		$this->add_control(
			'title',
			[
				'label'     => esc_html__( 'Social Share Title', 'theaisle-core' ),
				'type'      => \Elementor\Controls_Manager::TEXT
			]
		);


		$this->end_controls_section();
	}
	public function render() {

		$params = $this->get_settings_for_display();

		//Is social share enabled
		$params['enable_social_share'] = theaisle_elated_options()->getOptionValue( 'enable_social_share' ) === 'yes';
		
		//Is social share enabled for post type
		$post_type         = str_replace( '-', '_', get_post_type() );
		$params['enabled'] = theaisle_elated_options()->getOptionValue( 'enable_social_share_on_' . $post_type ) === 'yes';
		
		//Social Networks Data
		$params['networks'] = $this->getSocialNetworksParams( $params );
		
		$params['dropdown_class'] = ! empty( $params['dropdown_behavior'] ) ? 'eltdf-' . $params['dropdown_behavior'] : 'eltdf-bottom';
		
		$html = '';
		
		if ( $params['enable_social_share'] && $params['enabled'] ) {
			$html = theaisle_core_get_shortcode_module_template_part( 'templates/' . $params['type'], 'social-share', '', $params );
		}
		
		echo theaisle_elated_get_module_part($html);
	}

	public function getSocialNetworks() {
		return $this->socialNetworks;
	}
	
	private function getSocialNetworksParams($params) {
		$networks = array();
		$icons_type = $params['icon_type'];
		
		foreach ($this->socialNetworks as $net) {
			$html = '';
			
			if (theaisle_elated_options()->getOptionValue('enable_' . $net . '_share') == 'yes') {
				$image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
				$params = array(
					'name' => $net
				);
				$params['link'] = $this->getSocialNetworkShareLink($net, $image);
				$params['icon'] = $this->getSocialNetworkIcon($net, $icons_type);
				$params['custom_icon'] = (theaisle_elated_options()->getOptionValue($net . '_icon')) ? theaisle_elated_options()->getOptionValue($net . '_icon') : '';
				
				$html = theaisle_core_get_shortcode_module_template_part('templates/parts/network', 'social-share', '', $params);
			}
			
			$networks[$net] = $html;
		}
		
		return $networks;
	}

    private function getSocialNetworkShareLink($net, $image) {
        switch ($net) {
            case 'facebook':
                if (wp_is_mobile()) {
                    $link = 'window.open(\'https://m.facebook.com/sharer.php?u=' . urlencode(get_permalink()) . '\');';
                } else {
                    $link = 'window.open(\'https://www.facebook.com/sharer.php?u=' . urlencode(get_permalink()) . '\', \'sharer\', \'toolbar=0,status=0,width=620,height=280\');';
                }
                break;
            case 'twitter':
                $count_char = (isset($_SERVER['https'])) ? 23 : 22;
                $twitter_via = (theaisle_elated_options()->getOptionValue('twitter_via') !== '') ? ' via ' . theaisle_elated_options()->getOptionValue('twitter_via') . ' ' : '';
	            if (wp_is_mobile()) {
		            $link = 'window.open(\'https://twitter.com/share?text=' . urlencode(theaisle_elated_the_excerpt_max_charlength($count_char) . $twitter_via)  . '&url='. get_permalink() .'\', \'popupwindow\', \'scrollbars=yes,width=800,height=400\');';
	            } else {
		            $link = 'window.open(\'https://twitter.com/home?status=' . urlencode(theaisle_elated_the_excerpt_max_charlength($count_char) . $twitter_via) . get_permalink() . '\', \'popupwindow\', \'scrollbars=yes,width=800,height=400\');';
	            }
	            break;
            case 'google_plus':
                $link = 'popUp=window.open(\'https://plus.google.com/share?url=' . urlencode(get_permalink()) . '\', \'popupwindow\', \'scrollbars=yes,width=800,height=400\');popUp.focus();return false;';
                break;
            case 'linkedin':
                $link = 'popUp=window.open(\'http://linkedin.com/shareArticle?mini=true&amp;url=' . urlencode(get_permalink()) . '&amp;title=' . urlencode(get_the_title()) . '\', \'popupwindow\', \'scrollbars=yes,width=800,height=400\');popUp.focus();return false;';
                break;
            case 'tumblr':
                $link = 'popUp=window.open(\'http://www.tumblr.com/share/link?url=' . urlencode(get_permalink()) . '&amp;name=' . urlencode(get_the_title()) . '&amp;description=' . urlencode(get_the_excerpt()) . '\', \'popupwindow\', \'scrollbars=yes,width=800,height=400\');popUp.focus();return false;';
                break;
            case 'pinterest':
                $link = 'popUp=window.open(\'http://pinterest.com/pin/create/button/?url=' . urlencode(get_permalink()) . '&amp;description=' . theaisle_elated_addslashes(get_the_title()) . '&amp;media=' . urlencode($image[0]) . '\', \'popupwindow\', \'scrollbars=yes,width=800,height=400\');popUp.focus();return false;';
                break;
            case 'vk':
                $link = 'popUp=window.open(\'http://vkontakte.ru/share.php?url=' . urlencode(get_permalink()) . '&amp;title=' . urlencode(get_the_title()) . '&amp;description=' . urlencode(get_the_excerpt()) . '&amp;image=' . urlencode($image[0]) . '\', \'popupwindow\', \'scrollbars=yes,width=800,height=400\');popUp.focus();return false;';
                break;
            default:
                $link = '';
        }

        return $link;
    }

	private function getSocialNetworkIcon( $net, $type ) {
		switch ( $net ) {
			case 'facebook':
				$icon = ( $type == 'font-elegant' ) ? 'social_facebook' : 'fab fa-facebook';
				break;
			case 'twitter':
				$icon = ( $type == 'font-elegant' ) ? 'social_twitter' : 'fab fa-twitter';
				break;
			case 'google_plus':
				$icon = ( $type == 'font-elegant' ) ? 'social_googleplus' : 'fab fa-google-plus';
				break;
			case 'linkedin':
				$icon = ( $type == 'font-elegant' ) ? 'social_linkedin' : 'fab fa-linkedin';
				break;
			case 'tumblr':
				$icon = ( $type == 'font-elegant' ) ? 'social_tumblr' : 'fab fa-tumblr';
				break;
			case 'pinterest':
				$icon = ( $type == 'font-elegant' ) ? 'social_pinterest' : 'fab fa-pinterest';
				break;
			case 'vk':
				$icon = 'fab fa-vk';
				break;
			default:
				$icon = '';
		}
		
		return $icon;
	}

	private function getSocialNetworkText( $net ) {
		switch ( $net ) {
			case 'facebook':
				$text = esc_html__( 'fb', 'theaisle-core');
				break;
			case 'twitter':
				$text = esc_html__( 'tw', 'theaisle-core');
				break;
			case 'google_plus':
				$text = esc_html__( 'g+', 'theaisle-core');
				break;
			case 'linkedin':
				$text = esc_html__( 'lnkd', 'theaisle-core');
				break;
			case 'tumblr':
				$text = esc_html__( 'tmb', 'theaisle-core');
				break;
			case 'pinterest':
				$text = esc_html__( 'pin', 'theaisle-core');
				break;
			case 'vk':
				$text = esc_html__( 'vk', 'theaisle-core');
				break;
			default:
				$text = '';
		}
		
		return $text;
	}

}
\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new TheAisleCoreElementorSocialShare() );