<?php

namespace Fab\Controller;

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

use Fab\Metabox\FABMetaboxSetting;
use Fab\Interfaces\JIT_Interface;
use Fab\Feature\Animation as AnimationFeature;

/**
 * Scroll To Top Editor.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Scroll_To_Top_Editor extends Scroll_To_Top_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 post recommendation, return.
        if ( $this->type !== $type ) {
            return;
        }

        // Save post scroll to top offset.
        $post_meta = $this->post_meta['offset'];
        $offset    = isset( $params['fab_setting_scroll_to_top_offset'] ) ? intval( $params['fab_setting_scroll_to_top_offset'] ) : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $offset );

        // Save post scroll to top duration.
        $post_meta = $this->post_meta['duration'];
        $duration  = isset( $params['fab_setting_scroll_to_top_duration'] ) ? intval( $params['fab_setting_scroll_to_top_duration'] ) : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $duration );

        // Save post scroll to top animation in.
        $post_meta    = $this->post_meta['animation_in'];
        $animation_in = isset( $params['fab_setting_scroll_to_top_animation_in'] ) ? $params['fab_setting_scroll_to_top_animation_in'] : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $animation_in );

        // Save post scroll to top animation out.
        $post_meta     = $this->post_meta['animation_out'];
        $animation_out = isset( $params['fab_setting_scroll_to_top_animation_out'] ) ? $params['fab_setting_scroll_to_top_animation_out'] : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $animation_out );

        // Save post scroll to top animation duration.
        $post_meta          = $this->post_meta['animation_duration'];
        $animation_duration = isset( $params['fab_setting_scroll_to_top_animation_duration'] ) ? intval( $params['fab_setting_scroll_to_top_animation_duration'] ) : $post_meta['default'];
        update_post_meta( $post_id, $post_meta['meta_key'], $animation_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 ) {
        // Set offset field.
        $tabs['setting']['fields']['scroll_to_top_offset'] = array(
            'type'       => 'number',
            'name'       => 'fab_setting_scroll_to_top_offset',
            'label'      => __( 'Offset', 'floating-awesome-button' ),
            'tooltip'    => __( 'Number of pixels to be scrolled before the button appears', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['scroll_to_top']['offset'] ) ?
                $fab['extraOptions']['scroll_to_top']['offset'] : $this->post_meta['offset']['default'],
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set duration field.
        $tabs['setting']['fields']['scroll_to_top_duration'] = array(
            'type'       => 'number',
            'name'       => 'fab_setting_scroll_to_top_duration',
            'label'      => __( 'Duration', 'floating-awesome-button' ),
            'tooltip'    => __( 'Duration of the window scroll-to-top effect in milliseconds.', 'floating-awesome-button' ),
            'info'       => __( 'Window scroll duration in miliseconds', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['scroll_to_top']['duration'] ) ?
                $fab['extraOptions']['scroll_to_top']['duration'] : $this->post_meta['duration']['default'],
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set animation heading.
        $tabs['setting']['fields']['scroll_to_top_animation_heading'] = array(
            'type'       => 'heading',
            'label'      => __( 'Animation', 'floating-awesome-button' ),
            'info'       => sprintf(
                /* translators: %s is a link to the Animate.css website. */
                __( 'To see animation reference, you can go to <code><a href="%s" target="_blank" rel="noopener noreferrer">Animate.css</a></code>', 'floating-awesome-button' ),
                esc_url( 'https://animate.style/' )
            ),
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set animation in field.
        $tabs['setting']['fields']['scroll_to_top_animation_in'] = array(
            'type'       => 'select',
            'name'       => 'fab_setting_scroll_to_top_animation_in',
            'label'      => __( 'In', 'floating-awesome-button' ),
            'tooltip'    => __( 'Animation effect when the scroll-to-top icon appears.', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['scroll_to_top']['animation_in'] ) ?
                $fab['extraOptions']['scroll_to_top']['animation_in'] : $this->post_meta['animation_in']['default'],
            'inputClass' => 'field_option_animation_element select2',
            'options'    => AnimationFeature::$animateCSSAnimation,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set animation out field.
        $tabs['setting']['fields']['scroll_to_top_animation_out'] = array(
            'type'       => 'select',
            'name'       => 'fab_setting_scroll_to_top_animation_out',
            'label'      => __( 'Out', 'floating-awesome-button' ),
            'tooltip'    => __( 'Animation effect when the scroll-to-top icon disappears.', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['scroll_to_top']['animation_out'] ) ?
                $fab['extraOptions']['scroll_to_top']['animation_out'] : $this->post_meta['animation_out']['default'],
            'inputClass' => 'field_option_animation_element select2',
            'options'    => AnimationFeature::$animateCSSAnimation,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set animation duration field.
        $tabs['setting']['fields']['scroll_to_top_animation_duration'] = array(
            'type'       => 'number',
            'name'       => 'fab_setting_scroll_to_top_animation_duration',
            'label'      => __( 'Duration', 'floating-awesome-button' ),
            'info'       => __( 'Animation duration in milliseconds', 'floating-awesome-button' ),
            'tooltip'    => __( 'Set how long the animation should take when the scroll-to-top icon appears.', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['scroll_to_top']['animation_duration'] ) ?
                $fab['extraOptions']['scroll_to_top']['animation_duration'] : $this->post_meta['animation_duration']['default'],
            '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() {
        // Save Metabox Data.
        add_action( 'fab_save_post', array( __CLASS__, 'jit_save_metabox_data' ), 10, 3 );

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