using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using KendoUIInsert.Models;
using KendoUIInsert.Repository;

namespace KendoUIInsert.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            SetViewDataWithComponentsAndWorkPools();

            return View();
        }

        public JsonResult List([DataSourceRequest] DataSourceRequest request)
        {
            SetViewDataWithComponentsAndWorkPools();
            var modules = TaskRepository.GetModules();

            return Json(modules.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Create(TaskModuleDto module)
        {
            if (module != null && ModelState.IsValid)
            {
                SetViewDataWithComponentsAndWorkPools();
                TaskRepository.Create(module);
            }

            return Json(ModelState.ToDataSourceResult());
        }

        [HttpPost]
        public ActionResult Update([DataSourceRequest]DataSourceRequest request, TaskModuleDto module)
        {
            if (module != null && ModelState.IsValid)
            {
                TaskRepository.Update(module);
            }

            return Json(ModelState.ToDataSourceResult());
        }

        [HttpPost]
        public ActionResult Delete([DataSourceRequest]DataSourceRequest request, TaskModuleDto module)
        {
            if (module != null)
            {
                TaskRepository.Delete(module.Id);
            }

            return Json(ModelState.ToDataSourceResult());
        }

        
        private void SetViewDataWithComponentsAndWorkPools()
        {
            ViewData["ComponentClasses"] = TaskRepository.GetSharedClassesList();
            ViewData["WorkPools"] = TaskRepository.GetWorkPoolList();
        }

    }
}
