using System.Linq;
using System.Web.Mvc;
using System;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using System.Diagnostics;
using MvcKendoUiSample.Models;

namespace MvcKendoUiSample.Controllers
{
    public class CustomerController : Controller
    {


        public int CurrentPage
        {
            get
            {
                if (Request.QueryString["CustomerGrid-page"] == null)
                    return 0;
                else
                    return Convert.ToInt32(Request.QueryString["CustomerGrid-page"]);
            }
        }

        public int PageSize
        {
            get
            {
                if (Request.QueryString["CustomerGrid-pageSize"] == null)
                    return DEFAULT_PAGE_SIZE;
                else
                    return Convert.ToInt32(Request.QueryString["CustomerGrid-pageSize"]);
            }
        }

        private const int DEFAULT_PAGE_SIZE = 10;

        public ActionResult Index()
        {
            var customers = DataSource.Customers
                                        .Where(c => c.Status == Status.Active)
                                        .OrderBy(c => c.Name)
                                        .AsQueryable()
                                        .Page(CurrentPage, PageSize);


            return View(customers);
        }

        //
        // GET: /Customer/Details/5

        public ActionResult Details(string id)
        {

            // Load all projects for a customer including the customer object.
            var customers = DataSource.Customers
                                        .Single(c => c.Id == id);

            return View(customers);
        }

        //
        // GET: /Customer/Create

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

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                customer.Status = Status.Active;

                DataSource.Customers.Add(customer);

                return RedirectToAction("Index");
            }

            return View(customer);
        }

        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(string id)
        {
            var customer = DataSource.Customers.Single(c => c.Id == id);

            return View(customer);
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, Customer customer)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    DataSource.Store(customer);
                    return RedirectToAction("Index");
                }
                return View(customer);

            }
            catch
            {
                return View(customer);
            }
        }

        //
        // GET: /Customer/Delete/5

        public ActionResult Delete(string id)
        {
            return View();
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, Customer customer)
        {
            throw new NotImplementedException();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Customer_Update([DataSourceRequest] DataSourceRequest request, Customer customer)
        {
            if (customer != null && ModelState.IsValid)
            {
                Debug.WriteLine(customer.Name);
            }


            //Return any validation errors if any
            return Json(ModelState.ToDataSourceResult());


        }
    }
}
