<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;
use Fab\View;
use Fab\Helper\FABItem;

/**
 * Dashboard
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Dashboard extends Base implements JIT_Interface {

    /**
     * Register submenu page
     *
     * @return void
     */
    public function register_submenu_page() {
        add_submenu_page(
            'edit.php?post_type=fab',
            __( 'FAB Dashboard', 'floating-awesome-button' ),
            __( 'Dashboard', 'floating-awesome-button' ),
            'manage_options',
            'fab-dashboard',
            array( $this, 'page_dashboard' ),
            0
        );
    }

    /**
     * Render the dashboard page
     *
     * @return void
     */
    public function page_dashboard() {
        // Add custom thank you page.
        add_filter( 'admin_footer_text', array( \Fab\Controller\Custom_Footer::get_instance(), 'get_admin_footer_text' ) );

        // Maximum 12 templates.
        $max_templates = 12;

        // Get recommended templates.
        $recommended_templates = apply_filters( 'fab_dashboard_recommended_templates', array(), $max_templates );

        // Sort templates.
        sort( $recommended_templates );

        // Get only 12 templates.
        $recommended_templates = array_slice( $recommended_templates, 0, $max_templates );
        $recommended_templates = \Fab\Helper\FAB_Template::getInstance()->transform_metabox_templates( $recommended_templates );

        // Localize templates.
        \FAB()->Helper->wp_enqueue_script_component( 'fab-dashboard-component', 'assets/components/dashboard/main.js', array(), FAB_VERSION, false );
        wp_localize_script(
            'fab-dashboard-component',
            'FAB_INSIGHTS',
            apply_filters(
                'fab_insights_dashboard_data',
                array(
                    'templates'      => $recommended_templates,
                    'loading_gif'    => FAB_PLUGIN_URL . 'assets/img/loading.gif',
                    'rest_api'       => array(
                        'insights' => get_rest_url( null, 'fab/v1/insights' ),
                    ),
                    'nonce'          => wp_create_nonce( 'wp_rest' ),
                    'colors'         => array(
                        'primary' => '#4f46e5',
                        'status'  => array(
                            'publish'   => '#4f46e5',
                            'draft'     => '#95a5a6',
                            'trash'     => '#e74c3c',
                            'pending'   => '#f1c40f',
                            'private'   => '#34495e',
                            'scheduled' => '#1abc9c',
                        ),
                    ),
                    'urls'           => array(
                        'fab'             => admin_url( 'edit.php?post_type=fab' ),
                        'posts'           => admin_url( 'edit.php?post_type=post' ),
                        'templates'       => admin_url( 'edit.php?post_type=fab&page=floating-awesome-button-templates' ),
                        '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' ),
                    ),
                    'documentations' => array(
                        'daily_clicks'   => \FAB()->Helper->get_documentation( 12451 ),
                        'monthly_clicks' => \FAB()->Helper->get_documentation( 12447 ),
                    ),
                    'labels'         => array_merge(
                        \Fab\Helper\Metabox::getInstance()->get_metabox_labels(),
                        array(
                            // Main insights.
                            'number_of_buttons' => __( 'Number of Buttons', 'floating-awesome-button' ),
                            'total_clicks'      => __( 'Total Clicks', 'floating-awesome-button' ),
                            'average_clicks'    => __( 'Average Clicks', 'floating-awesome-button' ),
                            'type'              => __( 'Types', 'floating-awesome-button' ),
                            'daily_clicks'      => __( 'Daily Clicks', 'floating-awesome-button' ),
                            'monthly_clicks'    => __( 'Monthly Clicks', 'floating-awesome-button' ),
                            'clicks'            => __( 'Clicks', 'floating-awesome-button' ),
                            'explore_more'      => __( 'Explore More', 'floating-awesome-button' ),
                            'templates'         => __( 'Popular Templates', 'floating-awesome-button' ),
                            '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' ) );

        // Render template.
        View::RenderStatic( 'Backend.dashboard' );
    }

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

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Register submenu page.
        add_action( 'admin_menu', array( __CLASS__, 'jit_register_submenu_page' ) );
    }
}
