using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using StoreKeeperClient.Models;
using ComputerHardwareStoreContracts.BindingModels;

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

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

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

			return View(APIClient.GetRequest<List<PurchaseViewModel>>($"api/main/getpurchases?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 StoreKeeperBindingModel
			{
				Id = APIClient.Client.Id,
				Name = fio,
				Login = login,
				Password = password
			});
			APIClient.Client.Name = fio;
			APIClient.Client.Login = login;
			APIClient.Client.Password = password;
			Response.Redirect("Index");
		}

		[HttpGet]
        public IActionResult Enter()
        {
            return View();
        }

        [HttpGet]
        public IActionResult Register()
        {

            return View();
        }

        [HttpGet]
        public IActionResult AddBuildToPurchase()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult Builds()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View(new List<BuildViewModel>());
        }

        [HttpGet]
        public IActionResult BuildCreate()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult BuildDelete()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult BuildUpdate()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult CommentCreate()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult CommentDelete()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult CommentUpdate()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult Comments()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View(new List<CommentViewModel>());
        }

        [HttpGet]
        public IActionResult ProductsList()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View(new List<BuildViewModel>());
        }

        [HttpGet]
        public IActionResult PurchaseCreate()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult PurchaseDelete()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult Purchases()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View(new List<PurchaseViewModel>());
        }

        [HttpGet]
        public IActionResult PurchaseUpdate()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View();
        }

        [HttpGet]
        public IActionResult Report()
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			return View(new List<PurchaseViewModel>());
        }

        [HttpPost]
        public void Report(string password)
        {
			if (APIClient.Client == null)
			{
				throw new Exception("Вы как суда попали? Суда вход только авторизованным");
			}
			Response.Redirect("ReportOnly");
        }

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