<?php

/**
 * @file
 * Install required content for testing.
 *
 * Each course is dedicated to one testable feature.
 */

use Drupal\Core\File\FileExists;
use Drupal\Core\Utility\Error;
use Drupal\Component\Utility\DeprecationHelper;

/**
 * Implements hook_install().
 */
function anu_lms_demo_content_modules_installed($modules, $is_syncing) {
  // Check for itself. This gives the opportunity for configuration
  // to be installed such as pathauto patterns.
  if (!in_array('anu_lms_demo_content', $modules)) {
    return;
  }
  $categories = anu_lms_demo_create_course_categories();
  [$first_category, $second_category] = $categories;
  [$first_label, $second_label] = anu_lms_demo_create_course_labels();
  $default_cover_image = [
    ['target_id' => anu_lms_demo_create_cover_image(), 'alt' => 'Anu LMS logo'],
  ];
  // Create courses page.
  anu_lms_demo_create_courses_landing_page($categories);

  // Create separate courses for testing specific features.
  anu_lms_demo_create_course_for_testing_paragraphs([
    'field_course_image' => $default_cover_image,
    'field_course_category' => [$first_category],
    'field_course_label' => [$first_label],
  ]);
  anu_lms_demo_create_course_for_testing_navigation(
    [
      'field_course_image' => $default_cover_image,
      'field_course_category' => [$second_category],
      'field_course_label' => [$second_label],
    ]
  );
}

/**
 * Creates course categories.
 */
function anu_lms_demo_create_course_categories() {
  $categories = [];

  $categories[] = [
    'vid' => 'course_category',
    'name' => 'Getting started [DEMO]',
  ];

  $categories[] = [
    'vid' => 'course_category',
    'name' => 'Developer guides [DEMO]',
  ];

  $entities = anu_lms_demo_content_create_entities($categories, 'taxonomy_term');
  return array_keys($entities);
}

/**
 * Creates course labels.
 */
function anu_lms_demo_create_course_labels() {
  $categories = [];

  $categories[] = [
    'vid' => 'course_label',
    'name' => 'Demo label 1',
  ];

  $categories[] = [
    'vid' => 'course_label',
    'name' => 'Demo label 2',
  ];

  $entities = anu_lms_demo_content_create_entities($categories, 'taxonomy_term');
  return array_keys($entities);
}

/**
 * Creates a course with all available paragraphs.
 */
function anu_lms_demo_create_course_for_testing_paragraphs($extra_data = []) {
  $lessons_data = [];

  // Headings lesson start.
  $headings_data = [
    [
      'type' => 'lesson_heading',
      'field_lesson_heading_value' => 'Lesson heading - h2',
      'field_lesson_heading_size' => 'h2',
    ],
    [
      'type' => 'lesson_heading',
      'field_lesson_heading_value' => 'Lesson heading - h3',
      'field_lesson_heading_size' => 'h3',
    ],
    [
      'type' => 'lesson_heading',
      'field_lesson_heading_value' => 'Lesson heading - h4',
      'field_lesson_heading_size' => 'h4',
    ],
  ];
  $paragraphs = anu_lms_demo_content_create_entities($headings_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Headings',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // Headings lesson end.
  // Text lesson start.
  $text_items_data = [
    [
      'type' => 'lesson_text',
      'field_lesson_text_content' => [
        'value' => '<p>This is short text without formatting</p>',
        'format' => 'minimal_html',
      ],
    ],
    [
      'type' => 'lesson_text',
      'field_lesson_text_content' => [
        'value' => '<p>This is long text with formatting.
        <p><strong>Anu</strong> is a deceptively simple learning management system (LMS) for individual teachers, educational organisations and businesses built on Drupal. It’s been developed to provide engaging and impactful learning for real people, with a UX based on extensive research.</p>
        <p><a href="https://github.com/systemseed/anu_lms">Link to GitHub repository.</a></p>
        ',
        'format' => 'minimal_html',
      ],
    ],
    [
      'type' => 'lesson_footnotes',
      'field_lesson_footnotes_content' => [
        'value' => '<p>This is short text in the footnotes.</p>',
        'format' => 'minimal_html',
      ],
    ],
  ];
  $paragraphs = anu_lms_demo_content_create_entities($text_items_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Text',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // Text lesson end.
  // List lesson start.
  $list_items_data = [
    [
      'type' => 'lesson_list',
      'field_lesson_list_type' => 'ul',
      'field_lesson_list_items' => [
        'Chocolate',
        'Strawberry',
        'Vanilla',
      ],
    ],
    [
      'type' => 'lesson_list',
      'field_lesson_list_type' => 'ol',
      'field_lesson_list_items' => [
        'One',
        'Two',
        'Three',
      ],
    ],
  ];
  $paragraphs = anu_lms_demo_content_create_entities($list_items_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Lists',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // List lesson end.
  // Images lesson start.
  $image_items_data = [
    [
      'type' => 'lesson_image',
      'field_lesson_image_caption' => 'Image with caption',
      'field_lesson_image_image' => $extra_data['field_course_image'],
    ],
    [
      'type' => 'lesson_image_wide',
      'field_lesson_image_caption' => 'Image with caption (full width)',
      'field_lesson_image_image' => $extra_data['field_course_image'],
    ],
    [
      'type' => 'lesson_image_thumbnail',
      'field_lesson_image_caption_long' => [
        'value' => '<p>Image with a longer caption (<em>thumbnail</em>)</p>',
        'format' => 'minimal_html',
      ],
      'field_lesson_image_image' => $extra_data['field_course_image'],
    ],
  ];
  $paragraphs = anu_lms_demo_content_create_entities($image_items_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Images',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // Images lesson end.
  // Highlights lesson start.
  $highlight_items_data = [];
  foreach (['yellow', 'blue', 'purple', 'green'] as $color) {
    $highlight_items_data[] = [
      'type' => 'lesson_highlight',
      'field_lesson_highlight_color' => $color,
      'field_lesson_highlight_heading' => 'Highlight (full width)',
      'field_lesson_highlight_text' => [
        'value' => "<p>Highlight (full width) text (<em>$color</em>)</p>",
        'format' => 'minimal_html',
      ],
    ];
    $highlight_items_data[] = [
      'type' => 'lesson_highlight_marker',
      'field_lesson_highlight_color' => $color,
      'field_highlight_marker_text' => "Highlight (marker) text ($color)",
    ];

    $paragraphs = anu_lms_demo_content_create_entities([
      [
        'type' => 'lesson_img_list_item',
        'field_lesson_img_list_item_align' => 'middle',
        'field_lesson_img_list_item_image' => $extra_data['field_course_image'],
        'field_lesson_img_list_item_size' => 'small',
        'field_lesson_img_list_item_text' => [
          'value' => '<p>Highlight with image (<em>small</em>)</p>',
          'format' => 'minimal_html',
        ],
      ],
      [
        'type' => 'lesson_img_list_item',
        'field_lesson_img_list_item_align' => 'middle',
        'field_lesson_img_list_item_image' => $extra_data['field_course_image'],
        'field_lesson_img_list_item_size' => 'large',
        'field_lesson_img_list_item_text' => [
          'value' => '<p>Highlight with image (<em>large</em>)</p>',
          'format' => 'minimal_html',
        ],
      ],
    ], 'paragraph');
    $highlight_items_data[] = [
      'type' => 'lesson_img_list',
      'field_lesson_highlight_color' => $color,
      'field_lesson_img_list_heading' => 'Highlight (with image)',
      'field_lesson_img_list_items' => array_values($paragraphs),
    ];
  }
  $paragraphs = anu_lms_demo_content_create_entities($highlight_items_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Highlights',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // Highlights lesson end.
  // Dividers lesson start.
  $divider_items_data = [
    [
      'type' => 'lesson_text',
      'field_lesson_text_content' => [
        'value' => 'Example of a simple divider.',
        'format' => 'minimal_html',
      ],
    ],
    [
      'type' => 'lesson_divider',
      'field_lesson_divider_type' => 'simple',
    ],
  ];
  foreach ([1, 2, 3] as $item) {
    $divider_items_data[] = [
      'type' => 'lesson_text',
      'field_lesson_text_content' => [
        'value' => "Example of a numeric divider #$item.",
        'format' => 'minimal_html',
      ],
    ];
    $divider_items_data[] = [
      'type' => 'lesson_divider',
      'field_lesson_divider_type' => 'numeric',
    ];
  }
  $paragraphs = anu_lms_demo_content_create_entities($divider_items_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Dividers',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // Dividers lesson end.
  // Video lesson start.
  $video_items_data = [
    [
      'type' => 'lesson_embedded_video',
      'field_lesson_embedded_video_url' => ['uri' => 'https://www.youtube.com/watch?v=mqUV4ZWzkew'],
    ],
  ];
  $paragraphs = anu_lms_demo_content_create_entities($video_items_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Video',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // Video lesson end.
  // Checklist lesson start.
  $checklist_items = [];
  foreach ([1, 2, 3] as $item) {
    $checklist_items[] = [
      'type' => 'checklist_item',
      'field_checkbox_option' => [
        'value' => "Option $item",
        'format' => 'minimal_html',
      ],
      'field_lesson_text_content' => [
        'value' => "More about option <strong>$item</strong>",
        'format' => 'minimal_html',
      ],
    ];
  }
  $paragraphs = anu_lms_demo_content_create_entities($checklist_items, 'paragraph');
  $checklist_items_data = [
    [
      'type' => 'lesson_checklist',
      'field_checklist_items' => array_values($paragraphs),
    ],
  ];
  $paragraphs = anu_lms_demo_content_create_entities($checklist_items_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Checklists',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // Checklist lesson end.
  // Table lesson start.
  $table_items_data = [
    [
      'type' => 'lesson_table',
      'field_lesson_table_caption' => 'Table caption',
      'field_lesson_table_content' => [
        'value' => "<table>
	<thead>
		<tr>
			<th>Odd values</th>
			<th>Even values</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>Value 1</td>
			<td>Value 2</td>
		</tr>
		<tr>
			<td>Value 3</td>
			<td>Value 4</td>
		</tr>
		<tr>
			<td>Value 5</td>
			<td>Value 6</td>
		</tr>
		<tr>
			<td>Value 7</td>
			<td>Value 8</td>
		</tr>
	</tbody>
</table>",
        'format' => 'table',
      ],
    ],
  ];
  $paragraphs = anu_lms_demo_content_create_entities($table_items_data, 'paragraph');
  $default_section = anu_lms_demo_content_create_entities([
    [
      'type' => 'lesson_section',
      'field_lesson_section_content' => array_values($paragraphs),
    ],
  ], 'paragraph');

  $lessons_data[] = [
    'type' => 'module_lesson',
    'title' => 'Tables',
    'field_module_lesson_content' => array_values($default_section),
  ];
  // Table lesson end.
  $lessons = anu_lms_demo_content_create_entities($lessons_data, 'node');
  $default_module = anu_lms_demo_content_create_entities([
    [
      'type' => 'course_modules',
      'field_module_title' => 'Anu items',
      'field_module_lessons' => array_values($lessons),
    ],
  ], 'paragraph');

  $course = [
    'type' => 'course',
    'title' => 'Learn Anu lesson item types [DEMO]',
    'path' => '/course/demo-learn-anu-lesson-item-types',
    'field_course_description' => [
      'value' => 'This course contains all available lesson item types.',
      'format' => 'minimal_html',
    ],
    'field_course_finish_button' => ['uri' => 'internal:/courses/courses-demo'],
    'field_course_module' => array_values($default_module),
  ] + $extra_data;

  anu_lms_demo_content_create_entities([$course], 'node');
}

/**
 * Creates a course with multiple modules and sections.
 */
function anu_lms_demo_create_course_for_testing_navigation($extra_data = []) {
  // Modules -> Lessons -> Sections with text items.
  $content = [
    'Intro' => [
      'Intro 1' => ["
<p>Anu course is broken down into modules. Each module includes one or more lessons.</p>
<p>Modules and lessons are visible in the course navigation.</p>,",
      ],
      'Intro 2' => ["
<p>This is the second lesson of the first module.</p>
<p>Each lesson can have one or more sections. Sections are not visible in the course navigation.</p>
    ",
      ],
    ],
    'Modules' => [
      "Modules lesson" => ["
<p>Module is a paragraph with two fields: title and lessons.</p>
<p>Modules can be created and edited on the course edit form.</p>
<p>You are currently in the second module of the course. Click Next to go to the next module.</p>
    ",
      ],
    ],
    'Sections' => [
      "Sections lesson" => ["
<p>Section is a paragraph that doesn't have any visible fields. Its  purpose is to break lesson items into pages that can be navigated using Back/Next buttons.</p>
<p>Click the Next button to see the next section of this module.</p>
    ",
        "
<p>This is the second section of the Sections lesson.</p>
<p>Sections can be created and edited on the lesson edit form.</p>
",
        "
<p>This is the third section of the lesson.</p>
<p>Click the Finish button to finish this course.</p>
",
      ],
    ],
  ];

  $modules = [];
  foreach ($content as $module => $lessons_data) {
    $lessons = [];
    foreach ($lessons_data as $lesson_name => $sections_data) {
      $sections = [];
      foreach ($sections_data as $text) {
        $items_data = [
          [
            'type' => 'lesson_text',
            'field_lesson_text_content' => [
              'value' => $text,
              'format' => 'minimal_html',
            ],
          ],
        ];

        $paragraphs = anu_lms_demo_content_create_entities($items_data, 'paragraph');
        $sections += anu_lms_demo_content_create_entities([
          [
            'type' => 'lesson_section',
            'field_lesson_section_content' => array_values($paragraphs),
          ],
        ], 'paragraph');
      }

      $lessons += anu_lms_demo_content_create_entities([
        [
          'type' => 'module_lesson',
          'title' => $lesson_name,
          'field_module_lesson_content' => array_values($sections),
        ],
      ], 'node');
    }

    $modules += anu_lms_demo_content_create_entities([
      [
        'type' => 'course_modules',
        'field_module_title' => $module,
        'field_module_lessons' => array_values($lessons),
      ],
    ], 'paragraph');
  }

  $course = [
    'type' => 'course',
    'title' => 'Modules, lessons and sections  [DEMO]',
    'path' => '/course/demo-navigation',
    'field_course_description' => [
      'value' => 'This course contains multiple modules, lessons and sections.',
      'format' => 'minimal_html',
    ],
    'field_course_finish_button' => ['uri' => 'internal:/courses/courses-demo'],
    'field_course_module' => array_values($modules),
  ] + $extra_data;

  anu_lms_demo_content_create_entities([$course], 'node');
}

/**
 * Create cover image for courses.
 */
function anu_lms_demo_create_cover_image() {
  $data = file_get_contents(__DIR__ . "/anu-logo.png");
  $fileRepository = \Drupal::service('file.repository');

  /** @var \Drupal\file\Entity\File $file */
  $file = $fileRepository->writeData($data, 'public://anu_lms_logo.png', FileExists::Replace);

  $state = \Drupal::state()->get('anu_lms_demo_content.entities', []);
  $state['file'][] = $file->id();
  \Drupal::state()->set('anu_lms_demo_content.entities', $state);

  return $file->id();
}

/**
 * Create courses landing page.
 */
function anu_lms_demo_create_courses_landing_page($category_ids = []) {
  $courses_page = [
    'type' => 'courses_landing_page',
    'title' => 'Courses  [DEMO]',
    'path' => '/courses/courses-demo',
    'field_courses_page_categories' => array_values($category_ids),
  ];
  anu_lms_demo_content_create_entities([$courses_page], 'node');
}

/**
 * Helper to create entities.
 */
function anu_lms_demo_content_create_entities($data, $entity_type) {
  $entities = [];
  $state = \Drupal::state()->get('anu_lms_demo_content.entities', []);

  $storage = \Drupal::entityTypeManager()->getStorage($entity_type);

  foreach ($data as $entity_data) {
    $entity = $storage->create($entity_data);
    $entity->save();
    $entities[$entity->id()] = $entity;

    $state[$entity_type][] = $entity->id();
  }

  // Save entity ids in Drupal state to be able to delete them
  // on module uninstall.
  \Drupal::state()->set('anu_lms_demo_content.entities', $state);

  return $entities;
}

/**
 * Implements hook_uninstall().
 */
function anu_lms_demo_content_uninstall() {
  if ($state = \Drupal::state()->get('anu_lms_demo_content.entities')) {
    try {
      foreach ($state as $entity_type => $ids) {
        $storage = \Drupal::entityTypeManager()->getStorage($entity_type);
        $storage->delete($storage->loadMultiple($ids));
      }
    }
    catch (\Exception $e) {
      DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('anu_lms_demo_content'), $e), fn() => watchdog_exception('anu_lms_demo_content', $e));
    }

    \Drupal::state()->delete('anu_lms_demo_content.entities');
  }

}
