<?php

namespace LoftyIDX\includes;

use LoftyIDX\includes\providers\LoftyIDXNoticeServiceProvider;
use LoftyIDX\includes\providers\LoftyIDXProxyServiceProvider;
use LoftyIDX\includes\providers\LoftyIDXSettingsServiceProvider;
use LoftyIDX\includes\providers\LoftyIDXTemplateServiceProvider;
use LoftyIDX\includes\providers\LoftyIDXShortcodesProvider;

defined('ABSPATH') || exit;

class LoftyIDX
{
  public $option = [];
  private static $instance = null;
  public $config = [];
  public $services = [];

  protected function __construct()
  {
    $this->config = require_once(LOFTY_IDX_PATH . 'includes/config.php');
    $this->init_hooks();
  }

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

  public function register()
  {
    $providers = [
      'settings' => LoftyIDXSettingsServiceProvider::class, // Admin Dashboard Settings
      'proxy' => LoftyIDXProxyServiceProvider::class, // Rest Http forward
      'notice' => LoftyIDXNoticeServiceProvider::class, // Admin Dashboard Notice
      'template' => LoftyIDXTemplateServiceProvider::class, // Template Loader
      'shortcodes' => LoftyIDXShortcodesProvider::class,  // shortcodes provider
    ];

    /** @var Provider $provider */
    foreach ($providers as $key => $provider) {
      $service = (new $provider($this));
      $service->register();
      $this->services[$key] = $service;
    }

    include_once plugin_dir_path(__FILE__) . 'LoftyIDXWidgetQuickSearch.php';

  }

  private function init_hooks()
  {
    register_activation_hook(LOFTY_IDX_FILE, array(LoftyIDXInstall::class, 'install'));
    register_deactivation_hook(LOFTY_IDX_FILE, array(LoftyIDXInstall::class, 'uninstall'));
  }
}
