using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace CaseAccountingProviderView.Controllers
{
	public class HearingController : Controller
	{
		public IActionResult Create()
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Enter");
			}
			return View();
		}

		[HttpPost]
		public void Create([FromBody] HearingBindingModel hearingModel)
		{
			if (APIUser.User == null)
			{
				throw new Exception("403");
			}
			hearingModel.UserId = APIUser.User.Id;
			APIUser.PostRequest("api/hearing/create", hearingModel);
			Response.Redirect("/Home/Hearings");
		}

		public IActionResult Update(int id)
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Hearing = APIUser.GetRequest<HearingViewModel>($"api/hearing/get?id={id}");
			return View();
		}

		[HttpPost]
		public void Update([FromBody] HearingBindingModel hearingModel)
		{
			if (APIUser.User == null)
			{
				throw new Exception("403");
			}
			hearingModel.UserId = APIUser.User.Id;
			APIUser.PostRequest("api/hearing/update", hearingModel);
			Response.Redirect("/Home/Hearings");
		}

		public IActionResult AddHearing(int id)
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.HearingId = id;
			return View();
		}

		[HttpPost]
		public void AddHearing([FromBody] HearingBindingModel hearingModel)
		{
			if (APIUser.User == null)
			{
				throw new Exception("403");
			}
			APIUser.PostRequest("api/hearing/update", hearingModel);
		}

		public IActionResult Bind(int id)
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Hearing = APIUser.GetRequest<HearingViewModel>($"api/hearing/get?id={id}");
			return View();
		}

		[HttpPost]
		public void Bind([FromBody] HearingBindingModel hearingModel)
		{
			if (APIUser.User == null)
			{
				throw new Exception("403");
			}
			APIUser.PostRequest("api/hearing/update", hearingModel);
		}

		[HttpPost]
		public void Delete(int id)
		{
			if (APIUser.User == null)
			{
				throw new Exception("403");
			}
			APIUser.PostRequest($"api/hearing/delete", new HearingBindingModel() { Id = id });
			Response.Redirect("/Home/Hearings");
		}

		public List<HearingViewModel> GetAllByUser()
		{
			if (APIUser.User == null)
			{
				return new();
			}
			List<HearingViewModel>? hearingModel = APIUser.GetRequest<List<HearingViewModel>>($"api/hearing/getallbyuser?userId={APIUser.User.Id}");
			return hearingModel ?? new();
		}

		public HearingViewModel? Get(int id)
		{
			if (APIUser.User == null)
			{
				return new();
			}
			HearingViewModel? hearingModel = APIUser.GetRequest<HearingViewModel>($"api/hearing/get?id={id}");
			return hearingModel;
		}
	}
}
