using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Scheduler_CustomEditor.Models;

namespace Scheduler_CustomEditor.Controllers
{
    public class SchedulerController : Controller
    {
        private List<MeetingViewModel> GetData()
        {
            var url = "https://demos.telerik.com/kendo-ui/service/meetings";
            var client = new WebClient();
            var content = client.DownloadString(url);
            content = content.Substring(9, content.Length - 10);
            List<MeetingViewModel> data = JsonConvert.DeserializeObject<List<MeetingViewModel>>(content);
            return data;
        }
        [HttpGet]
        public virtual JsonResult Read()
        {
            List<MeetingViewModel> data = GetData();
            return Json(data);

        }
        [HttpPost]
        public virtual JsonResult Destroy(MeetingViewModel task)
        {
            if (ModelState.IsValid)
            {
                //... handle delete of task
            }

            return Json(task);
        }
        [HttpPost]
        public virtual JsonResult Create(IEnumerable<MeetingViewModel> models)
        {
            if (ModelState.IsValid)
            {
                //... handle create of task
            }

            return Json(models);
        }
        [HttpPost]
        public virtual JsonResult Update(MeetingViewModel task)
        {
            if (ModelState.IsValid)
            {
                //... handle update of task
            }

            return Json(task);
        }
        [HttpGet]
        public JsonResult GetTrades(string text)
        {
            var data = Enumerable.Range(1, 5).Select(x => new
            {
                Text = "Trade "+ x,
                Value = x,
                Color = "\\#6eb3fa"
            });

            if (!string.IsNullOrEmpty(text))
            {
                data = data.Where(p => p.Text.Contains(text));
            }

            return Json(data);
        }
        [HttpGet]
        public JsonResult GetPersons(int? tradeId)
        {
            var data = Enumerable.Range(1, 100).Select(x => new
            {
                Text = "Person " + x,
                Value = x,
                Color = "\\#6eb3fa"
            });

            if (tradeId != null)
            {
                data = data.Where(p => p.Value % tradeId == 0);
            }

            return Json(data);
        }
    }
}