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

namespace Kendo_Editor_Demo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult KendoGrid()
        {
            ViewData["UserRoles"] = GetRoles();
            return View();
        }

        private MultiSelectList GetRoles()
        {
            List<SelectListItem> result = new List<SelectListItem>();

            result.Add(new SelectListItem() { Text = "Role 1", Value = "1" });
            result.Add(new SelectListItem() { Text = "Role 2", Value = "2" });
            result.Add(new SelectListItem() { Text = "Role 3", Value = "3" });
            result.Add(new SelectListItem() { Text = "Role 4", Value = "4" });
            result.Add(new SelectListItem() { Text = "Role 5", Value = "5" });

            return new MultiSelectList(result, "Value", "Text");
        }

        public ActionResult GetUsers([DataSourceRequest] DataSourceRequest request)
        {
            return Json(AssembleUsers().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        public ActionResult CreateUsers([DataSourceRequest] DataSourceRequest request, RegisterViewModel model)
        {
            return Json(new[] { model }.ToDataSourceResult(request, ModelState));
        }

        public ActionResult UpdateUser([DataSourceRequest] DataSourceRequest request, RegisterViewModel model)
        {
            return Json(new[] { model }.ToDataSourceResult(request, ModelState));
        }

        private List<RegisterViewModel> AssembleUsers()
        {
            var users = new List<RegisterViewModel>() {
                new RegisterViewModel() { UserName = "User 1", Roles = new List<string>(){ "1", "3" }},
                new RegisterViewModel() { UserName = "User 2", Roles = new List<string>(){ "3" }},
                new RegisterViewModel() { UserName = "User 2", Roles = new List<string>(){ "3", "4" }}
            };

            return users;
        }
    }
}