<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Social Sharing Block
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Social_Sharing_Block extends Controller implements JIT_Interface {

    /**
     * Post meta key.
     *
     * @var string
     */
    public $post_meta_key = array(
        'fab_social_sharing_buttons' => array(
            'meta_key' => 'fab_social_sharing_buttons',
        ),
    );

    /**
     * Register the block
     *
     * @return void
     */
    public function register_block() {
        // Register block.
        register_block_type(
            FAB_PLUGIN_PATH . '/assets/build/blocks/social-sharing',
            array(
                'render_callback' => array( __CLASS__, 'jit_render_block' ),
            )
        );
    }

    /**
     * Render the block
     *
     * @return string
     */
    public function render_block() {
        // Get exclude buttons.
        $social_media = apply_filters( 'fab_list_of_social_buttons', array(), array() );
        ksort( $social_media );

        // Get fab_social_sharing_buttons postmeta.
        $fab_social_sharing_buttons = get_post_meta( get_the_ID(), $this->post_meta_key['fab_social_sharing_buttons']['meta_key'], true );
        $fab_social_sharing_buttons = $fab_social_sharing_buttons ? $fab_social_sharing_buttons : array_keys( $social_media );

        // Get exclude buttons.
        $exclude_buttons = array_diff( array_keys( $social_media ), $fab_social_sharing_buttons );

        return do_shortcode( sprintf( '[fab_social_sharing exclude="%s"]', implode( ',', $exclude_buttons ) ) );
    }

    /**
     * Enqueue block assets.
     *
     * @return void
     */
    public function enqueue_block_assets() {
        // Grab social media buttons.
        $social_media = apply_filters( 'fab_list_of_social_buttons', array(), array() );
        ksort( $social_media );

        // Get fab_social_sharing_buttons postmeta.
        $fab_social_sharing_buttons = get_post_meta( get_the_ID(), $this->post_meta_key['fab_social_sharing_buttons']['meta_key'], true );
        $fab_social_sharing_buttons = $fab_social_sharing_buttons ? $fab_social_sharing_buttons : array_keys( $social_media );

        // Localize script.
        \FAB()->Helper->wp_enqueue_script( 'fab-block-social-sharing', 'local/fab.js', array(), FAB_VERSION, true );
        wp_localize_script(
            'fab-block-social-sharing',
            'FAB_BLOCK_SOCIAL_SHARING',
            array(
                'nonce'        => wp_create_nonce( 'fab_social_sharing_block' ),
                'social_media' => $social_media,
                'options'      => array(
                    'buttons' => $fab_social_sharing_buttons,
                ),
            )
        );
    }

    /**
     * Ajax method to save options.
     *
     * @return void
     */
    public function ajax_save_social_sharing_block_options() {
        // Check nonce.
        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'fab_social_sharing_block' ) ) {
            wp_send_json_error( 'Invalid nonce' );
        }

        // Get the post ID.
        $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
        if ( ! $post_id ) {
            wp_send_json_error( 'Post ID is required' );
        }

        // Get the options.
        $options = isset( $_POST['options'] ) ? json_decode( stripslashes( $_POST['options'] ), true ) : array();

        // Update the post meta.
        update_post_meta( $post_id, $this->post_meta_key['fab_social_sharing_buttons']['meta_key'], $options['buttons'] );

        // Send the response.
        wp_send_json_success( 'Options saved successfully' );
    }

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

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Register block.
        add_action( 'init', array( __CLASS__, 'jit_register_block' ) );

        // Enqueue block assets.
        add_action( 'enqueue_block_assets', array( __CLASS__, 'jit_enqueue_block_assets' ) );

        // Register ajax api to save options.
        add_action( 'wp_ajax_fab_save_social_sharing_block_options', array( __CLASS__, 'jit_ajax_save_social_sharing_block_options' ) );
    }
}
