using System;
using Cogwheel;

namespace ADEPT.Core.Lesson;

public class LessonHandler : ILessonHandler
{
    public event EventHandler LessonLoaded;
    public event EventHandler PageChanged;
    
    public ILesson CurrentLesson { get; private set; }
    public IPage CurrentPage { get; private set; }

    public void Initialize()
    {
        COGWHEEL.RegisterObject(this);
    }

    [Command(Name = "lesson.load", Description = "Load a lesson.")]
    public void LoadLesson(string lessonName)
    {
        var path = $"{Adept.Constants.DefaultLessonPath}{lessonName}{Adept.Constants.DefaultLessonFileExtension}";
        
        COGWHEEL.Log($"[ADEPT] Attempting to load lesson '{lessonName}' at '{path}'");
        
        // Lesson lesson = null;
        //
        // CurrentLesson = lesson;
        // CurrentPage = CurrentLesson.Pages.ToArray()[0];
        //
        // LessonLoaded?.Invoke(this, EventArgs.Empty);
    }

    [Command(Name = "lesson.createtest", Description = "Create a test lesson.")]
    private void CreateTestLesson()
    {
        COGWHEEL.Log($"[ADEPT] Creating a Test Lesson");
        var lesson = new Lesson()
        {
            Title = "Test Lesson",
            Description = "This is a test lesson.",
            Pages = new()
            {
                new Page() { Title = "Test Page 1", Description = "A test page.", PageContext = PageContext.TwoD},
                new Page() { Title = "Test Page 2", Description = "A test page #2.", PageContext = PageContext.ThreeD},
                new Page() { Title = "Test Page 3", Description = "A test page #3.", PageContext = PageContext.TwoD},
                new Page() { Title = "Test Page 4", Description = "A test page #4.", PageContext = PageContext.ThreeD},
                new Page() { Title = "Test Page 5", Description = "A test page #5.", PageContext = PageContext.TwoD},
            }
        };
        
        CurrentLesson = lesson;
        CurrentPage = CurrentLesson.Pages.ToArray()[0];
        
        LessonLoaded?.Invoke(this, EventArgs.Empty);
    }
    
    public void SaveLesson(ILesson lesson, string lessonName)
    {
        throw new System.NotImplementedException();
    }

    [Command(Name = "lesson.next", Description = "Load the next page in the current lesson.")]
    public void LoadNextPage()
    {
        var nextIndex = CurrentLesson.Pages.IndexOf(CurrentPage) + 1;
        if (nextIndex >= CurrentLesson.Pages.Count)
        {
            COGWHEEL.LogWarning("[LESSONHANDLER] Cannot load next page. No more pages in lesson.");
            return;
        }
        
        CurrentPage = CurrentLesson.Pages.ToArray()[nextIndex];
        PageChanged?.Invoke(this, EventArgs.Empty);
    }

    [Command(Name = "lesson.previous", Description = "Load the previous page in the current lesson.")]
    public void LoadPreviousPage()
    {
        var prevIndex = CurrentLesson.Pages.IndexOf(CurrentPage) - 1;
        if (prevIndex < 0)
        {
            COGWHEEL.LogWarning("[LESSONHANDLER] Cannot load previous page. No more pages in lesson.");
            return;
        }
        
        CurrentPage = CurrentLesson.Pages.ToArray()[prevIndex];
        PageChanged?.Invoke(this, EventArgs.Empty);
    }
    
    [Command(Name = "lesson.gotopage", Description = "Load a specific page in the current lesson.")]
    public void LoadPageToIndex(int index)
    {
        if (index >= CurrentLesson.Pages.Count || index < 0)
        {
            COGWHEEL.LogWarning("[LESSONHANDLER] Cannot load page. No such page in lesson.");
            return;
        }
        
        COGWHEEL.Log("[LESSONHANDLER] Loading Page at index " + index);
        CurrentPage = CurrentLesson.Pages.ToArray()[index];
        PageChanged?.Invoke(this, EventArgs.Empty);
    }
}