<?php

namespace Fab\Controller;

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

use Fab\Metabox\FABMetaboxSetting;
use Fab\Interfaces\JIT_Interface;
use Fab\Helper\FABModal;

/**
 * Social Sharing Frontend.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Social_Sharing_Frontend extends Social_Sharing_Type implements JIT_Interface {

    /**
     * Add link data to FAB item
     *
     * @param object $instance FAB item instance.
     * @return void
     */
    public function add_fab_item_data( $instance ) {
        if ( $instance->getType() !== $this->type ) {
            return;
        }

        // Set social media popup.
        $modal = new FABModal( $instance->getID() );
        $instance->setModal( $modal );

        // Set extra options.
        $social_media = get_post_meta( $instance->getID(), $this->post_meta['social_media']['meta_key'], true );
        $instance->setExtraOptions( array( 'social_media' => $social_media ) );

        // Exclude social media.
        $exclude_social_media = is_array( $social_media ) ? array_filter(
            $social_media,
            function ( $value ) {
                return 'no' === $value;
            }
        ) : array();

        // Set content.
        $content  = $instance->getRawContent();
        $content .= empty( $exclude_social_media ) ?
            '[fab_social_sharing]' :
            sprintf( '[fab_social_sharing exclude="%s"]', implode( ',', array_keys( $exclude_social_media ) ) );
        $instance->setRawContent( $content );
    }

    /**
     * Add render function.
     *
     * @param object $instance FAB item instance.
     * @return void
     */
    public function add_fab_item_render( $instance ) {
        if ( $instance->getType() !== $this->type ) {
            return;
        }

        $instance->render_content();
    }

    /**
     * Add social buttons.
     *
     * @param array $buttons Social buttons.
     * @param array $atts    Shortcode attributes.
     * @return array
     */
    public static function add_fab_list_of_social_buttons( $buttons, $atts = array() ) {
        // Set default attributes.
        $atts = array_merge(
            array(
                'title' => '',
                'url'   => '',
            ),
            $atts
        );

        // Add social buttons.
        return array_merge(
            $buttons,
            array(
                'copy'      => array(
                    'icon'    => 'fas fa-copy',
                    'label'   => __( 'Copy', 'floating-awesome-button' ),
                    'onclick' => sprintf( 'navigator.clipboard.writeText("%s")', $atts['url'] ), // This can only be run on https website.
                    'class'   => 'bg-gray-500',
                ),
                'email'     => array(
                    'icon'   => 'fas fa-envelope',
                    'label'  => __( 'Email', 'floating-awesome-button' ),
                    'url'    => sprintf( 'mailto:?subject=%s&body=%s', $atts['title'], $atts['url'] ),
                    'class'  => 'bg-yellow-500',
                    'target' => '_blank',
                ),
                'facebook'  => array(
                    'icon'   => 'fab fa-facebook-f',
                    'label'  => __( 'Facebook', 'floating-awesome-button' ),
                    'url'    => sprintf( 'https://www.facebook.com/sharer/sharer.php?u=%s', $atts['url'] ),
                    'class'  => 'bg-blue-500',
                    'target' => '_blank',
                ),
                'linkedin'  => array(
                    'icon'   => 'fab fa-linkedin-in',
                    'label'  => __( 'LinkedIn', 'floating-awesome-button' ),
                    'url'    => sprintf( 'https://www.linkedin.com/sharing/share-offsite/?url=%s', $atts['url'] ),
                    'class'  => 'bg-blue-600',
                    'target' => '_blank',
                ),
                'messenger' => array(
                    'icon'   => 'fab fa-facebook-messenger',
                    'label'  => __( 'Messenger', 'floating-awesome-button' ),
                    'url'    => sprintf( 'https://www.facebook.com/dialog/send?link=%s&app_id=291494419107518&redirect_uri=%s', $atts['url'], $atts['url'] ),
                    'class'  => 'bg-blue-500',
                    'target' => '_blank',
                ),
                'pinterest' => array(
                    'icon'   => 'fab fa-pinterest-p',
                    'label'  => __( 'Pinterest', 'floating-awesome-button' ),
                    'url'    => sprintf( 'https://pinterest.com/pin/create/button/?url=%s&description=%s', $atts['url'], $atts['title'] ),
                    'class'  => 'bg-red-600',
                    'target' => '_blank',
                ),
                'reddit'    => array(
                    'icon'   => 'fab fa-reddit-alien',
                    'label'  => __( 'Reddit', 'floating-awesome-button' ),
                    'url'    => sprintf( 'https://reddit.com/submit?url=%s&title=%s', $atts['url'], $atts['title'] ),
                    'class'  => 'bg-orange-600',
                    'target' => '_blank',
                ),
                'telegram'  => array(
                    'icon'   => 'fab fa-telegram-plane',
                    'label'  => __( 'Telegram', 'floating-awesome-button' ),
                    'url'    => sprintf( 'https://telegram.me/share/url?url=%s&text=%s', $atts['url'], $atts['title'] ),
                    'class'  => 'bg-blue-500',
                    'target' => '_blank',
                ),
                'twitter'   => array(
                    'icon'   => 'fab fa-twitter',
                    'label'  => __( 'Twitter', 'floating-awesome-button' ),
                    'url'    => sprintf( 'https://twitter.com/intent/tweet?url=%s&text=%s', $atts['url'], $atts['title'] ),
                    'class'  => 'bg-blue-400',
                    'target' => '_blank',
                ),
                'whatsapp'  => array(
                    'icon'   => 'fab fa-whatsapp',
                    'label'  => __( 'WhatsApp', 'floating-awesome-button' ),
                    'url'    => sprintf( 'https://api.whatsapp.com/send?text=%s %s', $atts['title'], $atts['url'] ),
                    'class'  => 'bg-green-500',
                    'target' => '_blank',
                ),
            )
        );
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Adjust item data.
        add_action( 'fab_item_data', array( __CLASS__, 'jit_add_fab_item_data' ), 10, 1 );

        // Add render function.
        add_action( 'fab_item_render', array( __CLASS__, 'jit_add_fab_item_render' ), 10, 1 );

        // Add social buttons.
        add_filter( 'fab_list_of_social_buttons', array( __CLASS__, 'jit_add_fab_list_of_social_buttons' ), 10, 2 );
    }
}
