using BarClientApp.Models;
using BarContracts.BindingModels;
using BarContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace BarClientApp.Controllers
{
	public class HomeController : Controller
	{
		private readonly ILogger<HomeController> _logger;
		
		public HomeController(ILogger<HomeController> Logger)
		{
			_logger = Logger;
		}
		
		public IActionResult Index()
		{
			if (ApiClient.Client == null)
			{
				return Redirect("~/Home/Enter");
			}
			
			return View(ApiClient.GetRequest<List<OrderViewModel>>($"api/main/getorders?clientId={ApiClient.Client.Id}"));
		}
		
		[HttpGet]
		public IActionResult Privacy()
		{
			if (ApiClient.Client == null)
			{
				return Redirect("~/Home/Enter");
			}
			
			return View(ApiClient.Client);
		}
		
		[HttpPost]
		public void Privacy(string login, string password, string fio)
		{
			if (ApiClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			
			if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
			{
				throw new Exception("Введите логин, пароль и ФИО");
			}
			
			ApiClient.PostRequest("api/client/updatedata", new ClientBindingModel
			{
				Id = ApiClient.Client.Id,
				ClientFIO = fio,
				Email = login,
				Password = password
			});
			
			ApiClient.Client.ClientFIO = fio;
			ApiClient.Client.Email = login;
			ApiClient.Client.Password = password;
			
			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 IActionResult Enter()
		{
			return View();
		}
		
		[HttpPost]
		public void Enter(string login, string password)
		{
			if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
			{
				throw new Exception("Введите логин и пароль");
			}
			
			ApiClient.Client = ApiClient.GetRequest<ClientViewModel>($"api/client/login?login={login}&password={password}");
			
			if (ApiClient.Client == 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("Введите логин, пароль и ФИО");
			}
			
			ApiClient.PostRequest("api/client/register", new ClientBindingModel
			{
				ClientFIO = fio,
				Email = login,
				Password = password
			});
			
			Response.Redirect("Enter");
			return;
		}
		
		[HttpGet]
		public IActionResult Create()
		{
			ViewBag.Cocktails = ApiClient.GetRequest<List<CocktailViewModel>>("api/main/getcocktaillist");
			return View();
		}
		
		[HttpPost]
		public void Create(int cocktail, int count)
		{
			if (ApiClient.Client == null)
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");

			if (count <= 0)
				throw new Exception("Количество и сумма должны быть больше 0");

			
			ApiClient.PostRequest("api/main/createorder", new OrderBindingModel
			{
				ClientId = ApiClient.Client.Id,
				CocktailId = cocktail,
				Count = count,
				Sum = Calc(count, cocktail)
			});
			
			Response.Redirect("Index");
		}
		
		[HttpPost]
		public double Calc(int count, int cocktail)
		{
			CocktailViewModel? Cocktail = ApiClient.GetRequest<CocktailViewModel>($"api/main/getcocktail?cocktailId={cocktail}");
			return count * (Cocktail?.Price ?? 1);
		}

		[HttpGet]
		public IActionResult Mails()
		{
			if (ApiClient.Client == null)
			{
				return Redirect("~/Home/Enter");
			}

			return View(ApiClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={ApiClient.Client.Id}"));
		}
	}
}
