<?php

namespace Fab\Controller;

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

use Fab\Interfaces\Model_Interface;
use Fab\View;
use Fab\Helper\FABItem;
use Fab\Feature\Design;
use Fab\Feature\Modal;
use Fab\Metabox\FABMetaboxSetting;
use Fab\Metabox\FABMetaboxTrigger;

/**
 * Metabox Analytics
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Metabox_Analytics extends Base implements Model_Interface {

    /**
     * Add metabox analytics
     *
     * @return void
     */
    public function metabox_analytics() {
        add_meta_box(
            'fab-metabox-analytics',
            __( 'Analytics', 'floating-awesome-button' ),
            array( $this, 'metabox_analytics_callback' ),
            'fab',
            'normal',
            'low'
        );
    }

    /**
     * Metabox analytics callback
     *
     * @return void
     */
    public function metabox_analytics_callback() {
        global $post;

        // Enqueue Localize Script.
        \FAB()->Helper->wp_enqueue_script_component( 'metabox-analytics-component', 'assets/components/metabox-analytics/main.js', array(), FAB_VERSION, true );
        wp_localize_script(
            'metabox-analytics-component',
            'FAB_INSIGHTS',
            array(
                'post'        => $post,
                'loading_gif' => FAB_PLUGIN_URL . 'assets/img/loading.gif',
                'rest_api'    => array(
                    'analytics' => get_rest_url( null, 'fab/v1/analytics' ),
                ),
                'colors'      => array(
                    'primary' => '#4f46e5',
                    'status'  => array(
                        'publish'   => '#4f46e5',
                        'draft'     => '#95a5a6',
                        'trash'     => '#e74c3c',
                        'pending'   => '#f1c40f',
                        'private'   => '#34495e',
                        'scheduled' => '#1abc9c',
                    ),
                ),
                'nonce'       => wp_create_nonce( 'wp_rest' ),
                'urls'        => array(
                    'upgrade'         => admin_url( 'edit.php?post_type=fab&page=floating-awesome-button-setting-pricing' ),
                    'config_tracking' => admin_url( 'edit.php?post_type=fab&page=floating-awesome-button-setting#setting-feature_tracking' ),
                ),
                'labels'      => array_merge(
                    \Fab\Helper\Metabox::getInstance()->get_metabox_labels(),
                    array(
                        'loading'   => __( 'Loading...', 'floating-awesome-button' ),
                        'analytics' => array(
                            'daily_clicks' => array(
                                'label'          => __( 'Daily Clicks', 'floating-awesome-button' ),
                                'missing_clicks' => __( 'Missing Clicks', 'floating-awesome-button' ),
                                'no_data'        => __( 'No data available for daily clicks yet', 'floating-awesome-button' ),
                                'clicks'         => __( 'Clicks', 'floating-awesome-button' ),
                                'threshold'      => array(
                                    '0_10'       => array(
                                        'label' => __( '0 - 10', 'floating-awesome-button' ),
                                        'color' => 'rgba(79, 70, 229, .25)',
                                        'min'   => 0,
                                        'max'   => 10,
                                    ),
                                    '11_100'     => array(
                                        'label' => __( '11 - 100', 'floating-awesome-button' ),
                                        'color' => 'rgba(79, 70, 229, .50)',
                                        'min'   => 11,
                                        'max'   => 100,
                                    ),
                                    '101_1000'   => array(
                                        'label' => __( '101 - 1000', 'floating-awesome-button' ),
                                        'color' => 'rgba(79, 70, 229, .75)',
                                        'min'   => 101,
                                        'max'   => 1000,
                                    ),
                                    '1001_10000' => array(
                                        'label' => __( '1001 - 10000', 'floating-awesome-button' ),
                                        'color' => 'rgba(79, 70, 229, 1)',
                                        'min'   => 1001,
                                        'max'   => 10000,
                                    ),
                                ),
                            ),
                        ),
                        'no_data'   => __( 'No data', 'floating-awesome-button' ),
                    ),
                ),
            )
        );

        // Load vendors.
        \FAB()->Helper->wp_enqueue_script( 'fab-echarts', 'vendor/echarts/echarts.min.js', array(), FAB_VERSION, true );
        \FAB()->Helper->wp_enqueue_style( 'fontawesome', apply_filters( 'fab_fontawesome_css', 'vendor/fontawesome/css/all.min.css' ) );

        View::RenderStatic( 'Backend.Metabox.analytics' );
    }

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

    /**
     * Execute Class.
     *
     * @return void
     */
    public function run() {
        // @backend - Add Setting metabox to Fab CPT
        add_action( 'add_meta_boxes', array( $this, 'metabox_analytics' ), 10, 0 );
    }
}
