using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopShopApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace BlacksmithWorkshopShopApp.Controllers
{
	public class HomeController : Controller
	{
		private readonly ILogger<HomeController> _logger;
		public HomeController(ILogger<HomeController> logger)
		{
			_logger = logger;
		}
		public IActionResult Index()
		{
			if (ApiClient.Access == false)
			{
				return Redirect("~/Home/Enter");
			}
			return View(ApiClient.GetRequest<List<ShopViewModel>>($"api/shop/getshops"));

		}
		[HttpGet]
		public IActionResult Enter()
		{
			return View();
		}
		[HttpPost]
		public void Enter(string password)
		{
			if (string.IsNullOrEmpty(password))
			{
				throw new Exception("Введите пароль");
			}
			ApiClient.CheckPassword(password);

			if (ApiClient.Access == false)
			{
				throw new Exception("Неправильный пароль");
			}
			Response.Redirect("Index");
		}
		public IActionResult Create()
		{
			if (ApiClient.Access == false)
			{
				return Redirect("~/Home/Enter");
			}
			return View();
		}
		[HttpPost]
		public void Create(string name, string address, DateTime dateOpen, int maxCapacity)
		{
			if (ApiClient.Access == false)
			{
				throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
			}
			if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(address)
				|| maxCapacity <= 0)
			{
				throw new Exception("Ошибка в введенных данных");
			}
			ApiClient.PostRequest("api/shop/createshop", new ShopBindingModel
			{
				ShopName = name,
				Address = address,
				MaxCapacity = maxCapacity,
				OpeningDate = dateOpen
			});
			Response.Redirect("Index");
		}
		public IActionResult Delete()
		{
			if (ApiClient.Access == false)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
			return View();
		}
		[HttpPost]
		public void Delete(int shop)
		{
			if (ApiClient.Access == false)
			{
				throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
			}
			ApiClient.PostRequest("api/shop/deleteshop", new ShopBindingModel
			{
				Id = shop
			});
			Response.Redirect("Index");
		}
		public IActionResult Update()
		{
			if (ApiClient.Access == false)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
			return View();
		}
		[HttpPost]
		public void Update(int shop, string name, string address, DateTime dateOpen, int maxCapacity)
		{
			if (ApiClient.Access == false)
			{
				throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
			}
			if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(address)
				|| maxCapacity <= 0)
			{
				throw new Exception("Ошибка в введенных данных");
			}
			ApiClient.PostRequest("api/shop/updateshop", new ShopBindingModel
			{
				Id = shop,
				ShopName = name,
				Address = address,
				OpeningDate = dateOpen,
				MaxCapacity = maxCapacity,
			});
			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<ShopViewModel, string>? GetShop(int shopId)
		{
			if (ApiClient.Access == false)
			{
				throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
			}
			var result = ApiClient.GetRequest<Tuple<ShopViewModel, List<Tuple<string, int>>>>($"api/shop/getshop?shopid={shopId}");
			if (result == null)
			{
				return default;
			}
			string table = "";
			result.Item1.ShopManufactures.Clear();
			for (int i = 0; i < result.Item2.Count; i++)
			{
				var manufacture = result.Item2[i].Item1;
				var count = result.Item2[i].Item2;
				table += "<tr>";
				table += $"<td>{manufacture}</td>";
				table += $"<td>{count}</td>";
				table += "</tr>";
			}
			return Tuple.Create(result.Item1, table);
		}
		public IActionResult MakeSupply()
		{
			if (ApiClient.Access == false)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
			ViewBag.ListManufacture = ApiClient.GetRequest<List<ManufactureViewModel>>("api/main/GetManufactureList");
			return View();
		}
		[HttpPost]
		public void MakeSupply(int shop, int manufacture, int count)
		{
			if (ApiClient.Access == false)
			{
				throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
			}
			ApiClient.PostRequest("api/shop/makesupply", Tuple.Create(
				new ShopSearchModel() { Id = shop },
				new ManufactureViewModel() { Id = manufacture },
				count
			));
			Response.Redirect("Index");
		}
	}
}
