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

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

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

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

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

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

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

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

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

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

        [HttpPost]
        public List<ReportHearingLawyerViewModel>? GetReport([FromBody] ReportBindingModel reportModel)
        {
            if (APIUser.User == null)
            {
                return new();
            }
            reportModel.UserId = APIUser.User.Id;
            reportModel.UserEmail = APIUser.User.Login;
            List<ReportHearingLawyerViewModel>? list = APIUser.PostRequestWithResult<ReportBindingModel, List<ReportHearingLawyerViewModel>>
                ("api/reportprovider/getreportdata", reportModel);
            return list;
        }

        [HttpPost]
        public void SendByMailStatusReport([FromBody] ReportBindingModel reportModel)
        {
            if (APIUser.User == null)
            {
                return;
            }
            reportModel.UserId = APIUser.User.Id;
            reportModel.UserEmail = APIUser.User.Login;
            APIUser.PostRequest("api/reportprovider/sendbymailstatusreport", reportModel);
        }

        [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.Provider}");
			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.Provider
			});
			Response.Redirect("Login");
			return;
		}

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

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

		public IActionResult Hearings()
		{
			if (APIUser.User == null)
			{
				return Redirect("~/Home/Enter");
			}
			ViewBag.Hearings = APIUser.GetRequest<List<HearingViewModel>>
				($"api/hearing/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 });
		}
	}
}