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

namespace EditGrid.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Edit Energy Yield";

            return View();
        }

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

        public ActionResult ReadMonthYield([DataSourceRequest]DataSourceRequest request, DateTime date)
        {
            var yieldGrid = new MonthlyYieldModel();

            DataSourceResult result = yieldGrid.Yield.ToDataSourceResult(request);

            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateMonthYield([DataSourceRequest] DataSourceRequest request,
                                             [Bind(Prefix = "models")] IEnumerable<DatedEnergy> yields)
        {
            if (yields != null && ModelState.IsValid)
            {
                foreach (var yield in yields)
                {
                    var item = DataStore.Yield.FirstOrDefault(x => x.Date == yield.Date);
                    if (item != null) item.Etoday = yield.Etoday;
                }
            }
            return Json(yields.ToDataSourceResult(request, ModelState));
        }


    }
}
