<?php

namespace Fab\Controller;

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

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

/**
 * Reading Bar Type Editor.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Reading_Bar_Type_Editor extends Reading_Bar_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 reading bar, return.
        if ( $this->type !== $type ) {
            return;
        }

        // Save reading bar settings.
        $reading_bar_target_element = isset( $params['fab_setting_reading_bar_target_element'] ) ? sanitize_text_field( wp_unslash( $params['fab_setting_reading_bar_target_element'] ) ) : '';
        update_post_meta( $post_id, 'fab_setting_reading_bar_target_element', $reading_bar_target_element );

        // Save reading bar design settings.
        $reading_bar_design = isset( $params['fab_setting_reading_bar_design'] ) ? wp_unslash( $params['fab_setting_reading_bar_design'] ) : array();
        update_post_meta( $post_id, 'fab_setting_reading_bar_design', $reading_bar_design );
    }

    /**
     * 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 reading bar target element field.
        $tabs['setting']['fields']['reading_bar_target_element'] = array(
            'type'       => 'text',
            'name'       => 'fab_setting_reading_bar_target_element',
            'label'      => __( 'Target Element', 'floating-awesome-button' ),
            'info'       => __( 'Target element class/id to stick the bar', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['reading_bar']['target_element'] ) ?
                $fab['extraOptions']['reading_bar']['target_element'] : $this->post_meta['target_element']['default'],
            'required'   => true,
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set reading bar design heading.
        $tabs['setting']['fields']['reading_bar_design_heading'] = array(
            'type'       => 'heading',
            'label'      => __( 'Design', 'floating-awesome-button' ),
            'info'       => __( 'Configure the reading bar design', 'floating-awesome-button' ),
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set reading bar background color field.
        $tabs['setting']['fields']['reading_bar_background_color'] = array(
            'type'       => 'colorpicker',
            'name'       => 'fab_setting_reading_bar_design[background][color]',
            'label'      => __( 'Background Color', 'floating-awesome-button' ),
            'info'       => __( 'Choose the background color for your reading bar', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['reading_bar']['design']['background']['color'] ) ?
                $fab['extraOptions']['reading_bar']['design']['background']['color'] :
                $this->post_meta['design']['default']['background']['color'],
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set reading bar foreground color field.
        $tabs['setting']['fields']['reading_bar_foreground_color'] = array(
            'type'       => 'colorpicker',
            'name'       => 'fab_setting_reading_bar_design[foreground][color]',
            'label'      => __( 'Foreground Color', 'floating-awesome-button' ),
            'info'       => __( 'Choose the foreground color for your reading bar', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['reading_bar']['design']['foreground']['color'] ) ?
                $fab['extraOptions']['reading_bar']['design']['foreground']['color'] :
                $this->post_meta['design']['default']['foreground']['color'],
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set reading bar height field.
        $tabs['setting']['fields']['reading_bar_height'] = array(
            'type'       => 'text',
            'name'       => 'fab_setting_reading_bar_design[height]',
            'label'      => __( 'Height', 'floating-awesome-button' ),
            'info'       => __( 'Use any sizing values px, rem, em, vh, vw, etc. Default is set to .25rem', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['reading_bar']['design']['height'] ) ?
                $fab['extraOptions']['reading_bar']['design']['height'] :
                $this->post_meta['design']['default']['height'],
            'visible_if' => array(
                array(
                    'tab'      => 'setting',
                    'field'    => 'type',
                    'operator' => '==',
                    'value'    => $this->type,
                ),
            ),
        );

        // Set reading bar transition field.
        $tabs['setting']['fields']['reading_bar_transition'] = array(
            'type'       => 'text',
            'name'       => 'fab_setting_reading_bar_design[transition]',
            'label'      => __( 'Transition', 'floating-awesome-button' ),
            'info'       => __( 'Default is set to 0.2s', 'floating-awesome-button' ),
            'value'      => isset( $fab['extraOptions']['reading_bar']['design']['transition'] ) ?
                $fab['extraOptions']['reading_bar']['design']['transition'] :
                $this->post_meta['design']['default']['transition'],
            '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 for reading bar.
        add_filter( 'fab_metabox_setting_tabs', array( __CLASS__, 'jit_add_fab_metabox_setting_tabs_fields' ), 10, 2 );
    }
}
