using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.WebUtilities;
using bitforum.Models;
using bitforum.Models.Page.Banner;
using bitforum.Services;

namespace bitforum.Controllers.Page.Banner
{
    [Authorize]
    [Route("Page/Banner")]
    public class ItemController : Controller
    {
        private readonly ILogger<ItemController> _logger;
        private readonly DefaultDbContext _db;
        private readonly FileUploadService _fileUploadService;
        private readonly string _IndexViewPath = "~/Views/Page/Banner/Item/Index.cshtml";
        private readonly string _WriteViewPath = "~/Views/Page/Banner/Item/Write.cshtml";
        private readonly string _EditViewPath = "~/Views/Page/Banner/Item/Edit.cshtml";
        private Dictionary<string, string>? _queryString = null;

        public ItemController(ILogger<ItemController> logger, DefaultDbContext db, FileUploadService fileUploadService)
        {
            _logger = logger;
            _db = db;
            _fileUploadService = fileUploadService;
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            ViewBag.QueryString = QueryHelpers.ParseQuery(HttpContext.Request.QueryString.Value).ToDictionary(k => k.Key, v => string.Join(",", v.Value));
            base.OnActionExecuting(context);
        }

        [HttpGet("Item/{positionID?}")]
        public IActionResult Index(int? positionID, [FromQuery] int page = 1)
        {
            ViewBag.positionID = positionID;

            // 위치 목록
            ViewBag.BannerPositions = _db.BannerPosition.Include(c => c.BannerItem).Where(c => c.IsActive).ToList();

            // 배너 목록
            var bannerItems = _db.BannerItem.Include(c => c.BannerPosition).OrderBy(c => c.Order).AsQueryable();
            if (positionID.HasValue)
            {
                bannerItems = bannerItems.Where(c => c.PositionID == positionID);
            }
            ViewBag.BannerItems = bannerItems.ToList();
            ViewBag.Total = bannerItems.Count();
            ViewBag.Pagination = new Pagination(ViewBag.Total, page, 20, null);

            return View(_IndexViewPath);
        }

        [HttpGet("Item/Write")]
        public IActionResult Write()
        {
            ViewBag.BannerPositions = _db.BannerPosition.Where(c => c.IsActive).ToList();

            return View(_WriteViewPath);
        }
        
        [HttpPost("Item/Create")]
        public async Task<IActionResult> Create(BannerItem request, IFormFile? Image)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("유효성 검사에 실패하였습니다.");
                }

                if (request.PositionID <= 0 || !_db.BannerPosition.Any(c => c.ID == request.PositionID))
                {
                    throw new Exception("유효한 위치를 선택하세요.");
                }

                // 이미지 저장
                request.Image = await _fileUploadService.UploadImageAsync(Image, UploadFolder.Banner);
                request.UpdatedAt = null;
                request.CreatedAt = DateTime.Now;

                _db.BannerItem.Add(request);

                int affectedRows = await _db.SaveChangesAsync();
                if (affectedRows <= 0)
                {
                    throw new Exception("배너 등록 중 오류가 발생했습니다.");
                }

                string message = "배너가 정상적으로 등록되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
                return Redirect("/Page/Banner/Item");
            }
            catch (ArgumentException e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
                return Write();
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
                return Write();
            }
        }

        [HttpGet("Item/{id}/Edit")]
        public async Task<IActionResult> Edit(int id)
        {
            try
            {
                if (id <= 0)
                {
                    throw new Exception("유효하지 않은 접근입니다.");
                }

                var bannerItem = await _db.BannerItem.FirstAsync(c => c.ID == id);
                if (bannerItem is null)
                {
                    throw new Exception("FAQ 정보를 찾을 수 없습니다.");
                }

                // 위치 목록
                ViewBag.BannerPositions = new SelectList(_db.BannerPosition.Where(c => c.IsActive).ToList(), "ID", "Subject", bannerItem.PositionID);

                return View(_EditViewPath, bannerItem);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
                return Redirect("/Page/Banner/Item");
            }
        }

        [HttpPost("Item/Update")]
        public async Task<IActionResult> Update(BannerItem request, IFormFile? Image, [FromForm] bool IsImageRemove = false)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("유효성 검사에 실패하였습니다.");
                }

                if (request.PositionID <= 0 || !_db.BannerPosition.Any(c => c.ID == request.PositionID))
                {
                    throw new Exception("유효한 분류를 선택하세요.");
                }

                if (request.EndAt < request.StartAt)
                {
                    throw new Exception("사용 기간을 확인해주세요.");
                }

                var bannerItem = await _db.BannerItem.FirstAsync(c => c.ID == request.ID);
                if (bannerItem is null)
                {
                    throw new Exception("배너 정보를 찾을 수 없습니다.");
                }

                // 이미지 저장
                if (IsImageRemove)
                {
                    // 실제 파일 삭제
                    _fileUploadService.RemoveFile(bannerItem.Image);
                    bannerItem.Image = null;
                }
                else if (Image is not null)
                {
                    bannerItem.Image = await _fileUploadService.UploadImageAsync(Image, UploadFolder.Banner);
                }

                bannerItem.PositionID = request.PositionID;
                bannerItem.Subject = request.Subject;
                bannerItem.Width = request.Width;
                bannerItem.Height = request.Height;
                bannerItem.Link = request.Link;
                bannerItem.Order = request.Order;
                bannerItem.IsActive = request.IsActive;
                bannerItem.StartAt = request.StartAt;
                bannerItem.EndAt = request.EndAt;
                bannerItem.UpdatedAt = DateTime.Now;

                _db.BannerItem.Update(bannerItem);

                int affectedRows = await _db.SaveChangesAsync();
                if (affectedRows <= 0)
                {
                    throw new Exception("배너 수정 중 오류가 발생했습니다.");
                }

                string message = "배너가 정상적으로 수정되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
                return Redirect($"/Page/Banner/Item/{request.ID}/Edit");
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
                return await Edit(request.ID);
            }
        }

        [HttpGet("Item/{id}/Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                if (id <= 0)
                {
                    throw new Exception("유효하지 않은 접근입니다.");
                }

                var bannerItem = await _db.BannerItem.FindAsync(id);
                if (bannerItem == null)
                {
                    throw new Exception("배너 정보를 찾을 수 없습니다.");
                }

                _fileUploadService.RemoveFile(bannerItem.Image);
                _db.BannerItem.Remove(bannerItem);

                int affectedRows = await _db.SaveChangesAsync();
                if (affectedRows <= 0)
                {
                    throw new Exception("배너 삭제 중 오류가 발생했습니다.");
                }

                string message = "배너가 정상적으로 삭제되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
            }

            return Redirect("/Page/Banner/Item");
        }

        [HttpPost("Item/Delete")]
        public async Task<IActionResult> Delete([FromForm] int[] ids)
        {
            try
            {
                if (ids == null || ids.Length <= 0)
                {
                    throw new Exception("유효하지 않은 접근입니다.");
                }

                foreach (var id in ids) 
                {
                    var bannerItem = await _db.BannerItem.FindAsync(id);
                    if (bannerItem == null)
                    {
                        throw new Exception("배너 정보를 찾을 수 없습니다.");
                    }

                    _fileUploadService.RemoveFile(bannerItem.Image);
                    _db.BannerItem.Remove(bannerItem);

                    int affectedRows = await _db.SaveChangesAsync();
                    if (affectedRows <= 0)
                    {
                        throw new Exception($"{id}번호의 배너 삭제 중 오류가 발생했습니다.");
                    }
                }

                string message = "배너가 정상적으로 삭제되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
            }

            return Redirect("/Page/Banner/Item");
        }
    }
}
