<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;
use Fab\Module\FABModuleSearch;
use Fab\View;

/**
 * Shortcodes
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Shortcodes extends Base implements JIT_Interface {

    /**
     * Set View for [fab_search] shortcode
     *
     * @return      string              Html template string from view View/Frontend/search.php
     */
    public function fab_search() {
        $module = new FABModuleSearch();

        ob_start();
            $module->render();
        return ob_get_clean();
    }

    /**
     * Set View for [fab_ebook_mockup] shortcode
     *
     * @param array $atts Shortcode attributes.
     * @return string
     */
    public function fab_ebook_mockup( $atts ) {
        $atts = shortcode_atts(
            array(
                'cover_image' => 'https://placehold.co/400x600',
                'title'       => __( 'The Art of Simple Living', 'floating-awesome-button' ),
                'author'      => __( 'Jane Doe', 'floating-awesome-button' ),
                'description' => __( 'Discover practical strategies to simplify your life and find joy in everyday moments. This guide offers 100+ actionable tips for a more peaceful, productive life.', 'floating-awesome-button' ),
                'button_text' => __( 'Buy Now', 'floating-awesome-button' ),
                'button_link' => '#',
            ),
            $atts,
            'fab_ebook_mockup'
        );

        ob_start();
            View::RenderStatic( 'Frontend.Mockup.ebook', compact( 'atts' ) );
        return ob_get_clean();
    }

    /**
     * Set View for [fab_whatsapp_form] shortcode
     *
     * @param array $atts Shortcode attributes.
     * @return string
     */
    public function fab_whatsapp_form( $atts ) {
        // We use custom check because WordPress doesn't allow input and texetarea on their shortcode.
        if ( is_admin() || did_action( 'save_post' ) ) {
            return;
        }

        $atts = shortcode_atts(
            array(
                'description'         => __( 'Contact us via WhatsApp', 'floating-awesome-button' ),
                'phone_number'        => '+1234567890',
                'name_placeholder'    => __( 'Your Name', 'floating-awesome-button' ),
                'message_placeholder' => __( 'Hello, I want to order your service.', 'floating-awesome-button' ),
            ),
            $atts,
            'fab_whatsapp_form'
        );

        // We don't return because WordPress doesn't allow input and textarea on shortcode.
        View::RenderStatic( 'Frontend.Whatsapp.form', compact( 'atts' ) );
    }

    /**
     * Set View for [fab_social_sharing] shortcode
     *
     * @param array $atts Shortcode attributes.
     * @return string
     */
    public function fab_social_sharing( $atts ) {
        // Setup attributes.
        $atts = shortcode_atts(
            array(
                'title'   => get_the_title(),
                'url'     => get_the_permalink(),
                'exclude' => '',
            ),
            $atts,
            'fab_social_sharing'
        );

        // Render view.
        ob_start();
            View::RenderStatic( 'Frontend.social_sharing', compact( 'atts' ) );
        return ob_get_clean();
    }

    /**
     * Set View for [fab_related_post] shortcode
     *
     * @param array $atts Shortcode attributes.
     * @return string
     */
    public function fab_related_post( $atts ) {
        // Setup attributes.
        $atts = shortcode_atts(
            array(
                'title'         => get_the_title(),
                'url'           => get_the_permalink(),
                'display_count' => '',
            ),
            $atts,
            'fab_related_post'
        );

        // Render view.
        ob_start();
            View::RenderStatic( 'Frontend.related_post', compact( 'atts' ) );
        return ob_get_clean();
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add shortcode for the search form.
        add_shortcode( 'fab_search', array( __CLASS__, 'jit_fab_search' ) );

        // Add ebook mockup shortcode.
        add_shortcode( 'fab_ebook_mockup', array( __CLASS__, 'jit_fab_ebook_mockup' ) );

        // Add whatsapp form shortcode.
        add_shortcode( 'fab_whatsapp_form', array( __CLASS__, 'jit_fab_whatsapp_form' ) );

        // Add social sharing shortcode.
        add_shortcode( 'fab_social_sharing', array( __CLASS__, 'jit_fab_social_sharing' ) );

        // Add post recommendation shortcode.
        add_shortcode( 'fab_related_post', array( __CLASS__, 'jit_fab_related_post' ) );
    }
}
