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;
using Microsoft.AspNetCore.Hosting;
using static System.Net.Mime.MediaTypeNames;

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

        public int OpenEditorForPage = 0;

        public async Task OnGetAsync(int? openEditorForPage = null)
        {
            OpenEditorForPage = openEditorForPage ?? 0;
            Pages = await context.BulletinBoardPage.ToListAsync();
        }

        //Get
        public IActionResult OnGetPage(int id)
        {
            var page = context.BulletinBoardPage.Include(x => x.Links)
                .FirstOrDefault(x => x.IdBulletinBoardPage == id);
            if (page == null)
                return new JsonResult(new { successful = false, error = $"Page with ID {id} not exists!" });

            foreach (var link in page.Links)
            {
                link.BulletinBoardPage.Links = null;
            }
            return new JsonResult(new { successful = true, error = $"", page });
        }

        //Posts
        public IActionResult OnPostBoardPage(BulletinBoardPage page)
        {
            if (page.IdBulletinBoardPage <= 0)
            {
                context.BulletinBoardPage.Add(page);
            }
            else
            {
                var tmpPage = context.BulletinBoardPage.FirstOrDefault(x => x.IdBulletinBoardPage == page.IdBulletinBoardPage);
                if (tmpPage == null)
                    return new JsonResult(new { successful = false, error = $"Page with ID {page.IdBulletinBoardPage} not exists!", page });

                tmpPage.Name = page.Name;
                //tmpPage.Duration = page.Duration;
            }
            context.SaveChanges();

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

        public IActionResult OnPostBoardPageLink(int idPage, string link)
        {
            var tmpPage = context.BulletinBoardPage.FirstOrDefault(x => x.IdBulletinBoardPage == idPage);
            if (tmpPage == null)
                return new JsonResult(new { successful = false, error = $"Page with ID {idPage} not exists!" });
            tmpPage.ExternalLink = link;
            context.SaveChanges();

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

        public IActionResult OnPostBoardPagePicture(IFormFile? file, int idPage)
        {
            if (file == null)
                return new JsonResult(new { successful = true, error = "No file!" });

            var tmpPage = context.BulletinBoardPage.FirstOrDefault(x => x.IdBulletinBoardPage == idPage);
            if (tmpPage == null)
                return new JsonResult(new { successful = false, error = $"Page with ID {idPage} not exists!" });

            var uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "bulletin-board-images/pages");
            var imageName = "StaticImage_" + idPage + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolder, imageName);

            if (System.IO.File.Exists(filePath))
                System.IO.File.Delete(filePath);

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                file.CopyTo(fileStream);
            }

            tmpPage.Image = imageName;
            context.SaveChanges();

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