<?php

/**
 * @file
 * Install and uninstall functions for the WxT installation profile.
 */

use Drupal\menu_link_content\Entity\MenuLinkContent;

/**
 * Implements hook_install().
 *
 * Perform actions to set up the site for this profile.
 *
 * @see system_install()
 */
function wxt_install() {
  _wxt_setup_themes();
  _wxt_setup_branding();
  _wxt_setup_menus();
  _wxt_setup_lang_prefixes();
  _wxt_setup_404();
}

/**
 * Set up the default branding.
 */
function _wxt_setup_branding() {
  // Set the path to the logo, favicon and README file based on install
  // directory.
  $wxt_path = \Drupal::service('extension.list.profile')->getPath('wxt');
  \Drupal::configFactory()
    ->getEditable('system.theme.global')
    ->set('logo', [
      'path' => $wxt_path . '/wxt.svg',
      'url' => '',
      'use_default' => FALSE,
    ])
    ->set('favicon', [
      'mimetype' => 'image/vnd.microsoft.icon',
      'path' => $wxt_path . '/favicon.ico',
      'url' => '',
      'use_default' => FALSE,
    ])
    ->save(TRUE);
}

/**
 * Setup languages prefixes.
 */
function _wxt_setup_lang_prefixes() {
  $config = \Drupal::configFactory()->getEditable('language.negotiation');
  $prefixes = $config->get('url.prefixes');
  foreach (\Drupal::languageManager()->getLanguages() as $language) {
    if (empty($prefixes[$language->getId()])) {
      // For all defined languages set prefix to langcode.
      $prefixes[$language->getId()] = $language->getId();
    }
  }
  $config->set('url.prefixes', $prefixes)->save();
}

/**
 * Set up the default menu override(s).
 */
function _wxt_setup_menus() {
  /** @var \Drupal\menu_link_content\MenuLinkContentInterface $user_account */
  $parent_link = MenuLinkContent::create([
    'menu_name' => 'account',
    'link' => 'route:<nolink>',
    'title' => 'User Account',
    'expanded' => TRUE,
  ]);
  $parent_link->save();

  $menu_links = [
    'user.page',
    'user.logout',
  ];
  foreach ($menu_links as $menu_link) {
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $link = $menu_link_manager->getDefinition($menu_link);
    $link['parent'] = $parent_link->getPluginId();
    $menu_link_manager->updateDefinition($menu_link, $link);
    $cache = \Drupal::cache('menu');
    $cache->deleteAll();
  }
}

/**
 * Setup the themes.
 */
function _wxt_setup_themes() {
  // Set the default and admin theme.
  \Drupal::configFactory()
    ->getEditable('system.theme')
    ->set('default', 'wxt_bootstrap')
    ->set('admin', 'claro')
    ->save(TRUE);

  // Enable the admin theme.
  \Drupal::configFactory()
    ->getEditable('node.settings')
    ->set('use_admin_theme', TRUE)
    ->save(TRUE);
}

/**
 * Setup default 403/404.
 */
function _wxt_setup_404() {
  // Set default "403".
  \Drupal::configFactory()
    ->getEditable('system.site')
    ->set('page.403', '/system/404')
    ->save(TRUE);

  // Set default "404".
  \Drupal::configFactory()
    ->getEditable('system.site')
    ->set('page.404', '/system/404')
    ->save(TRUE);
}

/**
 * Implements hook_update_N().
 *
 * Opt in to newer gcweb look for existing installs.
 */
function wxt_update_8201() {
  $config = \Drupal::configFactory()->getEditable('wxt_library.settings');
  $config->set('wxt.theme', 'theme-gcweb-legacy');
  $config->save();
}

/**
 * Installs the layout library module.
 */
function wxt_update_8202() {
  \Drupal::service('module_installer')->install(['layout_library']);
}
