using Front.Program.Services;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Front.Program.ViewModels;

using Front.Program.Views.Academy.Main;

namespace Front.Program.Views.Academy
{
    public partial class AcademyMain : ComponentBase
    {
        [Inject] NavigationManager Navigation { get; set; } = default!;
        [Inject] UserStateService UserStateService { get; set; } = default!;
        [Inject] QueryParamService QueryParamService { get; set; } = default!;
        [Inject] StorageService StorageService { get; set; } = default!;
        [Inject] LoadingService LoadingService { get; set; } = default!;
        
        [Inject] NavigationService NavigationService { get; set; } = default!;

        private List<FavoriteItem> Favorites { get; set; } = new();
        private AttendanceInfo Attendance { get; set; } = new();
        private List<LearningClass> LearningClasses { get; set; } = new();
        private List<ScheduleItem> ScheduleItems { get; set; } = new();
        private AcademyNewsItem News { get; set; } = new();
        private List<UpcomingClassItem> UpcomingClasses { get; set; } = new();
        
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                // 뒤로가기를 완전히 막기
                await NavigationService.PreventBackNavigationAsync();
            
                // 또는 확인 대화상자와 함께 막기
                // await NavigationService.PreventBackNavigationWithConfirmAsync("정말로 이 페이지를 떠나시겠습니까?");
            }
        }

        protected override void OnInitialized()
        {
            // Sample Data Initialization
            Favorites = new List<FavoriteItem>
            {
                new() { Name = "학습 > 수업" },
                new() { Name = "학습 > 과제" },
                new() { Name = "커뮤니티 > 공지" },
                new() { Name = "커뮤니티 > Q&A" },
                new() { Name = "마이페이지" }
            };

            Attendance = new AttendanceInfo
            {
                Month = 7,
                MonthlyPercentage = 10,
                MonthlyAttendedDays = 11,
                MonthlyTotalDays = 31,
                DailyPercentage = 75,
                DailyAttendedHours = 3,
                DailyTotalHours = 4
            };

            LearningClasses = new List<LearningClass>
            {
                new() { Day = "월", Teacher = "선생님1", ClassName = "선생님1 의 수업", Progress = 20, Homework = "O" },
                new() { Day = "월", Teacher = "선생님2", ClassName = "선생님2 의 수업", Progress = 30, Homework = "X" },
                new() { Day = "화", Teacher = "선생님3", ClassName = "선생님3 의 수업", Progress = 45, Homework = "O" }
            };

            ScheduleItems = new List<ScheduleItem>
            {
                new() { IsImportant = true, Description = "중간고사", Date = new System.DateTime(2025, 12, 31) },
                new() { IsImportant = false, Description = "보강수업", Date = new System.DateTime(2025, 12, 31) },
                new() { IsImportant = true, Description = "학부모 상담", Date = new System.DateTime(2025, 12, 31) }
            };

            News = new AcademyNewsItem
            {
                Title = "School Closure on Friday",
                Content = "Due to the upcoming storm, the school will be closed on Friday, March 15th. Please stay safe and monitor your email for updates.",
                Date = new System.DateTime(2025, 12, 31),
                ImageUrl = "" // or a path to an image
            };

            UpcomingClasses = new List<UpcomingClassItem>
            {
                new() { ClassName = "수업 이름이 얼마나 길어질지는 모르겠지만 이정도는?", Date = "2025-07-14 (월)", Time = "09:00 ~ 11:00", Teacher = "이름이다섯 선생님" },
                new() { ClassName = "수학", Date = "2025-07-14 (월)", Time = "11:00 ~ 13:00", Teacher = "김수학 선생님" },
                new() { ClassName = "영어", Date = "2025-07-15 (화)", Time = "09:00 ~ 11:00", Teacher = "박영어 선생님" },
                new() { ClassName = "과학", Date = "2025-07-15 (화)", Time = "11:00 ~ 13:00", Teacher = "최과학 선생님" },
                new() { ClassName = "코딩", Date = "2025-07-16 (수)", Time = "14:00 ~ 16:00", Teacher = "이코딩 선생님" }
            };
        }

        // Data Models
        public class FavoriteItem
        {
            public string Name { get; set; } = "";
        }

        public class AttendanceInfo
        {
            public int Month { get; set; }
            public int MonthlyPercentage { get; set; }
            public int MonthlyAttendedDays { get; set; }
            public int MonthlyTotalDays { get; set; }
            public int DailyPercentage { get; set; }
            public int DailyAttendedHours { get; set; }
            public int DailyTotalHours { get; set; }
        }

        public class LearningClass
        {
            public string Day { get; set; } = "";
            public string Teacher { get; set; } = "";
            public string ClassName { get; set; } = "";
            public int Progress { get; set; }
            public string Homework { get; set; } = "";
        }

        public class ScheduleItem
        {
            public bool IsImportant { get; set; }
            public string Description { get; set; } = "";
            public System.DateTime Date { get; set; }
        }

        public class AcademyNewsItem
        {
            public string Title { get; set; } = "";
            public string Content { get; set; } = "";
            public System.DateTime Date { get; set; }
            public string? ImageUrl { get; set; }
        }

        public class UpcomingClassItem
        {
            public string ClassName { get; set; } = "";
            public string Date { get; set; } = "";
            public string Time { get; set; } = "";
            public string Teacher { get; set; } = "";
        }
    }
}
