using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using CaseAccountingDataModels.Models;
using Microsoft.AspNetCore.Mvc;

namespace CaseAccountingCustomerView.Controllers
{
	public class Lawyers : Controller
	{
        public IActionResult Create()
        {
            if (APIUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }
            return View();
        }

        [HttpPost]
        public void Create([FromBody] LawyerBindingModel lawyerModel)
        {
            if (APIUser.User == null)
            {
                throw new Exception("403");
            }
            lawyerModel.UserId = APIUser.User.Id;
            lawyerModel.SpecializationId = null;
            var dict = new Dictionary<int, IContractModel>();
            foreach (var element in lawyerModel.ContractViewModels)
            {
                var contract = APIUser.GetRequest<ContractViewModel>($"api/contract/get?id={element.Id}");
                dict.Add(element.Id, contract);
            }
            lawyerModel.Contracts = dict;
            APIUser.PostRequest("api/lawyer/create", lawyerModel);
            Response.Redirect("/Home/Lawyers");
        }

        public IActionResult Update(int id)
        {
            if (APIUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Lawyer = APIUser.GetRequest<LawyerViewModel>($"api/lawyer/get?id={id}");
            return View();
        }

        [HttpPost]
        public void Update([FromBody] LawyerBindingModel lawyerModel)
        {
            if (APIUser.User == null)
            {
                throw new Exception("403");
            }
            lawyerModel.UserId = APIUser.User.Id;
            var dict = new Dictionary<int, ICaseModel>();
            foreach (var element in lawyerModel.CaseViewModels)
            {
                var _case = APIUser.GetRequest<CaseViewModel>($"api/case/get?id={element.Id}");
                dict.Add(element.Id, _case);
            }
            lawyerModel.Cases = dict;
            var dictContr = new Dictionary<int, IContractModel>();
            foreach (var element in lawyerModel.ContractViewModels)
            {
                var contract = APIUser.GetRequest<ContractViewModel>($"api/contract/get?id={element.Id}");
                dictContr.Add(element.Id, contract);
            }
            lawyerModel.Contracts = dictContr;
            APIUser.PostRequest("api/lawyer/update", lawyerModel);
            Response.Redirect("/Home/Lawyers");
        }

        [HttpPost]
        public void Delete(int id)
        {
            if (APIUser.User == null)
            {
                throw new Exception("403");
            }
            APIUser.PostRequest($"api/lawyer/delete", new LawyerBindingModel() { Id = id });
            Response.Redirect("/Home/Lawyers");
        }

        public List<LawyerViewModel> GetAllByUser()
        {
            if (APIUser.User == null)
            {
                return new();
            }
            List<LawyerViewModel> ? lawyers = APIUser.GetRequest<List<LawyerViewModel>>($"api/lawyer/getallbyuser?userId={APIUser.User.Id}");
            return lawyers ?? new();
        }

        public LawyerViewModel? Get(int id)
        {
            if (APIUser.User == null)
            {
                return new();
            }
            LawyerViewModel? lawyer = APIUser.GetRequest<LawyerViewModel>($"api/lawyer/get?id={id}");
            return lawyer;
        }

        public IActionResult Bind(int id)
        {
            if (APIUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Lawyer = APIUser.GetRequest<LawyerViewModel>($"api/lawyer/get?id={id}");
            return View();
        }

        [HttpPost]
        public void Bind([FromBody] LawyerBindingModel lawyerModel)
        {
            if (APIUser.User == null)
            {
                throw new Exception("403");
            }
            APIUser.PostRequest("api/lawyer/update", lawyerModel);
        }

        public List<LawyerViewModel> GetAllByUserAndSpecialization(int specialization)
        {
            if (APIUser.User == null)
            {
                return new();
            }
            List<LawyerViewModel>? lawyers = APIUser.GetRequest<List<LawyerViewModel>>($"api/lawyer/getallbyuserandspecialization?userId={APIUser.User.Id}&specialization={specialization}");
            return lawyers ?? new();
        }

        public List<CaseViewModel> GetAllCases()
        {
            if (APIUser.User == null)
            {
                return new();
            }
            List<CaseViewModel>? _case = APIUser.GetRequest<List<CaseViewModel>>("api/lawyer/GetAllCases");
            return _case ?? new();
        }

    }
}

