using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SyncfusionMvcApplication40;
using SyncfusionMvcApplication40.Grid;
using MVCSampleBrowser.Models;
using Syncfusion.JavaScript;
using Syncfusion.JavaScript.DataSources;
using System.Collections;
using SyncfusionMvcApplication40.Models;
using System.Web.Script.Serialization;
using Syncfusion.EJ.Export;
using System.Reflection;
using Syncfusion.JavaScript.Models;
using Syncfusion.XlsIO;

namespace SyncfusionMvcApplication40.Controllers
{
    public class GridController : Controller
    {
        //
        // GET: /Grid/
        public class CarsList

        {

            public int UniqueKey { get; set; }

            public string Text { get; set; }

        }
       public static List<Orders> order = new List<Orders>();
        public ActionResult GridFeatures()       
        {
            return View();
        }
    
        private void BindDataSource()
        {
            int code = 10000;
            for (int i = 1; i < 10; i++)
            {
                order.Add(new Orders(code + 1, "ALFKI",1, 2.3 * i, new DateTime(1991, 05, 15), "Berlin",true));
                order.Add(new Orders(code + 2, "ANATR", 2, 3.3 * i, new DateTime(1990, 04, 04), "Madrid",false));
                order.Add(new Orders(code + 3, "ANTON", 2, 4.3 * i, new DateTime(1957, 11, 30), "Cholchester",false));
                order.Add(new Orders(code + 4, "BLONP", 1, 5.3 * i, new DateTime(1930, 10, 22), "Marseille",true));
                order.Add(new Orders(code + 5, "BOLID", 3, 6.3 * i, new DateTime(1953, 02, 18), "Tsawassen",false));
                code += 5;
            }         
        }
        public ActionResult UrlDataSource(DataManager dm)
        {
            BindDataSource();
            IEnumerable DataSource = order;
            DataOperations ds = new DataOperations();
            List<string> str = new List<string>();
            if (dm.Search != null && dm.Search.Count > 0)
            {
                DataSource = ds.PerformSearching(DataSource, dm.Search);
            }
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
            {
                DataSource = ds.PerformSorting(DataSource, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
                DataSource = ds.PerformWhereFilter(DataSource, dm.Where, dm.Where[0].Operator);
            }            
            var count = DataSource.Cast<Orders>().Count();
            DataSource = ds.PerformSkip(DataSource, dm.Skip);
            DataSource = ds.PerformTake(DataSource, dm.Take);
            return Json(new { result = DataSource, count = count});
        }

        [Serializable]
        public class Orders
        {
            public Orders()
            {

            }
            public Orders(long OrderId, string CustomerId, int EmployeeId, double Freight, DateTime OrderDate, string ShipCity, bool Verified)
            {
                this.OrderID = OrderId;
                this.CustomerID = CustomerId;
                this.EmployeeID = EmployeeId;
                this.Freight = Freight;
                this.OrderDate = OrderDate;
                this.ShipCity = ShipCity;
                this.Verified = Verified;
            }
            public long OrderID { get; set; }
            public string CustomerID { get; set; }
            public int EmployeeID { get; set; }
            public double Freight { get; set; }
            public DateTime OrderDate { get; set; }
            public string ShipCity { get; set; }
            public bool Verified { get; set; }
        }
        public ActionResult Update(Orders value)
        {
            Orders result = order.Where(o => o.OrderID == value.OrderID).FirstOrDefault();
            if (result != null)
            {
                result.OrderID = value.OrderID;
                result.OrderDate = value.OrderDate;
                result.CustomerID = value.CustomerID;
                result.EmployeeID = value.EmployeeID;
                result.Freight = value.Freight;
                result.ShipCity = value.ShipCity;
                result.Verified = value.Verified;


            }
            return Json(order, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Insert(Orders value)
        {
            order.Insert(0, value);
            return Json(order, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Delete(int key)
        {
            Orders result = order.Where(o => o.OrderID == key).FirstOrDefault();
            order.Remove(result);
            return Json(order, JsonRequestBehavior.AllowGet);
        }

    }
}