using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using UploadInGridEventsIssue.Models;

namespace UploadInGridEventsIssue.Controllers
{
    public class HomeController : Controller
    {
        public IHostingEnvironment HostingEnvironment { get; set; }
        List<ProductModel> products = new List<ProductModel>();

        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            HostingEnvironment = hostingEnvironment;
            products.Add(new ProductModel{ Id =1, ProductName ="Product 1", FileData = new FilesModel { ActivityId = 1, Create_Date = new DateTime(), File_Ext = ".png", File_Name = "telerik", File_Path = "uploads" } });
            products.Add(new ProductModel { Id = 2, ProductName = "Product 2", FileData = new FilesModel { ActivityId = 2, Create_Date = new DateTime(), File_Ext = ".png", File_Name = "kendo", File_Path = "uploads" } });

        }

        public IActionResult Index()
        {

            return View();
        }

        public ActionResult Grid_Read([DataSourceRequest]DataSourceRequest request)
        {

              DataSourceResult result = products.ToDataSourceResult(request);
                return Json(result);
            
        }

        //public async Task<ActionResult> SaveActivityDoc(IEnumerable<IFormFile> files)
        //{
        //    // The Name of the Upload component is "files"
        //    if (files != null)
        //    {
        //        foreach (var file in files)
        //        {
        //            var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);

        //            // Some browsers send file names with full path.
        //            // We are only interested in the file name.
        //            var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
        //            var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "uploads", fileName);
        //            products.Add(new Toolbox_Projects_Activity_Files { ActivityId = 2, Create_Date = new DateTime(), File_Data = new byte[] { 1, 2, 3 }, File_Ext = "Ext 1", File_Name = fileName, File_Path = physicalPath, Id = 2});

        //            // The files are not actually saved in this demo
        //            using (var fileStream = new FileStream(physicalPath, FileMode.Create))
        //            {
        //                await file.CopyToAsync(fileStream);
        //            }
        //        }
        //    }

        //    // Return an empty string to signify success
        //    return Content("");
        //}

    }
}
