using IVM.Models;
using IVM.Services.IVMAPIService;
using IVM.Services;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace IVM.Controllers
{
    public class AddressController : Controller
    {
        private readonly IvmApiService ivmApiService;

        public static EditModel EditAddressModel { get; internal set; }

        public static string getStateProvince 
        { get { string retval = StatesProvinces.StateProvince.Where(x => x.countryCode == EditAddressModel.countryCode && x.code == EditAddressModel.stateProvince).ToList()[0].name;
                return retval; } }

        public JsonResult GetStateList(string? Country)
        {
            JsonResult? retval = null;
            if (!string.IsNullOrEmpty(Country))
            {
                var Country_ID = CountryModel.Countries.Where(s => s.name == Country).ToList()[0].code;
                var State = StatesProvinces.StateProvince.Where(s => s.countryCode == Country_ID);
                retval = Json(State.Select(s => new { id = s.id, name = s.name }).ToList());
            }
            else
                retval = Json(StatesProvinces.StateProvince);

            return retval;
        }

        public AddressController(IvmApiService _ivmApiService)
        {
            ivmApiService = _ivmApiService;
        }

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

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

        //[AcceptVerbs(HttpVerbs.Post)]
        public IActionResult UpdateAddress([DataSourceRequest] DataSourceRequest request)
        {
            string Response = ivmApiService.UpdateAddress(HttpContext.Request.Form);
            return Json(Response);
        }

        public IActionResult CreateAddress([DataSourceRequest] DataSourceRequest request)
        {
            string Response = ivmApiService.CreateAddress(HttpContext.Request.Form);
            return View();
        }

        public IActionResult getTypes([DataSourceRequest] DataSourceRequest request)
        {
            return Json(AddressTypes.addressTypes.ToDataSourceResult(request));
        }

        public JsonResult GetCountries() { return Json(CountryModel.Countries); }

        public IActionResult getEditForm(int Id)
        {
            EditAddressModel = ivmApiService.getEditRecord(Id);
            IvmApiService.AddressTopId = Id;
            ViewData = ivmApiService.GetTableCounts(ViewData, new string[] { "Machines", "Vendors", "Locations", "Clients" });
            return PartialView("~/Views/Address/EditForm.cshtml");
        }
        public IActionResult LoadPane3()
        {
            return PartialView("~/Views/Address/EditPane3.cshtml");
        }
        public IActionResult LoadPane2()
        {
            return PartialView("~/Views/Address/EditPane2.cshtml");
        }

        public IActionResult LoadPane1()
        {
            return PartialView("~/Views/Address/EditPane1.cshtml");
        }
        public IActionResult getAddressEdit(int Id)
        {
            EditModel eM = ivmApiService.getAddressEdit(Id);
            return Json(eM);
        }

        public IActionResult getSearchForm()
        {
          return PartialView($"~/Views/Address/SearchForm.cshtml");
        }

        public IActionResult GetAddress([DataSourceRequest] DataSourceRequest request)
        {
            object response = ivmApiService.GetAddress(request, new JsonHelper() { JsonString = "{'Data', 'Array'}" } );
            return Json(response);
        }

        public IActionResult DeleteAddress([DataSourceRequest] DataSourceRequest request)
        {
            var responseForm = HttpContext.Request.Form;
            ivmApiService.DeleteAddress(responseForm["id"].ToString());
            return Json("Success");
        }

        public IActionResult SubmitAddressSearch([DataSourceRequest] DataSourceRequest request)
        {
            object response = ivmApiService.GetAddress(HttpContext.Request.Query);
            return Json(response);
        }

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