using LinqToDB;
using MediaManager.database;
using MediaManager.database.Tables;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace MediaManager.Pages;

public class EditShow : PageModel {
	public User AuthorizedUser;

	public void OnGet() {
		using var db     = new Database.DbConn();
		var       showId = int.Parse(RouteData.Values["id"]!.ToString()!);
		var       show   = db.Shows.First(p => p.ShowId == showId);

		AuthorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db))!;

		if (show.UserId != AuthorizedUser.UserId)
			Response.Redirect("/");
	}

	public void OnPost() {
		using var db     = new Database.DbConn();
		var       showId = int.Parse(RouteData.Values["id"]!.ToString()!);
		var       show   = db.Shows.First(p => p.ShowId == showId);

		AuthorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db))!;

		if (show.UserId != AuthorizedUser.UserId) {
			Response.Redirect("/");
			return;
		}

		if (Request.Form["action"] == "delete") {
			db.Delete(show);
			Response.Redirect("/Shows");
			return;
		}

		if (Request.Form["action"] == "autoinc") {
			if (show.WatchStatus == WatchStatus.Finished) {
				Response.Redirect("/Shows");
				return;
			}

			show.LastSeen = DateTime.Now;
			show.SeenEpisodes++;

			if (show.SeenEpisodes > show.TotalEpisodes)
				show.TotalEpisodes = show.SeenEpisodes;

			show.WatchStatus = show.WatchStatus switch {
				WatchStatus.Waiting   => WatchStatus.FirstWatch,
				WatchStatus.Unwatched => WatchStatus.FirstWatch,
				_                     => show.WatchStatus
			};

			db.Update(show);
			Response.Redirect($"/Shows#show_{show.ShowId}");
			return;
		}

		if (Request.Form["action"] == "rewatch") {
			show.SeenEpisodes = 0;
			show.WatchStatus  = WatchStatus.Rewatch;

			db.Update(show);
			Response.Redirect($"/Shows#show_{show.ShowId}");
			return;
		}

		if (Request.Form["action"] == "finish") {
			show.WatchStatus = WatchStatus.Finished;
			show.WatchCount++;
			db.Update(show);
			Response.Redirect($"/Shows#show_{show.ShowId}");
			return;
		}

		if (Request.Form["action"] == "waiting") {
			show.WatchStatus = WatchStatus.Waiting;
			db.Update(show);
			Response.Redirect($"/Shows#show_{show.ShowId}");
			return;
		}

		show.Title          = Request.Form["title"];
		show.Year           = int.Parse(Request.Form["year"]);
		show.WatchCount     = int.Parse(Request.Form["watchcount"]);
		show.Rating         = int.Parse(Request.Form["rating"]);
		show.Rewatchability = int.Parse(Request.Form["rewatchability"]);
		show.LastSeen       = string.IsNullOrEmpty(Request.Form["lastwatch"]) ? DateTime.MinValue : DateTime.Parse(Request.Form["lastwatch"]);
		show.WatchStatus    = (WatchStatus)int.Parse(Request.Form["status"]);
		show.SeenEpisodes   = int.Parse(Request.Form["seeneps"]);
		show.TotalEpisodes  = int.Parse(Request.Form["totaleps"]);
		show.Comment        = Request.Form["comment"];

		if (show.SeenEpisodes >= show.TotalEpisodes) {
			show.SeenEpisodes = show.TotalEpisodes;
			if (show.WatchStatus != WatchStatus.Waiting)
				show.WatchStatus = WatchStatus.Finished;
		}

		db.Update(show);
		Response.Redirect($"/Shows#show_{show.ShowId}");
	}
}
