using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using NestedGrids.Models;

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

        public ActionResult About()
        {
            return View();
        }

        private IEnumerable<Talonario> CreateTalonario(int id) 
        {
            return Enumerable.Range(0, 10).Select(i => new Talonario { 
                ID = i,
                ID_ENTREGA_TALONARIO = id,
                NUMERO_FINAL = i,
                NUMERO_INICIAL = i
            });
        }

        private IEnumerable<EntregaTalonario> CreateEntregaTalonario()
        {
            return Enumerable.Range(0, 20).Select(i => new EntregaTalonario
            {
                ID = i,
                NOMBRE_RECIBE = "recibe " + i,
                SERIE = "serie " + i,
                Talonarios = CreateTalonario(i)
            });
        }

        [GridAction]
        public ActionResult Select()
        {
            return View(new GridModel(CreateEntregaTalonario()));
        }

        [GridAction]
        public ActionResult SelectChild(int ID_ENTREGA_TALONARIO)
        {
            return View(new GridModel(CreateTalonario(ID_ENTREGA_TALONARIO)));
        }

        [GridAction]
        public ActionResult SaveChanges()
        {
            return View(new GridModel(CreateEntregaTalonario()));
        }

        public ActionResult CheckInterval(int? NUMERO_INICIAL, int? NUMERO_FINAL)
        {
            return Json(NUMERO_INICIAL > 2, JsonRequestBehavior.AllowGet);
        }
    }
}
