using Microsoft.AspNetCore.Mvc;
using SushiBarClientApi.Models;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc.Rendering;
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;

namespace SushiBarClientApi.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}"));
        }

        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("Enter with auth");
            }
            if (string.IsNullOrEmpty(login) ||
                string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
            {
                throw new Exception("Enter login, password, fio");
            }
            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("Enter login and password");
            }
            APIClient.Client = APIClient.GetRequest<ClientViewModel>($"api/Client/Login?login={login}&password={password}");
            if (APIClient.Client == null)
            {
                throw new Exception("Invalid login or 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 and password");
            }
            APIClient.PostRequest("api/Client/Register", new
                ClientBindingModel
                {
                    ClientFio = fio,
                    Email = login,
                    Password = password
                });
            Response.Redirect("Enter");
        }
        
        [HttpGet]
        public IActionResult Create()
        {
            ViewBag.Sushi = new SelectList(
                APIClient.GetRequest<List<SushiViewModel>>("api/Main/GetProductList"), 
                "Id", 
                "SushiName");
            return View();
        }
        
        [HttpPost]
        public void Create(int sushi, int count)
        {
            if (APIClient.Client == null)
            {
                throw new Exception("Only with auth");
            }
            if (count <= 0)
            {
                throw new Exception("Count and sum must be more then zero");
            }
            APIClient.PostRequest("api/Main/CreateOrder", new
                OrderBindingModel
                {
                    ClientId = APIClient.Client.Id,
                    SushiId = sushi,
                    Count = count,
                    Sum = Calc(count, sushi)
                });
            Response.Redirect("Index");
        }

        [HttpPost]
        public double Calc(int count, int sushi)
        {
            var prod = APIClient.GetRequest<SushiViewModel>($"api/Main/GetProduct?sushiId={sushi}");
            return count * (prod?.Price ?? 1);
        }
    }
}