<?php

namespace ThimPress\ThimBlocks\Gutenberg\Blocks\SocialShareItem;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use ThimPress\ThimBlocks\Gutenberg\Blocks\AbstractBlockType;
use ThimPress\ThimBlocks\Helpers\SVGHelper;
use Throwable;

class SocialShareItemBlockType extends AbstractBlockType {

	public $block_name = 'social-share-item';

	public function get_supports(): array {
		return array(
			'align'                => true,
			'html'                 => false,
			'spacing'              => array(
				'margin'          => true,
				'padding'         => true,
				'defaultControls' => array(
					'margin'  => true,
					'padding' => true,
				),
			),
			'typography'           => array(
				'fontSize'        => true,
				'fontWeight'      => true,
				'fontStyle'       => true,
				'textTransform'   => true,
				'defaultControls' => array(
					'fontSize' => true,
				),
			),
			'color'                => array(
				'text'            => true,
				'link'            => true,
				'background'      => true,
				'defaultControls' => array(
					'text'       => true,
					'background' => true,
					'link'       => false,
				),
			),
			'__experimentalBorder' => array(
				'color'           => true,
				'radius'          => true,
				'style'           => true,
				'width'           => true,
				'defaultControls' => array(
					'radius' => true,
					'width'  => true,
				),
			),
		);
	}

	public function render_content_block_template( array $attributes, $content, $block ): string {
		$html = '';

		try {
			$icon_class = $attributes['iconClass'] ?? '';
			$iconSVG    = $attributes['iconSVG'] ?? '';
			$network    = $attributes['network'] ?? 'facebook';

			$post_url     = get_permalink();
			$post_title   = get_the_title();
			$post_excerpt = get_the_excerpt();
			$post_thumb   = wp_get_attachment_url( get_post_thumbnail_id() );

			$link_share = '#';

			switch ( $network ) {
				case 'facebook':
					$link_share = 'https://www.facebook.com/sharer.php?u=' . urlencode( $post_url );
					break;
				case 'twitter':
					$link_share = 'https://twitter.com/share?url=' . urlencode( $post_url ) . '&text=' . rawurlencode( $post_title );
					break;
				case 'pinterest':
					$link_share = 'http://pinterest.com/pin/create/button/?url=' . urlencode( $post_url ) . '&description=' . rawurlencode( $post_excerpt ) . '&media=' . urlencode( $post_thumb );
					break;
				case 'linkedin':
					$link_share = 'https://www.linkedin.com/shareArticle?mini=true&url=' . urlencode( $post_url ) . '&title=' . rawurlencode( $post_title ) . '&summary=' . rawurlencode( $post_excerpt );
					break;
				case 'reddit':
					$link_share = 'https://www.reddit.com/submit?url=' . urlencode( $post_url ) . '&title=' . rawurlencode( $post_title );
					break;
				case 'blogger':
					$link_share = 'https://www.blogger.com/blog-this.g?u=' . urlencode( $post_url ) . '&n=' . rawurlencode( $post_title ) . '&t=' . rawurlencode( $post_excerpt );
					break;
				case 'tumblr':
					$link_share = 'https://www.tumblr.com/widgets/share/tool?canonicalUrl=' . urlencode( $post_url ) . '&title=' . rawurlencode( $post_title ) . '&caption=' . rawurlencode( $post_excerpt );
					break;
				case 'stumbleupon':
					$link_share = 'http://www.stumbleupon.com/submit?url=' . urlencode( $post_url ) . '&title=' . rawurlencode( $post_title );
					break;
				case 'pocket':
					$link_share = 'https://getpocket.com/edit?url=' . urlencode( $post_url ) . '&title=' . rawurlencode( $post_title );
					break;
				case 'whatsapp':
					$link_share = 'https://api.whatsapp.com/send?text=' . rawurlencode( $post_title . ' ' . $post_url );
					break;
			}

			ob_start();
			?>
			<a <?php echo wp_kses_post( get_block_wrapper_attributes( array( 'class' => 'thim-blocks-social-share-item share-' . esc_attr( $network ) ) ) ); ?>
				href="<?php echo esc_url( $link_share ); ?>" target="_blank" rel="noopener noreferrer">
				<?php
				$svg_output = SVGHelper::render_svg( $iconSVG );

				if ( $svg_output ) {
					echo wp_kses( $svg_output, SVGHelper::allowed_tags() );
				} elseif ( ! empty( $icon_class ) ) {
					echo '<i class="thim-social-share__icon ' . esc_attr( $icon_class ) . '"></i>';
				} elseif ( ! empty( $network ) ) {
					echo '<i class="thim-social-share__icon tb-icon-' . esc_attr( $network ) . '"></i>';
				}
				?>
			</a>
			<?php
			$html = ob_get_clean();
		} catch ( Throwable $e ) {
			return $html;
		}

		return $html;
	}

}
