<?php

namespace Fab\Controller;

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

use Fab\Interfaces\Model_Interface;
use Fab\View;
use Fab\Helper\FABItem;
use Fab\Feature\Design;
use Fab\Feature\Modal;
use Fab\Metabox\FABMetaboxSetting;
use Fab\Metabox\FABMetaboxTrigger;

/**
 * Metabox Post
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Metabox_Post extends Base implements Model_Interface {

    /**
     * Register metabox product on custom post type WooCommerce Product
     *
     * @return void
     */
    public function metabox_post_page() {
        global $post;

        // Don't show metabox on add new screens.
        $screen = get_current_screen();
        if ( ! $screen || 'add' === $screen->action ) {
            return;
        }

        // Make sure $post is an object.
        if ( ! is_object( $post ) || ! isset( $post->ID ) ) {
            return;
        }

        // Don't show metabox on pages with query strings.
        $permalink = get_permalink( $post->ID );
        if ( false !== strpos( $permalink, '?' ) ) {
            return;
        }

        // Get recommendations.
        $max_recommendations = 4;
        $recommendations     = apply_filters( 'fab_metabox_post_recommendations', array(), $max_recommendations );
        // If no recommendations, return.
        if ( empty( $recommendations ) ) {
            return;
        }

        // Add metabox.
        add_meta_box(
            'fab-metabox-post',
            __( 'Floating Awesome Button (FAB)', 'floating-awesome-button' ),
            array( $this, 'metabox_post_callback' ),
            'post',
            'normal',
            'default'
        );

        // Add metabox.
        add_meta_box(
            'fab-metabox-page',
            __( 'Floating Awesome Button (FAB)', 'floating-awesome-button' ),
            array( $this, 'metabox_post_callback' ),
            'page',
            'normal',
            'default'
        );
    }

    /**
     * Metabox Post set view template
     *
     * @return void
     */
    public function metabox_post_callback() {
        global $post;

        // Get recommendations.
        $max_recommendations = 4;
        $recommendations     = apply_filters( 'fab_metabox_post_recommendations', array(), $max_recommendations );

        // Enqueue Script & component.
        \FAB()->Helper->wp_enqueue_script_component( 'metabox-post-component', 'assets/components/metabox-post/main.js', array(), FAB_VERSION, true );
        wp_localize_script(
            'metabox-post-component',
            'FAB_METABOX_POST',
            apply_filters(
                'fab_backend_enqueue_metabox_post_localize',
                array(
                    'urls'            => array(
                        'url_tips' => ( 'post' === $post->post_type ) ? ARTISTUDIO_URL . 'tag/blogger/' : ARTISTUDIO_URL . 'tag/business/',
                    ),
                    'data'            => array(),
                    'labels'          => array_merge(
                        \Fab\Helper\Metabox::getInstance()->get_metabox_labels(),
                        array(
                            'get_tips' => __( 'Get Tips', 'floating-awesome-button' ),
                            'tips'     => ( 'post' === $post->post_type ) ?
                                __( 'Get expert blogging tips that will help you grow your audience, boost your website traffic, and increase your conversion rates. Learn proven strategies for content creation, SEO optimization, social media promotion, and reader engagement to take your blog to the next level.', 'floating-awesome-button' ) :
                                __( 'Get expert business tips that will help you grow your business, increase your sales, and build a successful online presence. Learn proven strategies for marketing, sales, and customer engagement to take your business to the next level.', 'floating-awesome-button' ),
                        )
                    ),
                    'documentations'  => array(),
                    'recommendations' => $recommendations,
                    'post_id'         => $post->ID,
                    'nonce'           => wp_create_nonce( 'wp_rest' ),
                )
            )
        );

        // Enqueue style.
        \FAB()->Helper->wp_enqueue_style_sass( 'fab', 'assets/css/backend/style.scss' );

        // Load vendors.
        \FAB()->Helper->wp_enqueue_script( 'fab-echarts', 'vendor/echarts/echarts.min.js', array(), FAB_VERSION, true );
        \FAB()->Helper->wp_enqueue_style( 'fontawesome', apply_filters( 'fab_fontawesome_css', 'vendor/fontawesome/css/all.min.css' ) );

        // Render View.
        View::RenderStatic( 'Backend.Metabox.post' );
    }

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

    /**
     * Execute Class.
     *
     * @return void
     */
    public function run() {
        // @backend - Add metabox to Post
        add_action( 'add_meta_boxes', array( $this, 'metabox_post_page' ), 10, 0 );
    }
}
