using FaultMapDev.EFModels;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kendo.Mvc.Extensions;

namespace FaultMapDev.Controllers
{
    public class HomeController : Controller
    {
        private QM_AthenaContext _db;

        public HomeController()
        {
            _db = new QM_AthenaContext();
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetWatchList_Read([DataSourceRequest] DataSourceRequest request, string queueName)
        {
            List<FaultWatchList> response = new List<FaultWatchList>();

            int counter = 0;

            FaultWatchList item = new FaultWatchList();
            item.FaultCode = "1298/03";
            item.FaultCount = 22319;
            item.TestValue = 3;
            item.FaultName = "DEF Tank Level";
            item.Type = "ACM";

            FaultWatchList item2 = new FaultWatchList();
            item2.FaultCode = "321/03";
            item2.FaultCount = 5000;
            item2.TestValue = 1;
            item2.FaultName = "DOC Inlet / DPF Outlet Pressure";
            item2.Type = "MCM";

            FaultWatchList item3 = new FaultWatchList();
            item3.FaultCode = "369/333";
            item3.FaultCount = 33000;
            item3.TestValue = 3;
            item3.FaultName = "DEF Inducement";
            item3.Type = "ACM";

            response.Add(item);

            response.Add(item2);

            response.Add(item3);

            //
            DataSourceResult result = response.ToDataSourceResult(request);
            //
            return Json(result, new Newtonsoft.Json.JsonSerializerSettings());
        }
        [HttpPost]
        public ActionResult GetFaultLocations()
        {
            List<MapData> response = new List<MapData>();


            #region test data
            //double[] array2 = { 30.20125561428524, -97.8789197628971 };
            //double[] array3 = { 27.30125561428524, -87.6789197628971 };
            double[] array4D = { 9.726383, 5.835435 };


            //MapData mData = new MapData();
            //mData.Location = array2;
            //mData.PointValue = 10;

            //MapData mData2 = new MapData();
            //mData2.Location = array3;
            //mData2.PointValue = 10;

            MapData mData3 = new MapData();
            mData3.Location = array4D;
            mData3.PointValue = 1000;

            //response.Add(mData);
            //response.Add(mData2);
            //response.Add(mData3);
            #endregion 

            var query = _db.ObjTelematicFaults1.Where(t => t.FaultCode == @"2003/31");

            foreach(ObjTelematicFault1 fault in query)
            {
                MapData md = new MapData();
                double[] tempArray = {  Convert.ToDouble(fault.Latitude), Convert.ToDouble(fault.Longitude) };
                md.Location = tempArray;
                md.PointValue = 10;
                response.Add(md);
            }


            response.Add(mData3);
            return Json(response);
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View();
        }
    }

    public class MapPoint
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

    public class MapData
    {
        public double[] Location { get; set; }
        public double PointValue { get; set; }
    }
}
