using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
//using System.Web.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Spice.Data;
using Spice.Models;

namespace Spice.Controllers
{
    [Area("Customer")]
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly ApplicationDbContext _db;
        private readonly IConfiguration _configuration;
        public HomeController(ILogger<HomeController> logger, ApplicationDbContext db, IConfiguration configuration)
        {
            _logger = logger;
            _db = db;
            _configuration = configuration;
        }

        public IActionResult Index()
        {           
            return View(_db.Category.Where(s => s.Name == "Apetizer").ToList());
        }

        public IActionResult Privacy(string magnitude, string deveui)
        {
            //List<db_data> db_Data = new List<db_data>();
            //DateTime d = DateTime.Now;
            //db_Data.Add(new db_data(25.0, 35.0));
            //ViewBag.db_Data = db_Data.ToArray();
            //ViewBag.magnitude = "humd";
            ViewData["magnitude"] = magnitude;//"humd";
            ViewData["deveui"] = deveui;
            return View(_db.Category.Where(s => s.Name == "Apetizer").ToList());
        }

        public async Task<IActionResult> GetData(string magnitude, string deveui)           
        {
            Object[] myarray = new object[942];
            List<object[]> list = new List<object[]>();
            db d = new db();
            string cs = _configuration.GetConnectionString("DefaultConnection");
            //myarray = await d.GetDbData(ref myarray, cs);
            list = await d.GetDbData(ref myarray, cs,magnitude, deveui);

            /*Funkcni      db_data d = new db_data(1552, 25.5);
            List<db_data> db_Data = new List<db_data>();
            for(int i = 0; i < 50000; ++i)
            {
                db_Data.Add(new db_data(1521811800000 + i*3500, 35.5+i*0.001));
            }
            Object[] myarray = new object[50000];
            for (int i = 0; i < 50000; ++i)
            {
                myarray[i] = new object[] { 1521811800000 + i * 3500, 35.5 + i * 0.001 };
            }*/
            /*db_Data.Add(new db_data(1521811800000, 35.5));
            db_Data.Add(new db_data(1547044200000, 45.5));
            db_Data.Add(new db_data(1562851800000, 55.5));*/
            //funkcni: [[data[0].X, data[0].Y],[data[1].X, data[1].Y],[data[2].X, data[2].Y]]
            //DateTime d = DateTime.Now;
            /*db_Data.Add(new db_data("leden", 35.0));
            db_Data.Add(new db_data("unor", 45.0));
            string a = db_Data[0].X;*/
            //ViewBag.db_Data = JsonConvert.SerializeObject(db_Data);

            //JsonResult j = Json(db_Data, new System.Text.Json.JsonSerializerOptions());
            //return Json(db_Data, new System.Text.Json.JsonSerializerOptions());
            return Json(list, new System.Text.Json.JsonSerializerOptions());
            //return JsonConvert.SerializeObject(db_Data);
        }


        public async Task<IActionResult> UpdateChart(long min, long max, string magnitude, string deveui)
        {
            Object[] myarray = new object[942];
            List<object[]> list = new List<object[]>();
            db d = new db();
            string cs = _configuration.GetConnectionString("DefaultConnection");
            list = await d.GetDbData(ref myarray, cs, min, max, magnitude, deveui);

            List<object[]> list1 = new List<object[]>();
            if (list.Count > 100)
            {
                int del = list.Count / 100; // chci 100
                double a = 0;
                for(int i = 0; i < list.Count; i+=del)
                {
                    a = 0;
                    int j = 0;
                    for (j = 0; j < del; ++j)
                    {
                        if((j+i)<list.Count)
                            a += (((double)list.ElementAt(j+i)[1]));
                    }
                    a = a/j;
                    //list1.Add(new object[] { list.ElementAt(i)[0], (((double)list.ElementAt(i)[1] + (double)list.ElementAt(i + 1)[1]) / 2) });
                    list1.Add(new object[] { list.ElementAt(i)[0], a});
                }
                list1.RemoveAt(list1.Count-1);
                return Json(list1, new System.Text.Json.JsonSerializerOptions());
            }

            //myarray =  await d.GetDbData(ref myarray, cs, min, max);

            /*long[] larray = new long[myarray.Length];
            long i = 0;
            foreach(object item in myarray)
            {
                //var x = item;
                larray[i] = (long)((object[])item)[0];
                ++i;
            }
            Array.Sort(larray);
            long j = 0;
            foreach(object item in myarray)
            {
                ((object[])item)[0] = larray[j];
                ++j;
            }*/
            
            /*for (int i = 0; i < 50000; ++i)
            {
                myarray[i] = new object[] { 1521811800000 + i * 3500, 35.5 + i * 0.001 };
            }*/
            //return Json(myarray, new System.Text.Json.JsonSerializerOptions());
            return Json(list, new System.Text.Json.JsonSerializerOptions());

        }


        public IActionResult GetChunk(long min, long max)
        {
            db_data d = new db_data(1552, 25.5);
            List<db_data> db_Data = new List<db_data>();
            for (int i = 0; i < 50000; ++i)
            {
                db_Data.Add(new db_data(1521811800000 + i * 3500, 35.5 + i * 0.001));
            }
            Object[] myarray = new object[max-min];
            for (int i = 0; i < 50000; ++i)
            {
                myarray[i] = new object[] { 1521811800000 + i * 3500, 35.5 + i * 0.001 };
            }
            /*db_Data.Add(new db_data(1521811800000, 35.5));
            db_Data.Add(new db_data(1547044200000, 45.5));
            db_Data.Add(new db_data(1562851800000, 55.5));*/
            //funkcni: [[data[0].X, data[0].Y],[data[1].X, data[1].Y],[data[2].X, data[2].Y]]
            //DateTime d = DateTime.Now;
            /*db_Data.Add(new db_data("leden", 35.0));
            db_Data.Add(new db_data("unor", 45.0));
            string a = db_Data[0].X;*/
            //ViewBag.db_Data = JsonConvert.SerializeObject(db_Data);

            JsonResult j = Json(db_Data, new System.Text.Json.JsonSerializerOptions());
            //return Json(db_Data, new System.Text.Json.JsonSerializerOptions());
            return Json(myarray, new System.Text.Json.JsonSerializerOptions());
            //return JsonConvert.SerializeObject(db_Data);
        }


        


        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
