using VetClinicAdminApp.Models;
using VetClinicContracts.BindingModels;
using VetClinicContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;


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

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

        public IActionResult Index()
        {
        if (APIAdmin.Admin == null)
     {
               return Redirect("~/Home/Enter");
            }

             return View(APIAdmin.GetRequest<List<VisitViewModel>>($"api/main/getorders?clientId={APIAdmin.Admin.Id}"));
    }

        [HttpGet]
        public IActionResult Privacy()
        {
            if (APIAdmin.Admin == null)
            {
                return Redirect("~/Home/Enter");
            }

            return View(APIAdmin.Admin);
        }

        [HttpPost]
        public void Privacy(string login, string password, string fio)
        {
            if (APIAdmin.Admin == null)
            {
                throw new Exception("Вы как сюда попали? Суда вход только авторизованным");
            }

            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
            {
                throw new Exception("Введите логин, пароль и ФИО");
            }

            APIAdmin.PostRequest("api/admin/updatedata", new AdminBindingModel
            {
                Id = APIAdmin.Admin.Id,
                AdminFIO = fio,
                Email = login,
                Password = password
            });

            APIAdmin.Admin.AdminFIO = fio;
            APIAdmin.Admin.Email = login;
            APIAdmin.Admin.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("Введите логин и пароль");
            }

            APIAdmin.Admin = APIAdmin.GetRequest<AdminViewModel>($"api/admin/login?login={login}&password={password}");

            if (APIAdmin.Admin == 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("Введите логин, пароль и ФИО");
            }

            APIAdmin.PostRequest("api/admin/register", new AdminBindingModel
            {
                AdminFIO = fio,
                Email = login,
                Password = password
            });

            Response.Redirect("Enter");

            return;
        }

       // [HttpGet]
       // public IActionResult Create()
       // {
        //    ViewBag.Snacks = APIAdmin.GetRequest<List<AnimalViewModel>>("api/main/getanimallist");

       //     return View();
       // }

        [HttpPost]
        public void Create(int animal)
        {
            if (APIAdmin.Admin == null)
            {
                throw new Exception("Вы как сюда попали? Суда вход только авторизованным");
            }

            //APIAdmin.PostRequest("api/main/createorder", new VisitBindingModel
            //{
            //    AdminId = APIAdmin.Admin.Id,
            //    AnimalId = animal,
           // });

            Response.Redirect("Index");
        }
    }
}