<?php
namespace Tiamr;

use Tiamr\Admin\Assets as Admin_Assets;
use Tiamr\Admin\Admin_Menu;
use Tiamr\Admin\Menu_Item_Fields;
use Tiamr\Frontend\Menu_Filter;
use Tiamr\Rules\Free_Rule_Evaluator;
use Tiamr\Rules\User_Context;

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


final class Plugin {
    /** @var Plugin|null */
    private static $instance = null;

    /** @var Free_Rule_Evaluator */
    private $free_rule_evaluator;

    /** @var User_Context */
    private $user_context;

    private function __construct() {
        $this->user_context        = new User_Context();
        $this->free_rule_evaluator = new Free_Rule_Evaluator($this->user_context);

        $this->register_hooks();
    }

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

        return self::$instance;
    }

    public function get_free_rule_evaluator(): Free_Rule_Evaluator {
        return $this->free_rule_evaluator;
    }

    public function get_user_context(): User_Context {
        return $this->user_context;
    }


    private function register_hooks(): void {
        new Menu_Filter($this->free_rule_evaluator, $this->user_context);

        if (is_admin()) {
            new Admin_Assets();
            new Admin_Menu();
            new Menu_Item_Fields();
        }

        /**
         * Fires after the Free plugin core has loaded.
         *
         * A future Pro plugin can use this hook to register Pro UI, condition
         * evaluators, PMPro integrations, licence checks, etc.
         */
        do_action('tiamr/loaded', $this);
    }
}
