<?php
if (!defined('ABSPATH')) {
    exit;
}

class BMTQB_Plugin {
    private static $instance = null;
    private $industries = [];
    private $submission_controller;
    private $submission_post_type;
    private $admin_details;
    private $integration_manager;
    private $license_manager;
    private $update_manager;

    public static function instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    private function __construct() {
        $this->integration_manager = new BMTQB_Integration_Manager();
        $this->submission_controller = new BMTQB_Submission_Controller($this->integration_manager);
        $this->submission_post_type = new BMTQB_Submission_Post_Type();
        $this->admin_details = new BMTQB_Submission_Details($this->integration_manager);
        $this->license_manager = new BMTQB_License_Manager();
        $this->update_manager = new BMTQB_Update_Manager($this->license_manager);
    }

    public function register_industry(BMTQB_Industry_Interface $industry) {
        $key = $industry->key();
        $this->industries[$key] = $industry;

        add_action('wp_enqueue_scripts', function () use ($industry) {
            $this->enqueue_industry_assets($industry);
        });

        add_shortcode($industry->shortcode(), [$industry, 'render_form']);

        $this->submission_controller->register($industry);
        $this->submission_post_type->register($industry);
        $this->admin_details->register($industry);
        $this->license_manager->register_industry($industry);
        $this->update_manager->register_industry($industry);

        do_action('bmtqb_industry_registered', $industry);
    }

    public function industries() {
        return $this->industries;
    }

    public function industry($key) {
        return $this->industries[$key] ?? null;
    }

    public function integrations() {
        return $this->integration_manager;
    }

    public function updates() {
        return $this->update_manager;
    }

    public function licenses() {
        return $this->license_manager;
    }

    private function enqueue_industry_assets(BMTQB_Industry_Interface $industry) {
        $style_path = $industry->plugin_path() . 'assets/css/style.css';
        $script_path = $industry->plugin_path() . 'assets/js/app.js';

        if (file_exists($style_path)) {
            wp_enqueue_style(
                $industry->style_handle(),
                $industry->plugin_url() . 'assets/css/style.css',
                [],
                $industry->version()
            );
        }

        if (file_exists($script_path)) {
            wp_enqueue_script(
                $industry->script_handle(),
                $industry->plugin_url() . 'assets/js/app.js',
                ['jquery'],
                $industry->version(),
                true
            );

            $settings = BMTQB_Settings_Repository::all($industry);
            wp_localize_script($industry->script_handle(), $industry->script_object(), [
                'ajax_url' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce($industry->nonce_action()),
                'settings' => $industry->public_settings($settings),
            ]);
        }
    }
}
