<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

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

    /**
     * Add link data to FAB item
     *
     * @frontend
     *
     * @param object $instance FAB item instance.
     * @return void
     */
    public function add_fab_item_data( $instance ) {
        if ( $instance->getType() !== $this->type ) {
            return;
        }

        // Set is button.
        $instance->setIsButton( false );

        // Set is standalone.
        $instance->setStandalone( 'yes' );

        // Set extra options.
        $target_element = get_post_meta( $instance->getId(), $this->post_meta['target_element']['meta_key'], true );
        $design         = get_post_meta( $instance->getId(), $this->post_meta['design']['meta_key'], true );
        $instance->setExtraOptions(
            array(
                'readingbar' => array(
                    'target_element' => $target_element ? $target_element : $this->post_meta['target_element']['default'],
                    'design'         => $design ? $design : $this->post_meta['design']['default'],
                ),
            )
        );
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add fab item data.
        add_action( 'fab_item_data', array( __CLASS__, 'jit_add_fab_item_data' ), 10, 1 );
    }
}
