<?php

namespace Fab\Controller;

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

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

/**
 * FAB Module Link.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Anchor_Link_Type extends Link_Group implements JIT_Interface {

    /**
     * Post Metas.
     *
     * @var array
     */
    public $post_metas = array(
        'scroll_duration' => array(
            'meta_key' => 'fab_setting_scroll_duration',
            'default'  => 1000,
        ),
    );

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

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

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

        // Set Link.
        $link = get_post_meta( $instance->getID(), FABMetaboxSetting::$post_metas['link']['meta_key'], true );
        $link = ( $link && is_string( $link ) ) ? $link : '';
        $instance->setLink( $link );

        // Set Scroll Duration as Extra Option.
        $post_meta                        = $this->post_metas['scroll_duration'];
        $extra_options                    = $instance->getExtraOptions();
        $extra_options['scroll_duration'] = get_post_meta( $instance->getID(), $post_meta['meta_key'], true );
        $extra_options['scroll_duration'] = ( $extra_options['scroll_duration'] && is_numeric( $extra_options['scroll_duration'] ) ) ? $extra_options['scroll_duration'] : $post_meta['default'];
        $instance->setExtraOptions( $extra_options );
    }

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

        $postmeta['fab_setting_link'] = isset( $data->settings->link ) ? $data->settings->link : '';

        return $postmeta;
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add group type.
        add_filter( 'fab_types_group_link', array( __CLASS__, 'jit_add_fab_types_group' ), 10, 1 );

        // Adjust item data.
        add_action( 'fab_item_data', array( __CLASS__, 'jit_add_fab_item_data' ), 10, 1 );

        // Add fab setting types.
        add_filter( 'fab_setting_types', array( __CLASS__, 'jit_add_fab_setting_types' ), 10, 1 );

        // Filter template postmeta.
        add_filter( 'fab_template_postmeta', array( __CLASS__, 'jit_filter_template_postmeta' ), 10, 3 );
    }
}
