using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using CaseAccountingCustomerView.Models;
using CaseAccountingDataModels.Enum;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace CaseAccountingCustomerView.Controllers
{
    public class HomeController : Controller
    {
        public HomeController() { }

        public IActionResult Index()
        {
            if (APIUser.User == null)
            {
                return Redirect("~/Home/Login");
            }
            return View();
        }

        public IActionResult Login()
        {
            return View();
        }

        public IActionResult Registration()
        {
            return View();
        }

        [HttpPost]
        public void Login(string login, string password)
        {
            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
            {
                throw new Exception("Введите логин и пароль");
            }
            APIUser.User = APIUser.GetRequest<UserViewModel>($"api/user/login?login={login}&password={password}&role={Role.Customer}");
            if (APIUser.User == null)
            {
                throw new Exception("Неверный логин/пароль");
            }
            Response.Redirect("Index");
        }

        [HttpPost]
        public void Registration(string login, string password)
        {
            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
            {
                throw new Exception("Введите логин и пароль");
            }
            APIUser.PostRequest("api/user/register", new UserBindingModel
            {
                Login = login,
                Password = password,
                Role = Role.Customer
			});
            Response.Redirect("Login");
            return;
        }

        public IActionResult Lawyers( )
        {
            if (APIUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Lawyers = APIUser.GetRequest<List<LawyerViewModel>>
                ($"api/lawyer/getallbyuser?userId={APIUser.User.Id}");
            return View();
        }

        public IActionResult Specializations()
        {
            if (APIUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Specializations = APIUser.GetRequest<List<SpecializationViewModel>>
                ($"api/specialization/getallbyuser?userId={APIUser.User.Id}");
            return View();
        }

        public IActionResult Contracts()
        {
            if (APIUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Contracts = APIUser.GetRequest<List<ContractViewModel>>
                ($"api/contract/getallbyuser?userId={APIUser.User.Id}");
            return View();
        }

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

        [HttpPost]
        public int[]? HearingLawyerList([FromBody] LawyerHearingListBindingModel listModel)
        {
            if (APIUser.User == null)
            {
                return Array.Empty<int>();
            }
            byte[]? file = APIUser.PostRequestWithResult<LawyerHearingListBindingModel, byte[]>
                ("api/reportcustomer/lawyerhearinglist", listModel);
            return file!.Select(b => (int)b).ToArray();
        }

        public IActionResult HearingLawyerList()
        {
            return View();
        }
    }
}