<?php
namespace SCC\Admin;
class Kernel
{
    /**
     * Settings page slug
     */
    private $settings_page_hook = 'stylish-cost-calculator_page_stylish_cost_calculator_settings';
    /**
     * Hold the class instance.
     */
    private static $instance = null;
    /**
     * The constructor
     */
    private function __construct()
    {
        $this->includes();
        add_action('admin_enqueue_scripts', [ $this, 'enqueue_scripts' ]);
        add_action('wp_ajax_scc_admin_settings_ajax', [ $this, 'settings_ajax' ]);
    }
    /**
     * The object is created from within the class itself
     * only if the class has no instance.
     */
    public static function init()
    {
        if (self::$instance == null ) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    /**
     * Require admin specific files
     */
    private function includes()
    {
        include_once dirname(__FILE__) . '/existing-calculators-list.php';
        include_once dirname(__FILE__) . '/scc-diagnostic.php';
        include_once dirname(__FILE__) . '/scc-settings-help-menu.php';
        include_once dirname(__FILE__) . '/stripe-integration.php';
        include_once dirname(__FILE__) . '/coupon.php';
        include_once dirname(__FILE__) . '/quote-list-view.php';
        include dirname(__FILE__) . '/tabs-init.php';
    }
    /**
     * Admin load css and js
     */
    public function enqueue_scripts( $hook )
    {
        // If page is settings page
        if ($hook === $this->settings_page_hook ) {
            wp_enqueue_media();
            $version = filemtime(SCC_DIR . '/assets/js/scc-admin-settings.js');
            wp_enqueue_script('scc-admin-settings-js', SCC_URL . 'assets/js/scc-admin-settings.js', [ 'jquery' ], $version, true);
            wp_localize_script(
                'scc-admin-settings-js', 'SCC_Settings', [
                    'security' => wp_create_nonce("scostc-admin-settings-referer")
                ]
            );
            $version = filemtime(SCC_DIR . '/styles/scc-admin-settings.css');
            wp_enqueue_style('scc-admin-settings-css', SCC_URL . 'styles/scc-admin-settings.css', [], $version);
            $translation_array = 'var rt_vars = ' . json_encode(array( 'rt_adminurl' => admin_url(), 'rt_url' => get_site_url(), 'rt_urlajax' =>  (get_site_url() . '/wp-admin/admin-ajax.php') , 'a_value' => '10' ));
            wp_add_inline_script( 'scc-js-js', $translation_array );
        }
        // if (isset($_GET["page"]) == "scc-tabs" || isset($_GET["page"]) == "Stylish_Cost_Calculator_EditItems" || isset($_GET["page"]) == "edit_sccbyid" || isset($_GET["page"]) == "stylish_cost_calculator_Diagnostic" || isset($_GET["page"]) == "stylish_cost_calculator_help" || isset($_GET["page"]) == "stylish_cost_calculator_settings"  || isset($_GET["page"]) == "Stylish_Cost_Calculator_Coupon" ) {
        //     wp_enqueue_style('gf-admin-style', SCC_URL. 'styles/scc-back-end.css', array(), '3.1.7');
        // }
    }
    /**
     * Manage admin ajax functionality
     */
    public function settings_ajax()
    {
        check_ajax_referer('scostc-admin-settings-referer', 'security');
        if (! current_user_can('manage_options') ) {
            wp_send_json_error();
        }
        include_once __DIR__ . '/Ajax/Settings.php';
        $ajax = new Ajax\Settings();
        $method = isset($_REQUEST['method']) ? $_REQUEST['method'] : false;
        if (! method_exists($ajax, $method) ) {
            wp_send_json_error();
        }
        $data = isset($_REQUEST['data']) ? $_REQUEST['data'] : [];
        call_user_func([ $ajax, $method ], $data);
    }
}
