<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Reading Bar Type
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Reading_Bar_Type extends Bar_Button_Group implements JIT_Interface {

    /**
     * Post meta.
     *
     * @var array
     */
    protected $post_meta = array(
        'target_element' => array(
            'meta_key' => 'fab_setting_reading_bar_target_element',
            'default'  => 'body',
        ),
        'design'         => array(
            'meta_key' => 'fab_setting_reading_bar_design',
            'default'  => array(
                'height'     => '.25rem',
                'transition' => '.25s',
                'background' => array(
                    'color' => '#e5e7eb',
                ),
                'foreground' => array(
                    'color' => '#4f46e5',
                ),
            ),
        ),
    );

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

    /**
     * Get labels type.
     *
     * @return array
     */
    public function get_labels_type() {
        return array(
            'name' => __( 'Reading Bar', 'floating-awesome-button' ),
        );
    }

    /**
     * Add as recommended template.
     *
     * @param array $templates The templates.
     * @param int   $max_templates The max templates.
     * @return array
     */
    public function add_fab_dashboard_recommended_templates( $templates, $max_templates ) {
        // Check if max templates is reached.
        if ( count( $templates ) >= $max_templates ) {
            return $templates;
        }

        // Get insight type.
        $insight_type = \Fab\Controller\Insight_Type::get_instance()->get_transient();

        /**
         * Recommend type if the type has not been used.
         * The check is relying on the transient, so it might not be accurate.
         */
        if ( ! isset( $insight_type[ $this->type ] ) ) {
            $templates[] = 'reading-bar';
        }

        return $templates;
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add fab setting types.
        add_filter( 'fab_setting_types', array( __CLASS__, 'jit_add_fab_setting_types' ), 10, 1 );

        // Add as recommended template.
        add_filter( 'fab_dashboard_recommended_templates', array( __CLASS__, 'jit_add_fab_dashboard_recommended_templates' ), 10, 2 );
    }
}
