<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Link Group.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Link_Group extends Group implements JIT_Interface {

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

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

    /**
     * Add fab metabox setting tabs fields.
     *
     * @param array $tabs The tabs.
     * @param array $fab The fab.
     * @return array
     */
    public function add_fab_metabox_setting_tabs_fields( $tabs, $fab ) {
        // Set link field.
        $tabs['setting']['fields']['link'] = array(
            'type'       => 'text',
            'name'       => 'fab_setting_link',
            'label'      => __( 'Link Address', 'floating-awesome-button' ),
            'tooltip'    => __( 'Specify the link address for the button. This can be a full URL or an anchor link.', 'floating-awesome-button' ),
            'info'       => sprintf(
                __( 'Use %1$s to target page or %2$s for anchor link', 'floating-awesome-button' ),
                '<code>https://</code>',
                '<code>#</code>'
            ),
            'value'      => isset( $fab['link'] ) ? $fab['link'] : '',
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => 'IN',
                    'value'    => apply_filters( 'fab_types_group_link', array(), array() ),
                ),
            ),
        );

        // Add link behavior field.
        $tabs['setting']['fields']['link_behavior'] = array(
            'type'       => 'switch',
            'name'       => 'fab_setting_link_behavior',
            'label'      => __( 'Link Behavior', 'floating-awesome-button' ),
            'tooltip'    => __( 'Configure the behavior of the link.', 'floating-awesome-button' ),
            'value'      => isset( $fab['linkBehavior'] ) ? $fab['linkBehavior'] : false,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => 'IN',
                    'value'    => array(
                        \Fab\Controller\Link_Type::get_instance()->type,
                        \Fab\Controller\Single_Post_Type::get_instance()->type,
                    ),
                ),
            ),
        );

        return $tabs;
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add fab metabox setting tabs fields.
        add_filter( 'fab_metabox_setting_tabs', array( __CLASS__, 'jit_add_fab_metabox_setting_tabs_fields' ), 10, 2 );
    }
}
