<?php

namespace Fab\Controller;

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

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

/**
 * Single Post.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Single_Post_Type_Editor extends Single_Post_Type implements JIT_Interface {

    /**
     * Save Metabox Data.
     *
     * @param int    $post_id Post ID.
     * @param object $post Post object.
     * @param bool   $update Whether the post is being updated.
     * @return void
     */
    public function save_metabox_data( $post_id, $post, $update ) {
        $params = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
        $type   = isset( $params['fab_setting_type'] ) ? sanitize_text_field( wp_unslash( $params['fab_setting_type'] ) ) : '';

        // If type is not single post, return.
        if ( $this->type !== $type ) {
            return;
        }

        // Save link behavior.
        $link_behavior = \Fab\Plugin\Helper::getInstance()->transform_yes_no_value( $params['fab_setting_link_behavior'] );
        update_post_meta( $post_id, 'fab_setting_link_behavior', $link_behavior );
    }

    /**
     * Add fab setting types.
     *
     * @editor
     *
     * @param array $types Types.
     * @return array
     */
    public function add_fab_setting_types( $types ) {
        // Initiate group type.
        $types = $this->add_group_type( $types );

        // Get 3 registered post types.
        $post_types = get_post_types( array( 'public' => true ), 'object' );
        $post_types = array_filter(
            $post_types,
            function ( $post_type ) {
                return 'attachment' !== $post_type->name;
            }
        );
        $post_types = array_slice( $post_types, 0, 3 );

        // Add single post type to types array.
        $types[ $this->key ]['children'][] = array(
            'id'   => $this->type,
            // translators: %s will be replaced by a list of post types.
            'text' => sprintf( __( 'Single Post (%s, etc)', 'floating-awesome-button' ), implode( ', ', array_keys( $post_types ) ) ),
        );

        return $types;
    }

    /**
     * Add single post data input to be processed.
     *
     * @editor
     *
     * @param array $input Input.
     * @param array $data  Data.
     * @return array
     */
    public function add_fab_setting_input( $input, $data ) {
        if ( ! isset( $data['fab_setting_type'] ) || $data['fab_setting_type'] !== $this->type ) {
            return $input;
        }

        // Add post metas to input.
        foreach ( $this->post_metas as $meta ) {
            $input[ $meta['meta_key'] ] = array(
                'default' => '',
            );
        }

        return $input;
    }

    /**
     * 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 post id field.
        $tabs['setting']['fields']['post_id'] = array(
            'type'        => 'select',
            'name'        => 'fab_setting_post_id',
            'label'       => __( 'Single Post', 'floating-awesome-button' ),
            'info'        => __( 'Select a single post to link to.', 'floating-awesome-button' ),
            'tooltip'     => __( 'Select a single post to link to.', 'floating-awesome-button' ),
            'placeholder' => __( 'Search posts...', 'floating-awesome-button' ),
            'value'       => isset( $fab['post']->ID ) ? $fab['post'] : '',
            'required'    => true,
            'visible_if'  => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
            'select2'     => array(
                'allowClear'         => false,
                'minimumInputLength' => 3,
                'ajax'               => array(
                    'url'      => rest_url( '/fab/v1/search-posts' ),
                    'dataType' => 'json',
                    'delay'    => 250,
                    'cache'    => true,
                ),
            ),
        );

        return $tabs;
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Save Metabox Data.
        add_action( 'fab_save_post', array( __CLASS__, 'jit_save_metabox_data' ), 10, 3 );

        // Add fab setting types.
        add_filter( 'fab_setting_types', array( __CLASS__, 'jit_add_fab_setting_types' ), 10, 1 );

        // Add single post data input to be processed.
        add_action( 'fab_setting_input', array( __CLASS__, 'jit_add_fab_setting_input' ), 10, 2 );

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