<?php

/**
 * @file
 * D_commerce module.
 */

use Drupal\Core\GeneratedLink;
use Drupal\Core\Url;

/**
 * Implements hook_theme().
 */
function d_commerce_theme($existing, $type, $theme, $path) {
  return [
    'commerce_checkout_order_summary' => [
      'template' => 'd_commerce-checkout-order-summary',
    ],
    'commerce_order_receipt__default' => [
      'template' => 'commerce-order-receipt--default',
      'base hook' => 'commerce_order_receipt__default',
    ],
    'commerce_order_total_summary__default' => [
      'template' => 'commerce-order-total-summary--default',
      'base hook' => 'commerce_order_total_summary',
    ],
    'commerce_coupon_redemption_form' => [
      'template' => 'd_commerce-coupon-redemption-form',
    ],
    'commerce_checkout_completion_message' => [
      'template' => 'commerce-checkout-completion-message',
      'base hook' => 'commerce_checkout_completion_message',
    ],
    'commerce_checkout_completion_register' => [
      'template' => 'commerce-checkout-completion-register',
      'base hook' => 'commerce_checkout_completion_register',
    ],
  ];
}

/**
 * Implements hook_preprocess_page().
 */
function d_commerce_preprocess_page(&$variables) {
  _d_commerce_attach_libraries($variables);
}

/**
 * Implements hook_preprocess_maintenance_page().
 */
function d_commerce_preprocess_maintenance_page(&$variables) {
  _d_commerce_attach_libraries($variables);
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function d_commerce_theme_suggestions_commerce_order_total_summary_alter(array &$suggestions, array $variables) {
  $suggestions[] = $variables['theme_hook_original'] . '__' . $variables['order_entity']->bundle();
}

/**
 * Implements hook_preprocess_HOOK().
 */
function d_commerce_preprocess_commerce_cart_block(&$variables) {
  // Remove original cart icon.
  $variables['icon'] = NULL;

  // Regenerate cart link with button classes.
  $options = [
    'attributes' => ['class' => ['btn btn-primary']],
  ];
  $variables['links'] = [
    [
      '#type' => 'link',
      '#title' => t('Cart'),
      '#url' => Url::fromRoute('commerce_cart.page', [], $options),
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function d_commerce_form_commerce_checkout_flow_multistep_default_alter(&$form, &$form_state, $form_id) {
  if (
    isset($form['actions']['next']['#suffix']) &&
    $form['actions']['next']['#suffix'] instanceof GeneratedLink
  ) {
    $form['actions']['next']['#suffix']->setGeneratedLink(str_replace(
      'link--previous',
      'link--previous btn btn-light',
      $form['actions']['next']['#suffix']->getGeneratedLink()
    ));
  }
  if (isset($form['shipping_information']['recalculate_shipping'])) {
    $form['shipping_information']['recalculate_shipping']['#attributes']['class'][] = 'btn btn-light mb-4';
  }
  if (isset($form['shipping_information']['shipping_profile']['edit_button'])) {
    $form['shipping_information']['shipping_profile']['edit_button']['#attributes']['class'][] = 'btn btn-light';
  }
  if (isset($form['payment_information']['billing_information']['edit_button'])) {
    $form['payment_information']['billing_information']['edit_button']['#attributes']['class'][] = 'btn btn-light';
  }

  $step = $form['#step_id'] ?? '';

  switch ($step) {
    case 'login':
      if (isset($form['login']['returning_customer']['submit'])) {
        $form['login']['returning_customer']['submit']['#attributes']['class'][] = 'mr-3';
      }
      if (isset($form['login']['returning_customer']['forgot_password'])) {
        $form['login']['returning_customer']['forgot_password']['#attributes'] = [
          'class' => [
            'btn',
            'btn-outline-primary',
          ],
        ];
      }
      break;

    case 'order_information':
      if (isset($form['payment_information']['billing_information'])) {
        $form['payment_information']['billing_information']['#weight'] = -1;
      }
      break;
  }
}

/**
 * Attach related libraries from other modules and themes.
 *
 * @param array $variables
 *   Array of theme variables.
 */
function _d_commerce_attach_libraries(array &$variables) {
  $module_name = 'd_commerce';
  $theme_name = 'droopler_theme';
  if (\Drupal::service('theme_handler')->themeExists($theme_name)) {
    $d_commerce_library = \Drupal::service('library.discovery')
      ->getLibraryByName($theme_name, $module_name);

    if ($d_commerce_library) {
      $variables['#attached']['library'][] = implode('/', [
        $theme_name,
        $module_name,
      ]);
    }
  }
}
