<?php namespace GermanAirlinesVa\Graphql;

use System\Classes\PluginBase;

use App;
use Config;
use Illuminate\Foundation\AliasLoader;
use GermanAirlinesVa\Graphql\Classes\GraphqlServiceProvider;

class Plugin extends PluginBase
{
  public function registerComponents()
  {
  }

  public function registerSettings()
  {
    return [
      'settings' => [
        'label' => 'germanairlinesva.graphql::lang.settings.label',
        'description' => 'germanairlinesva.graphql::lang.settings.description',
        'category' => 'system::lang.system.categories.cms',
        'icon' => 'icon-compress',
        'class' => 'GermanAirlinesVa\Graphql\Models\Settings',
        'order' => 1000,
        'keywords' => 'graphql',
        'permissions' => ['germanairlinesva.graphql.schemas'],
      ],
    ];
  }

  public function boot()
  {
    App::make('October\Rain\Support\ClassLoader')->addDirectories('graphql');
    $this->bootPackages();
    App::register(GraphqlServiceProvider::class);
  }

  public function bootPackages()
  {
    // Get the namespace of the current plugin to use in accessing the Config of the plugin
    $pluginNamespace = str_replace('\\', '.', strtolower(__NAMESPACE__));

    // Instantiate the AliasLoader for any aliases that will be loaded
    $aliasLoader = AliasLoader::getInstance();

    // Get the packages to boot
    $packages = Config::get($pluginNamespace . '::packages');

    // Boot each package
    foreach ($packages as $name => $options) {
      // Setup the configuration for the package, pulling from this plugin's config
      if (!empty($options['config']) && !empty($options['config_namespace'])) {
        Config::set($options['config_namespace'], $options['config']);
      }

      // Register any Service Providers for the package
      if (!empty($options['providers'])) {
        foreach ($options['providers'] as $provider) {
          App::register($provider);
        }
      }

      // Register any Aliases for the package
      if (!empty($options['aliases'])) {
        foreach ($options['aliases'] as $alias => $path) {
          $aliasLoader->alias($alias, $path);
        }
      }
    }

    return $packages;
  }
}
