<?php

namespace Booktics\Assets;

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Manage all admin scripts and styles
 */
class Admin_Assets extends Base_Assets {
    /**
     * Register single service
     *
     * @return  void
     */
    public function register() {
        add_action( 'admin_enqueue_scripts', [
            $this,
            'register_styles_scripts'
        ] );
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] );
        add_action( 'admin_footer', [ $this, 'admin_helpscout_beacon' ] );
    }

    /**
     * Enqueue scripts and styles
     *
     * @return  void
     */
    public function enqueue() {
        $screen = get_current_screen();
        if ( $screen->base !== 'toplevel_page_booktics') {
            return;
        }
        $this->enqueue_i18n_loader();
        wp_enqueue_style( 'wp-color-picker' );
        wp_enqueue_style( 'booktics-admin-style' );
        wp_enqueue_style( 'booktics-vendor' );

        wp_enqueue_media();
        wp_enqueue_script( 'booktics-flatpickr-scripts' );
        wp_enqueue_script( 'booktics-packages' );
        wp_enqueue_script( 'booktics-dashboard-scripts' );

        wp_localize_script( 'booktics-packages', 'booktics', Localize::get_admin() );
        wp_localize_script( 'booktics-packages', 'booktics_enabled_modules', booktics_get_enabled_modules() );

        if ( function_exists( 'wp_set_script_translations' ) ) {
            wp_set_script_translations( 'booktics-dashboard-scripts', 'booktics' );
        }

        wp_localize_script(
            'booktics-dashboard-scripts',
            'bookticsData',
            [
                'publicPath' => plugins_url( '../../build/', __FILE__ ),
            ]
        );
    }

    /**
     * Get all scripts
     *
     * @return  array List register scripts
     */
    public function get_scripts(): array {
        $scripts = [
            'booktics-dashboard-scripts' => [
                'src'       => booktics()->assets_url . '/build/js/dashboard.js',
                'deps'      => [ 'booktics-packages' ],
                'in_footer' => true,
            ],
            'booktics-flatpickr-scripts' => [
                'src'       => booktics()->assets_url . '/js/vendors/flatpickr.js',
                'deps'      => [],
                'in_footer' => true,
            ],
            'booktics-packages'          => [
                'src'       => booktics()->assets_url . '/build/js/packages.js',
                'deps'      => [
                    'wp-api-fetch',
                    'wp-components',
                    'wp-compose',
                    'wp-i18n',
                    'wp-element',
                    'wp-data'
                ],
                'in_footer' => true,
            ],
        ];

        return apply_filters( 'booktics_admin_assets', $scripts );
    }

    /**
     * List of register styles
     *
     * @return  array
     */
    public function get_styles(): array {
        $styles = [
            'booktics-admin-style' => [
                'src' => booktics()->assets_url . '/build/css/admin.css',
            ],
            'booktics-vendor'      => [
                'src' => booktics()->assets_url . '/build/css/vendor.css',
            ],
        ];

        return apply_filters( 'booktics_admin_styles', $styles );
    }

    /**
     * Add HelpScout script to admin footer
     *
     * @return  void
     */
    public function admin_helpscout_beacon() {
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        $is_booktics_screen = false;

        if ( $screen ) {
            $screen_id = isset( $screen->id ) ? $screen->id : '';

            // Check: only load on Booktics top-level admin page
            if ( 'toplevel_page_booktics' === $screen_id ) {
                $is_booktics_screen = true;
            }
        }

        // Allow overriding detection via filter.
        $is_booktics_screen = apply_filters( 'booktics_is_admin_screen', $is_booktics_screen, $screen );

        if ( ! $is_booktics_screen ) {
            return;
        }
        ?>
<script type="text/javascript">
! function(e, t, n) {
    function a() {
        var e = t.getElementsByTagName("script")[0],
            n = t.createElement("script");
        n.type = "text/javascript", n.async = !0, n.src = "https://beacon-v2.helpscout.net", e.parentNode.insertBefore(
            n, e)
    }
    if (e.Beacon = n = function(t, n, a) {
            e.Beacon.readyQueue.push({
                method: t,
                options: n,
                data: a
            })
        }, n.readyQueue = [], "complete" === t.readyState) a();
    else if (e.attachEvent) e.attachEvent("onload", a);
    else e.addEventListener("load", a, !1)
}(window, document, window.Beacon || function() {});
</script>
<script type="text/javascript">
window.Beacon('config', {
    color: "#22B855",
});
window.Beacon('init', '0f1a82db-0aa9-4bac-accc-ee1bb81d4940');
window.Beacon('on', 'ready', function() {
    window.Beacon('close');
});
</script>
<?php
    }
}