using DressAtelierClientApp.Models;
using DressAtelierContracts.BindingModels;
using DressAtelierContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace DressAtelierClientApp.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 void Privacy(string login, string password, string fullname)
        {
            if (APIClient.Client == null) { throw new Exception("Only authorized clients are here."); }
            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fullname)) { throw new Exception("Enter login, password and fullname"); }
            APIClient.PostRequest("api/client/updatedata", 
                new ClientBindingModel 
                { 
                    ID = APIClient.Client.ID, 
                    FullName = fullname, 
                    Email = login, 
                    Password = password 
                }); 
            APIClient.Client.FullName = fullname; 
            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("Enter login and password"); }
            APIClient.Client = APIClient.GetRequest<ClientViewModel>($"api/client/login?login={login}&password={password}"); 
            if (APIClient.Client == null) { throw new Exception("Incorrect login/password"); }
            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("Enter login, password and fullname"); } 
            APIClient.PostRequest("api/client/register", new ClientBindingModel 
            { 
                FullName = fio, 
                Email = login, 
                Password = password 
            }); 
            Response.Redirect("Enter"); 
            return; 
        }

        [HttpGet] 
        public IActionResult Create() 
        { 
            ViewBag.Dresses = APIClient.GetRequest<List<DressViewModel>>("api/main/getdresslist"); 
            return View(); 
        }

        [HttpPost] 
        public void Create(int dress, int count) 
        { 
            if (APIClient.Client == null) { throw new Exception("Only authorized clients may be here"); } 
            if (count <= 0) { throw new Exception("Quantity and total must be over 0"); } 
            APIClient.PostRequest("api/main/createorder", new OrderBindingModel 
            { 
                ClientID = APIClient.Client.ID, 
                DressID = dress, 
                Count = count, 
                Sum = Calc(count, dress) 
            }); 
            Response.Redirect("Index"); 
        }

        [HttpPost] 
        public double Calc(int count, int dress) 
        { 
            var prod = APIClient.GetRequest<DressViewModel>($"api/main/getdress?dressID={dress}"); 
            return count * (prod?.Price ?? 1); 
        }
    }
}