using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using CaseAccountingDataModels.Enum;
using CaseAccountingProviderView.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace CaseAccountingProviderView.Controllers
{
	public class HomeController : Controller
	{
		private readonly ILogger<HomeController> _logger;

		public HomeController(ILogger<HomeController> logger)
		{
			_logger = logger;
		}

		public IActionResult Index()
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Login");
			}
			return View();
		}

		public IActionResult Login()
		{
			return View();
		}

		public IActionResult Registration()
		{
			return View();
		}

		[HttpPost]
		public void Login(string login, string password)
		{
			if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
			{
				throw new Exception("Введите логин и пароль");
			}
			APIUser.User = APIUser.GetRequest<UserViewModel>($"api/user/login?login={login}&password={password}&role={Role.Provider}");
			if (APIUser.User == null)
			{
				throw new Exception("Неверный логин/пароль");
			}
			Response.Redirect("Index");
		}

		[HttpPost]
		public void Registration(string login, string password)
		{
			if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
			{
				throw new Exception("Введите логин и пароль");
			}
			APIUser.PostRequest("api/user/register", new UserBindingModel
			{
				Login = login,
				Password = password,
				Role = Role.Provider
			});
			Response.Redirect("Login");
			return;
		}

		public IActionResult Cases()
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Enter");
			}
			/*if (page == 0)
			{
				page = 1;
			}*/
			ViewBag.Cases = APIUser.GetRequest<List<CaseViewModel>>
				($"api/case/getallbyuser?userId={APIUser.User.Id}");
			/*ViewBag.Page = page;
			ViewBag.NumberOfPages = APIUser.GetRequest<int>
				($"api/student/getnumberofpages?userId={APIUser.User.Id}");*/
			return View();
		}

		public IActionResult Deals()
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Deals = APIUser.GetRequest<List<DealViewModel>>
				($"api/deal/getallbyuser?userId={APIUser.User.Id}");
			return View();
		}

		public IActionResult Hearings()
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Hearings = APIUser.GetRequest<List<HearingViewModel>>
				($"api/hearing/getallbyuser?userId={APIUser.User.Id}");
			return View();
		}

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