<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Anchor Link Type Editor.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Anchor_Link_Editor extends Anchor_Link_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 link, return.
        if ( $this->type !== $type ) {
            return;
        }

        // Save scroll duration.
        $post_meta       = $this->post_metas['scroll_duration'];
        $scroll_duration = isset( $params['fab_setting_scroll_duration'] ) ? intval( $params['fab_setting_scroll_duration'] ) : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $scroll_duration );
    }

    /**
     * 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 ) {
        // Add scroll duration field.
        $tabs['setting']['fields']['scroll_duration'] = array(
            'type'        => 'text',
            'name'        => 'fab_setting_scroll_duration',
            'label'       => __( 'Scroll Duration', 'floating-awesome-button' ),
            'tooltip'     => __( 'Specify the duration for the scroll', 'floating-awesome-button' ),
            'info'        => __( 'Window scroll duration in miliseconds', 'floating-awesome-button' ),
            'value'       => isset( $fab['extraOptions']['scroll_duration'] ) ? $fab['extraOptions']['scroll_duration'] : 1000,
            'required'    => true,
            'place_after' => 'link',
            '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() {
        // Save Metabox Data.
        add_action( 'fab_save_post', array( __CLASS__, 'jit_save_metabox_data' ), 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 );
    }
}
