<?php

/**
 * @file
 * Contains d_demo module.
 */

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_content_structure_alter().
 */
function d_demo_content_structure_alter(&$structure, $context) {
  $path = \Drupal::service('extension.path.resolver')->getPath('module', 'd_demo') . '/pages';
  if ($context == 'all') {
    $structure['homepage'] = [
      'file' => "$path/homepage.yml",
      'link' => 'Home',
      'weight' => -50,
    ];
    $structure['invest'] = ['file' => "$path/invest.yml"];
    $structure['services'] = [
      'file' => "$path/services.yml",
      'link' => 'Services',
      'weight' => 20,
    ];
    $structure['services_engine_installation'] = ['file' => "$path/services_engine_installation.yml"];
    $structure['services_engine_overhaul'] = ['file' => "$path/services_engine_overhaul.yml"];
    $structure['services_monitoring_and_assessment'] = ['file' => "$path/services_monitoring_and_assessment.yml"];
    $structure['services_engine_repair_and_overhaul'] = ['file' => "$path/services_engine_repair_and_overhaul.yml"];
    $structure['services_engine_repair'] = ['file' => "$path/services_engine_repair.yml"];
    $structure['services_engine_oil_change'] = ['file' => "$path/services_engine_oil_change.yml"];
    $structure['services_engine_testing_services'] = ['file' => "$path/services_engine_testing_services.yml"];
    $structure['services_for_aircrafts'] = ['file' => "$path/services_for_aircrafts.yml"];
    $structure['services_for_boats'] = ['file' => "$path/services_for_boats.yml"];
    $structure['services_for_cars'] = ['file' => "$path/services_for_cars.yml"];
    $structure['services_regular_servicing '] = ['file' => "$path/services_regular_servicing.yml"];
    $structure['services_ad_hoc_engine_repairs'] = ['file' => "$path/services_ad_hoc_engine_repairs.yml"];
    $structure['services_regular_servicing'] = ['file' => "$path/services_regular_servicing.yml"];
    $structure['services_for_spacecrafts'] = ['file' => "$path/services_for_spacecrafts.yml"];
    $structure['contact'] = ['file' => "$path/contact.yml"];
    $structure['about_us'] = ['file' => "$path/about_us.yml"];
    $structure['careers'] = ['file' => "$path/careers.yml"];
    $structure['engines_car'] = [
      'file' => "$path/engines_car.yml",
      'link' => 'Car Engines',
      'parent' => 'd_demo.engines',
      'children' => [
        'engines_car_diesel' => [
          'file' => "$path/engines_car_diesel.yml",
          'link' => 'Diesel Engines',
          'weight' => 0,
        ],
        'engines_car_gasoline' => [
          'file' => "$path/engines_car_gasoline.yml",
          'link' => 'Gasoline Engines',
          'weight' => 1,
        ],
      ],
    ];
    $structure['engines_boat'] = [
      'file' => "$path/engines_boat.yml",
      'link' => 'Boat Engines',
      'parent' => 'd_demo.engines',
    ];
    $structure['engines_aircraft'] = [
      'file' => "$path/engines_aircraft.yml",
      'link' => 'Aircraft Engines',
      'parent' => 'd_demo.engines',
    ];
    $structure['gdpr'] = ['file' => "$path/gdpr.yml"];
    $structure['terms_of_service'] = ['file' => "$path/terms_of_service.yml"];
    $structure['privacy_policy'] = ['file' => "$path/privacy_policy.yml"];
  }
}

/**
 * Implements hook_block_structure_alter().
 */
function d_demo_block_structure_alter(&$structure, $context) {
  $path = \Drupal::service('extension.path.resolver')->getPath('module', 'd_demo') . '/blocks';
  if ($context == 'block_second_run') {
    $structure['cars'] = ['file' => "$path/mega_submenu_services_for_cars.yml"];
    $structure['boats'] = ['file' => "$path/mega_submenu_services_for_boats.yml"];
    $structure['aircrafts'] = ['file' => "$path/mega_submenu_services_for_aircrafts.yml"];
    $structure['spacecrafts'] = ['file' => "$path/mega_submenu_services_for_spacecrafts.yml"];
  }

  if ($context == 'all') {
    $structure['footer_office_1'] = ['file' => "$path/footer_office_1.yml"];
    $structure['footer_office_2'] = ['file' => "$path/footer_office_2.yml"];
    $structure['footer_engines'] = ['file' => "$path/footer_engines.yml"];
    $structure['footer_services'] = ['file' => "$path/footer_services.yml"];
    $structure['footer_others'] = ['file' => "$path/footer_others.yml"];
    $structure['footer_secondary_menu'] = ['file' => "$path/footer_secondary_menu.yml"];
    $structure['d_social_media_footer'] = ['file' => "$path/d_social_media_footer.yml"];
    $structure['secondary_menu'] = ['file' => "$path/secondary_menu.yml"];
    $structure['bottom_footer_menu'] = ['file' => "$path/bottom_footer_menu.yml"];

    if (\Drupal::moduleHandler()->moduleExists('d_search')) {
      $structure['header_search_link'] = ['file' => "$path/header_search_link.yml"];
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function d_demo_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
  switch ($form_id) {
    case 'contact_message_feedback_form':
      unset($form['actions']['preview']);
      unset($form['copy']);
      foreach ($form as &$formElement) {
        if (isset($formElement['#type'])) {
          switch ($formElement['#type']) {
            case 'text':
            case 'textfield':
            case 'email':
              $formElement['#attributes']['placeholder'] = $formElement['#title'];
              $formElement['#title_display'] = 'invisible';
              break;

            case 'item':
            case 'container':
              if (!empty($formElement['widget']['#title'])) {
                $title = $formElement['widget']['#title'];
                $formElement['widget'][0]['value']['#attributes']['placeholder'] = $title;
                $formElement['widget'][0]['value']['#title_display'] = 'invisible';
              }
              break;
          }
        }
      }

      // Add wrapper for name and mail field.
      $wrapper = [
        '#type' => 'container',
        '#weight' => -666,
        '#attributes' => [
          'class' => ['name-and-mail'],
        ],
      ];

      $wrapper['name'] = $form['name'];
      $wrapper['mail'] = $form['mail'];
      if ($wrapper['name']['#type'] == 'item') {
        $wrapper['name']['#type'] = 'textfield';
        $wrapper['name']['#disabled'] = TRUE;
        $wrapper['name']['#required'] = TRUE;
        $wrapper['name']['#attributes']['placeholder'] = $wrapper['name']['#title'];
        $wrapper['name']['#title_display'] = 'invisible';
      }
      if ($wrapper['mail']['#type'] == 'item') {
        $wrapper['mail']['#type'] = 'textfield';
        $wrapper['mail']['#disabled'] = TRUE;
        $wrapper['mail']['#required'] = TRUE;
        $wrapper['mail']['#attributes']['placeholder'] = $wrapper['mail']['#title'];
        $wrapper['mail']['#title_display'] = 'invisible';
      }

      $form['name_and_mail'] = $wrapper;
      unset($form['name']);
      unset($form['mail']);

      break;
  }
}

/**
 * Implements hook_attachments().
 */
function d_demo_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'd_demo/d_demo_random_price';
}
