<?php

namespace Fab\Controller;

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

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

/**
 * Print Type
 * - We call this class Print_Type because Print is already a class in PHP/WordPress.
 * - Getting error when using Print as class name.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Print_Type extends Bar_Button_Group implements JIT_Interface {

    /**
     * Constructor.
     */
    public function __construct() {
        parent::__construct();
        $this->type = 'print';
    }

    /**
     * Get labels type.
     *
     * @return array
     */
    public function get_labels_type() {
        return array(
            'name' => __( 'Print', 'floating-awesome-button' ),
        );
    }

    /**
     * Add print 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 print target.
        $extra_options          = $instance->getExtraOptions();
        $extra_options['print'] = array(
            'target' => get_post_meta( $instance->getID(), FABMetaboxSetting::$post_metas['print_target']['meta_key'], true ),
        );
        $instance->setExtraOptions( $extra_options );
    }

    /**
     * Filter template postmeta.
     *
     * @param array  $postmeta The postmeta.
     * @param string $type The type.
     * @param object $data The data.
     * @return array
     */
    public function filter_template_postmeta( $postmeta, $type, $data ) {
        if ( $this->type !== $type ) {
            return $postmeta;
        }

        $postmeta['fab_setting_print_target'] = isset( $data->settings->target ) ? $data->settings->target : 'body';

        return $postmeta;
    }

    /**
     * Add fab metabox setting tabs fields.
     *
     * @param array $tabs
     * @param array $fab
     * @return array
     */
    public function add_fab_metabox_setting_tabs_fields( $tabs, $fab ) {
        // Set print target field.
        $tabs['setting']['fields']['print_target'] = array(
            'type'       => 'text',
            'name'       => 'fab_setting_print_target',
            'label'      => __( 'Print Target', 'floating-awesome-button' ),
            'tooltip'    => __( 'Specify the element to print when button is clicked', 'floating-awesome-button' ),
            'info'       => __( 'CSS selector of element to print. Default is body.', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['print']['target'] ) ? $fab['extraOptions']['print']['target'] : 'body',
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        return $tabs;
    }

    /*
    |--------------------------------------------------------------------------
    | 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 fab setting types.
        add_filter( 'fab_setting_types', array( __CLASS__, 'jit_add_fab_setting_types' ), 10, 1 );

        // Filter template postmeta.
        add_filter( 'fab_template_postmeta', array( __CLASS__, 'jit_filter_template_postmeta' ), 10, 3 );

        // Get fab metabox setting tabs fields.
        add_filter( 'fab_metabox_setting_tabs', array( __CLASS__, 'jit_add_fab_metabox_setting_tabs_fields' ), 10, 2 );
    }
}
