using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using TravelExpenses.Models;

namespace TravelExpenses.Pages.TravelOrders
{
    public class CreateModel : PageModel
    {
        public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request)
        {
            OnGet();
            //ToDestinations = ToDestinations.ToList();
            return new JsonResult(ToDestinations.ToDataSourceResult(request));
        }

        public JsonResult OnGetTravelTypes()
        {
            OnGet();
            //TravelTypes = TravelTypes.ToList();
            return new JsonResult(TravelTypes);
        }

        public JsonResult OnGetCountries(int travelTypeId)
        {
            OnGet();
            //Countries = Countries.ToList();
            if (travelTypeId > 0)
            {
                Destinations = Destinations.Where(d => d.TravelTypeId == travelTypeId).ToList();
                int[] countries = Destinations.Select(d => d.CountryId).ToArray();
                Countries = Countries.Where(c => countries.Contains(c.CountryId)).ToList();
            }
            return new JsonResult(Countries);
        }

        public JsonResult OnGetPlaces(int countryId)
        {
            OnGet();
            //Places = Places.ToList();
            if (countryId > 0)
            {
                Places = Places.Where(p => p.CountryId == countryId).ToList();
            }
            return new JsonResult(Places);
        }

        public JsonResult OnGetCenters(int placeId)
        {
            OnGet();
            //Centers = Centers.ToList();
            if (placeId > 0)
            {
                Centers = Centers.Where(c =>  c.PlaceId == placeId).ToList();
            }
            return new JsonResult(Centers);
        }

        public JsonResult OnPostCreate([DataSourceRequest] DataSourceRequest request, ToDestination toDestination)
        {
            //ToDestinations = ToDestinations.ToList();

            if (ToDestinations.Any())
            {
                int lastId = ToDestinations.Max(td => td.ToDestinationId);
                toDestination.ToDestinationId = lastId + 1;
            }
            else
            {
                toDestination.ToDestinationId = 1;
            }

            ToDestinations.Add(toDestination);

            return new JsonResult(new[] { toDestination }.ToDataSourceResult(request, ModelState));
        }

        public JsonResult OnPostUpdate([DataSourceRequest] DataSourceRequest request, ToDestination toDestination)
        {
            //ToDestinations = ToDestinations.ToList();
            ToDestinations.Where(x => x.ToDestinationId == toDestination.ToDestinationId).Select(x => toDestination);

            return new JsonResult(new[] { toDestination }.ToDataSourceResult(request, ModelState));
        }

        public JsonResult OnPostDestroy([DataSourceRequest] DataSourceRequest request, ToDestination toDestination)
        {
            ToDestinations.Remove(ToDestinations.FirstOrDefault(x => x.ToDestinationId == toDestination.ToDestinationId));

            return new JsonResult(new[] { toDestination }.ToDataSourceResult(request, ModelState));
        }

        public List<Destination> Destinations { get; set; }
        public List<ToDestination> ToDestinations { get; set; }
        public List<TravelType> TravelTypes { get; set; }
        public List<Country> Countries { get; set; }
        public List<Place> Places { get; set; }
        public List<Center> Centers { get; set; }

        public void OnGet()
        {
            TravelTypes = new List<TravelType>
            { new TravelType { TravelTypeId = 1, TravelTypeName = "Regular" } };

            Countries = new List<Country>
            { new Country { CountryId = 1, CountryName = "Croatia" } };

            Places = new List<Place>
            { new Place { PlaceId = 1, PlaceName = "Zagreb", AreaCode = "10000", CountryId = 1 } };

            Centers = new List<Center>
            { new Center { CenterId = 1, CenterName = "J", CenterAddress = "J 31", PlaceId = 1 } };

            Destinations = new List<Destination>
            { new Destination { DestinationId = 1, DestinationName = "Reg. Cro.", TravelTypeId = 1, CountryId = 1, DailyAmount = 100, CurrencyId = 1 } };

            ToDestinations = new List<ToDestination>
            { new ToDestination { DestinationId = 1, CenterId = 1, PlaceId = 1, CountryId = 1, TravelTypeId = 1 } };
        }
    }
}
