using System;
using System.Collections.Generic;
using System.Data;

using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CatalogSupport;
using CatalogSupport.Helper;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System.IO;
using System.Threading.Tasks;
using System.Net.Http;
using System.Text;
using System.Web.Configuration;
using Newtonsoft.Json;


using System.ComponentModel.DataAnnotations;

using CatalogSupport.Filters;

using CatalogSupport.ViewModels;
using Kendo.Mvc;

using System.Web.UI;
using System.Drawing;

using System.Net.Http.Headers;
using CatalogSupport.Models;

namespace CatalogSupport.Controllers
{
    public partial class ProductsController : Controller
    {

        #region Private Members

    


        //    public Dictionary<ProductType, IBaseService> serviceList;


        PageModel pageModel = new PageModel();
        #endregion Private Members

        #region Constructor

  
        public ProductsController()
        {
     
        }

        #endregion Constructor

        #region Public Methods
        // Get Products

        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public async Task<ActionResult> GetProducts([DataSourceRequest]DataSourceRequest request)
        {
            List<ProductModel> Products = new List<ProductModel>();
            var model = new ProductModel
            {
                ProductType = ProductType.PLC.ToString(),
                PartNo = "PLC18",
                SeriesName = "AS300",
                QIPCode = "123456",
                UserName = "Firstname Lastname"
            };

            Products.Add(model);

            model = new ProductModel
            {
                ProductType = ProductType.PLC.ToString(),
                PartNo = "PLC21",
                SeriesName = "AS300",
                QIPCode = "123458",
                UserName = "John Galt"
            };

            Products.Add(model);
            DataSourceResult rlt = null;


            rlt = Products.ToDataSourceResult(request);

        
            return Json(rlt, JsonRequestBehavior.AllowGet);

        }



        public ActionResult Index(ProductType productType)
        {

            var model = new ProductViewModel()
            {
                ProductType = productType,
                Title = productType.ToString(),
                ProductModel = new ProductModel(),
            };

        

            return View(model);
        }


    


        #endregion Public Methods




        #region Helper Methods


        [HttpPost]
        public ActionResult Upload(IEnumerable<HttpPostedFileBase> file)
        {
            return null;
        }

        public ActionResult ImageUpload()
        {

            return PartialView("ImageUpload");
        }

        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult RenderPartial(ProductType productType)
        {

            var View = productType.ToString() + "Model";
            var ModelName = "DIASelector.Business.Contract.Model." + productType.ToString() + "Model";
            var Type = System.Type.GetType(ModelName + ", DeltaIA.DIASelector.Business.Contract");


            return PartialView(View);
        }

        public ActionResult PartialExcelUpload()
        {
            return PartialView("_UploadExcel");
        }

 

 
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
          
            }
            base.Dispose(disposing);
        }

        private QueryDetailDescriptor InitQueryDetailDescriptor()
        {
            var queryDetailDescriptor = new QueryDetailDescriptor
            {
                SortingDescriptors = new List<SortingDescriptor>(),
                FilterDescriptors = new List<FilterDescriptor>()
            };
            return queryDetailDescriptor;
        }

        private QueryDetailDescriptor QueryDetailDescriptorFromGrid(DataSourceRequest request)
        {
            var queryDetailDescriptor = InitQueryDetailDescriptor();


            if (request != null)
            {
                // In case the property name in the grid data does not match the property name of the entity you would have to set the correct member name for
                // the middle layer to be able to sort in a override for this method in the respective controller.
                foreach (var desc in request.Sorts.Select(sortDescriptor =>
                        new SortingDescriptor
                        {
                            // As we always have the prefix 'Item' followed by the member name of the entity as in the db we have to remove the prefix
                            // and the separator ('.'). If grid member names follow another pattern this has to be adapted in method GetMemberNameWithoutPrefix
                            SortMember = sortDescriptor.Member,
                            SortDirection = sortDescriptor.SortDirection
                        }))
                {
                    queryDetailDescriptor.SortingDescriptors.Add(desc);
                }

                queryDetailDescriptor.StartPosition = request.Page > 1 ? (request.Page - 1) * request.PageSize : 0;
                queryDetailDescriptor.ItemsCount = request.PageSize;

                foreach (var filter in request.Filters)
                {
                    if (filter is CompositeFilterDescriptor)
                    {
                        var compositeFilterDescriptor = (CompositeFilterDescriptor)filter;
                        foreach (var filterDescriptor in compositeFilterDescriptor.FilterDescriptors)
                        {
                            queryDetailDescriptor.FilterDescriptors.Add(GetFilterDescriptor(filterDescriptor));
                        }
                    }
                    else
                    {
                        //var rlt = ApplyFilter(filter);
                        queryDetailDescriptor.FilterDescriptors.Add(GetFilterDescriptor(filter));
                    }

                }
            }

            return queryDetailDescriptor;
        }

        private FilterDescriptor GetFilterDescriptor(IFilterDescriptor filter)
        {
            var kendoDesc = (Kendo.Mvc.FilterDescriptor)filter;
            var desc = new FilterDescriptor
            {
                Operator = kendoDesc.Operator,
                Value = kendoDesc.Value.ToString().ToLower(),
                Member = kendoDesc.Member
            };
            return desc;
        }

   
        #endregion Helper Methods

    }
}
