<?php

namespace Fab\Controller;

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

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

/**
 * Reading Bar.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Reading_Bar_Type_Template extends Reading_Bar_Type implements JIT_Interface {

    /**
     * Filter template postmeta.
     *
     * @template
     *
     * @param array  $postmeta Array of postmeta.
     * @param string $type Type of postmeta.
     * @param object $data Data of postmeta.
     */
    public function filter_template_postmeta( $postmeta, $type, $data ) {
        if ( $this->type !== $type ) {
            return $postmeta;
        }

        // Add reading bar settings.
        if ( isset( $data->settings->target_element ) ) {
            $postmeta[ $this->post_meta['target_element']['meta_key'] ] = $data->settings->target_element;
        }

        // Add reading bar design settings.
        if ( isset( $data->design ) ) {
            $postmeta[ $this->post_meta['design']['meta_key'] ] = array(
                'background' => array(
                    'color' => $data->design->background_color,
                ),
                'foreground' => array(
                    'color' => $data->design->foreground_color,
                ),
                'height'     => $data->design->height,
                'transition' => $data->design->transition,
            );
        }

        return $postmeta;
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Filter template postmeta.
        add_filter( 'fab_template_postmeta', array( __CLASS__, 'jit_filter_template_postmeta' ), 10, 3 );
    }
}
