using Microsoft.AspNetCore.Mvc;
using VetClinicAdminApp.Models;
using System.Diagnostics;
using VetClinicContracts.BindingModels;
using VetClinicContracts.ViewModels;


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

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

		public IActionResult Index()
		{
			if (APIAdmin.Admin == null)
			{
				return Redirect("~/Home/Enter");
			}
			return
View(APIAdmin.GetRequest<List<VisitViewModel>>($"api/visit/getvisits?vistId={APIAdmin.Admin.Id}"));

		}

		[HttpGet]
		public IActionResult Privacy()
		{
			if (APIAdmin.Admin == null)
			{
				return Redirect("~/Home/Enter");
			}
			return View(APIAdmin.Admin);
		}

		[HttpPost]
		public void Privacy(string login, string password, string fio)
		{
			if (APIAdmin.Admin == null)
			{
				throw new Exception("Âû êàê ñóäà ïîïàëè? Ñóäà âõîä òîëüêî àâòîðèçîâàííûì");
			}
			if (string.IsNullOrEmpty(login) ||
			string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
			{
				throw new Exception("Ââåäèòå email, ïàðîëü è ÔÈÎ");
			}
			APIAdmin.PostRequest("api/admin/updatedata", new
			AdminBindingModel
			{
				Id = APIAdmin.Admin.Id,
				AdminFIO = fio,
				Email = login,
				Password = password
			});
			APIAdmin.Admin.AdminFIO = fio;
			APIAdmin.Admin.Email = login;
			APIAdmin.Admin.Password = password;
			Response.Redirect("Index");
		}

		[HttpGet]
		public IActionResult Enter()
		{
			return View();
		}
		[HttpPost]
		public void Enter(string login, string password)
		{
			if (string.IsNullOrEmpty(login) ||
			string.IsNullOrEmpty(password))
			{
				throw new Exception("Ââåäèòå email è ïàðîëü");
			}
			APIAdmin.Admin =
			APIAdmin.GetRequest<AdminViewModel>($"api/admin/login?login={login}&password={password}");
			if (APIAdmin.Admin == null)
			{
				throw new Exception("Íåâåðíûé ëîãèí/ïàðîëü");
			}
			Response.Redirect("Index");
		}
		[HttpGet]
		public IActionResult Register()
		{
			return View();
		}
		[HttpPost]
		public void Register(string login, string password, string fio)
		{
			if (string.IsNullOrEmpty(login) ||
			string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
			{
				throw new Exception("Ââåäèòå ëîãèí, ïàðîëü è ÔÈÎ");
			}
			APIAdmin.PostRequest("api/admin/register", new
			AdminBindingModel
			{
				AdminFIO = fio,
				Email = login,
				Password = password
			});
			Response.Redirect("Enter");
			return;
		}

		public IActionResult Create()
		{
			if (APIAdmin.Admin == null)
			{
				return Redirect("~/Home/Enter");
			}
			return View();
		}



		[HttpPost]
		public void CreateVisit(string name, DateTime visitdate)
		{
			if (APIAdmin.Admin == null)
			{
				throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
			}
			if (string.IsNullOrEmpty(name))
			{
				throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
			}
			APIAdmin.PostRequest("api/visit/createvisit", new VisitBindingModel
			{
				NameVisit = name,
				DateVisit = visitdate,
				AdminId = APIAdmin.Admin.Id
			});
			Response.Redirect("Index");
		}

		public IActionResult DeleteMedicine()
		{
			if (APIAdmin.Admin == null)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Visits = APIAdmin.GetRequest<List<VisitViewModel>>("api/visit/getvisits");
			return View();
		}

		[HttpPost]
		public void DeleteVisit(int shop)
		{
			if (APIAdmin.Admin == null)
			{
				throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
			}
			APIAdmin.PostRequest("api/visit/deletevisit", new VisitBindingModel
			{
				Id = shop
			});
			Response.Redirect("Index");
		}

		public IActionResult Update()
		{
			if (APIAdmin.Admin == null)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Medicines = APIAdmin.GetRequest<List<MedicineViewModel>>("api/shop/getvisits");
			return View();
		}

		[HttpPost]
		public void Update(int visit, string name, DateTime vistdate)
		{
			if (APIAdmin.Admin == null)
			{
				throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
			}
			if (string.IsNullOrEmpty(name) )
			{
				throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
			}
			APIAdmin.PostRequest("api/visit/updatevisit", new VisitBindingModel
			{
				Id = visit,
				NameVisit = name,
				DateVisit = vistdate,
			});
			Response.Redirect("Index");
		}

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

		[HttpGet]
		public Tuple<VisitViewModel, string>? GetMedicine(int visitId)
		{
			if (APIAdmin.Admin == null)
			{
				throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
			}
			var result = APIAdmin.GetRequest<Tuple<VisitViewModel, List<string>>>($"api/visit/getvisit?visitid={visitId}");
			if (result == null)
			{
				return default;
			}
			string table = "";
			result.Item1.VisitAnimals.Clear();
			for (int i = 0; i < result.Item2.Count; i++)
			{
				var animal = result.Item2[i];
				table += "<tr>";
				table += $"<td>{animal}</td>";
				table += "</tr>";
			}
			return Tuple.Create(result.Item1, table);
		}
	}
}
