<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Scroll To Top Type Frontend
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Scroll_To_Top_Type_Frontend extends Scroll_To_Top_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 extra options.
        $offset             = get_post_meta( $instance->getId(), $this->post_meta['offset']['meta_key'], true );
        $duration           = get_post_meta( $instance->getId(), $this->post_meta['duration']['meta_key'], true );
        $animation_in       = get_post_meta( $instance->getId(), $this->post_meta['animation_in']['meta_key'], true );
        $animation_out      = get_post_meta( $instance->getId(), $this->post_meta['animation_out']['meta_key'], true );
        $animation_duration = get_post_meta( $instance->getId(), $this->post_meta['animation_duration']['meta_key'], true );
        $instance->setExtraOptions(
            array(
                'scroll_to_top' => array(
                    'offset'             => $offset ? $offset : $this->post_meta['offset']['default'],
                    'duration'           => $duration ? $duration : $this->post_meta['duration']['default'],
                    'animation_in'       => $animation_in ? $animation_in : $this->post_meta['animation_in']['default'],
                    'animation_out'      => $animation_out ? $animation_out : $this->post_meta['animation_out']['default'],
                    'animation_duration' => $animation_duration ? $animation_duration : $this->post_meta['animation_duration']['default'],
                ),
            )
        );

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

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

    /*
    |--------------------------------------------------------------------------
    | 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 );
    }
}
