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

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

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

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

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

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

		[HttpPost]
		public void AddCase([FromBody] CaseBindingModel caseModel)
		{
			if (APIUser.User == null)
			{
				throw new Exception("403");
			}
			APIUser.PostRequest("api/case/update", caseModel);
		}

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

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

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

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

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

		public List<SpecializationViewModel> GetAllSpecializations()
		{
			if (APIUser.User == null)
			{
				return new();
			}
			List<SpecializationViewModel>? specializationModel = APIUser.GetRequest<List<SpecializationViewModel>>($"api/case/getallspecializations");
			return specializationModel ?? new();
		}

        public List<SpecializationCasesViewModel> GetSpecializationCases()
        {
            if (APIUser.User == null)
            {
                return new();
            }
            List<SpecializationCasesViewModel>? specializationCases = APIUser.GetRequest<List<SpecializationCasesViewModel>>($"api/case/getspecializationcases");
            return specializationCases ?? new();
        }
    }
}
