<?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\FABMetaboxDesign;
use Fab\Metabox\FABMetaboxSetting;
use Fab\Metabox\FABMetaboxTrigger;
use Fab\Metabox\Setting\Setting;

/**
 * Metabox Setting
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Metabox_Setting extends Base implements Model_Interface {

    /**
     * Register metabox settings on custom post type Fab
     *
     * @return      void
     */
    public function metabox_settings() {
        add_meta_box(
            'fab-metabox-settings',
            __( 'Setting', 'floating-awesome-button' ),
            array( $this, 'metabox_settings_callback' ),
            'fab',
            'normal',
            'default',
            array( 'is_display' => false )
        );
    }

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

        // Grab Data.
        $fab     = new FABItem( $post->ID );
        $fab     = $fab->getVars();
        $setting = new Setting( $fab );

        // Enqueue Localize Script.
        \FAB()->Helper->wp_enqueue_script_component( 'metabox-setting-component', 'assets/components/metabox-setting/main.js', array(), FAB_VERSION, true );
        wp_localize_script(
            'metabox-setting-component',
            'FAB_METABOX_SETTING',
            apply_filters(
                'fab_backend_enequeue_metabox_setting_localize',
                array(
                    'tabs' => $setting->get_tabs(),
                    'url'  => array(
                        'documentation' => \FAB()->Helper->get_documentation( '' ), // We set empty string because we will generate it dynamically in frontend.
                    ),
                )
            )
        );

        // Render View.
        View::RenderStatic( 'Backend.Metabox.setting', compact( 'fab' ) );
    }

    /**
     * Save Metabox Data.
     *
     * @param int     $post_id The post ID.
     * @param WP_Post $post    The post object.
     * @param bool    $update  Whether this is an update.
     */
    public function save_metabox_data( $post_id, $post, $update ) {
        // Ignore save metabox data if the post status is trash.
        if ( 'trash' === $post->post_status ) {
            return;
        }

        // Save Metabox Setting.
        if ( \Fab()->Helper->check_input( FABMetaboxSetting::get_input() ) ) {
            $metabox = new FABMetaboxSetting();
            $metabox->sanitize();
            $metabox->setDefaultInput();
            $metabox->save();
        }

        // Save Metabox Design.
        if ( \Fab()->Helper->check_input( FABMetaboxDesign::$input ) ) {
            $metabox = new FABMetaboxDesign();
            $metabox->sanitize();
            $metabox->setDefaultInput();
            $metabox->save();
        }

        // Save Metabox Trigger.
        if ( \Fab()->Helper->check_input( FABMetaboxTrigger::$input ) ) {
            $metabox = new FABMetaboxTrigger();
            $metabox->sanitize();
            $metabox->setDefaultInput();
            $metabox->save();
        }
    }

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

    /**
     * Execute Class.
     *
     * @return void
     */
    public function run() {
        // Add Setting metabox to Fab CPT.
        add_action( 'add_meta_boxes', array( $this, 'metabox_settings' ), 10, 0 );

        // Save Metabox Data.
        add_action( 'fab_save_post', array( $this, 'save_metabox_data' ), 10, 3 );
    }
}
