using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;
using ZpcBulletinBoard.Data;
using ZpcBulletinBoard.Models.Editor;

namespace ZpcBulletinBoard.Pages.BoardsLinks
{
    [Authorize]
    public class IndexModel(ApplicationDbContext context) : PageModel
    {
        public IList<BulletinBoardPage> Pages { get;set; }

        public async Task OnGetAsync()
        {
            Pages = await context.BulletinBoardPage.OrderBy(x => x.Name).ToListAsync();
        }

        //Get
        public IActionResult OnGetBoards()
        {
            var boards = context.BulletinBoards
                .Include(x => x.Links.OrderBy(y => y.Order))
                .ThenInclude(x => x.BulletinBoardPage)
                .ToList();

            foreach (var board in boards)
            {
                foreach (var link in board.Links)
                {
                    link.BulletinBoardPage.Links = null;
                    link.BulletinBoard = null;
                }
            }

            return  new JsonResult(new { successful = true, error = $"", boards });
        }


        //Post
        public IActionResult OnPostAddLink(int idBoard, int idPage)
        {
            var order = 1;
            var links = context.BulletinBoardPageLinks.Where(x => x.IdBulletinBoardFk == idBoard).ToList();
            if (links.Any())
                order = links.Max(x => x.Order) + 1;

            var link = new BulletinBoardPageLink
            {
                Duration = 30,
                IdBulletinBoardFk = idBoard,
                IdBulletinBoardPageFk = idPage,
                Order = order
            };

            context.BulletinBoardPageLinks.Add(link);

            context.SaveChanges();

            link = context.BulletinBoardPageLinks.Include(x => x.BulletinBoardPage).First(x => x.IdLink == link.IdLink);

            link.BulletinBoardPage.Links = null;

            return new JsonResult(new { successful = true, error = $"", link });
        }

        public IActionResult OnPostSwapLinkOrder(int idLinkStart, int idLinkEnd)
        {
            var linkStart = context.BulletinBoardPageLinks.FirstOrDefault(x => x.IdLink == idLinkStart);
            var linkEnd = context.BulletinBoardPageLinks.FirstOrDefault(x => x.IdLink == idLinkEnd);

            if (linkStart == null)
                return new JsonResult(new { successful = false, error = $"Link with ID {idLinkStart} not exists!" });

            if (linkEnd == null)
                return new JsonResult(new { successful = false, error = $"Link with ID {idLinkEnd} not exists!" });

            (linkStart.Order, linkEnd.Order) = (linkEnd.Order, linkStart.Order);

            context.SaveChanges();

            return new JsonResult(new { successful = true, error = $"", linkStart, linkEnd });
        }

        public IActionResult OnPostLinkDuration(int idLink, int duration)
        {
            var link = context.BulletinBoardPageLinks.FirstOrDefault(x => x.IdLink == idLink);

            if (link == null)
                return new JsonResult(new { successful = false, error = $"Link with ID {idLink} not exists!" });

            link.Duration = duration;

            context.SaveChanges();

            return new JsonResult(new { successful = true, error = $"", link });
        }

        //Delete
        public IActionResult OnDeleteLink(int idLink)
        {
            var link = context.BulletinBoardPageLinks.FirstOrDefault(x => x.IdLink == idLink);
            if (link == null)
                return new JsonResult(new { successful = false, error = $"Link with ID {idLink} not exists!", link });

            var links = context.BulletinBoardPageLinks
                .Where(x => x.IdBulletinBoardFk == link.IdBulletinBoardFk && x.Order > link.Order).ToList();
            foreach (var tmpLink in links)
            {
                tmpLink.Order -= 1;
            }

            context.BulletinBoardPageLinks.Remove(link);

            context.SaveChanges();

            return new JsonResult(new { successful = true, error = $"", link });
        }
    }
}
