using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Edge.WebHost.Models.TankMonitor;
using Edge.WebHost.Models.VeederRoot_ATG_Console_Handler_Models;
using Edge.Core.IndustryStandardInterface.ATG;
using Edge.WebHost.Models.SmartFuel;

namespace Edge.WebHost.Controllers
{
    public class TankDetailsController : Controller
    {
        CurrentTank currenttank = new CurrentTank();

        public IActionResult Index()
        {
            var smartFuelInfo = new SmartFuelViewInfo();
            smartFuelInfo.queryString = Request.Query["q"].ToString();
            return View("../WebConsole/TankDetails/Index", smartFuelInfo);
        }

        public IActionResult TankOverviewFnav()
        {
            return PartialView("/Views/WebConsole/TankDetails/TankOverviewFnav.cshtml");
        }

        public IActionResult TankDevicesOverview()
        {
            return PartialView("/Views/WebConsole/TankDetails/TankDevicesOverview.cshtml");
        }

        public IActionResult TankDetailFnav()
        {
            return PartialView("/Views/WebConsole/TankDetails/TankDetailFnav.cshtml");
        }

        public IActionResult TankDeviceDetails(int index)
        {
            ViewBag.tankIndex = index;
            return PartialView("/Views/WebConsole/TankDetails/TankDeviceDetails.cshtml");
        }

        public IActionResult AlarmHistoryFnav()
        {
            return PartialView("/Views/WebConsole/TankDetails/AlarmHistoryFnav.cshtml");
        }

        public IActionResult AlarmHistory()
        {
            return PartialView("/Views/WebConsole/TankDetails/AlarmHistory.cshtml");
        }

        public IActionResult FuelInventoryFnav()
        {
            return PartialView("/Views/WebConsole/TankDetails/FuelInventoryFnav.cshtml");
        }

        public IActionResult FuelInventoryList()
        {
            return PartialView("/Views/WebConsole/TankDetails/FuelInventoryList.cshtml");
        }

        public IActionResult ActiveAlarm()
        {
            return View("/Views/WebConsole/TankDetails/ActiveAlarm");
        }

        public IActionResult InStockFuelReport()
        {
            return View("/Views/WebConsole/TankDetails/InStockFuelReport");
        }

        public IActionResult NewInStockFuel()
        {
            return View("/Views/WebConsole/TankDetails/NewInStockFuel");
        }

        public IActionResult NewFuelInventory()
        {
            return View("/Views/WebConsole/TankDetails/NewFuelInventory");
        }

        public IActionResult Test(int index)
        {
            ViewBag.tankIndex = index;
            return View("../WebConsole/TankDetails/Index");
        }

        //public IActionResult TankDeviceDetails()
        ////public IActionResult TankDeviceDetails(string tankID, string fuelID, string fuelKind)
        //{
        //    if (currenttank.currentTankID == null)
        //    {
        //        currenttank.currentTankID = "1";
        //        currenttank.currentFuelID = "92";//"#92";
        //        currenttank.currentFuelKind = "汽油";//"GasOil";
        //    }
        //    ViewBag.CurrentTankID = currenttank.currentTankID.ToString();
        //    ViewBag.CurrentFuelID = currenttank.currentFuelID.ToString();
        //    ViewBag.CurrentFuelKind = currenttank.currentFuelKind.ToString();
        //    return View("TankDeviceDetails");
        //}

        private static MockAutoTankGaugeController mockAtgController;
        // GET: /<controller>/
        //public IActionResult Index()
        static TankDetailsController()
        {
            //return "This is my default action...";
            //return View();

            var tank1 = new Tank()
            {
                TankNumber = 1,
                Diameter = 1800,
                Product = new Product()
                {
                    ProductCode = "02",
                    ProductLabel = "92#"
                },
                Limit = new TankLimit() { },
                Probe = new Probe()
                {
                    ProbeLength = 1800,
                    State = "",
                }
            };
            var tank2 = new Tank()
            {
                TankNumber = 2,
                Diameter = 1800,
                Product = new Product()
                {
                    ProductCode = "05",
                    ProductLabel = "95#"
                },
                Limit = new TankLimit()
                {

                },
                Probe = new Probe()
                {
                    ProbeLength = 2800,
                    State = "",
                }
            };
            var tank3 = new Tank()
            {
                TankNumber = 3,
                Diameter = 1800,
                Product = new Product()
                {
                    ProductCode = "08",
                    ProductLabel = "98#"
                },
                Limit = new TankLimit()
                {

                },
                Probe = new Probe()
                {
                    ProbeLength = 1800,
                    State = "",
                }
            };
            var tank4 = new Tank()
            {
                TankNumber = 4,
                Diameter = 1800,
                Product = new Product()
                {
                    ProductCode = "09",
                    ProductLabel = "0#"
                },
                Limit = new TankLimit()
                {

                },
                Probe = new Probe()
                {
                    ProbeLength = 1800,
                    State = "",
                }
            };
            var tank5 = new Tank()
            {
                TankNumber = 5,
                Diameter = 1800,
                Product = new Product()
                {
                    ProductCode = "11",
                    ProductLabel = "-35#"
                },
                Limit = new TankLimit()
                {

                },
                Probe = new Probe()
                {
                    ProbeLength = 1800,
                    State = "",
                }
            };

            mockAtgController =
                new MockAutoTankGaugeController(
                    new List<Tank>()
                    {
                        tank1,tank2,tank3,tank4,tank5
                    }, SystemUnit.Metric);
        }

        // 
        // GET: /HelloWorld/Welcome/ 

        // GET: VeederRoot_ATG_Console_Handler_/Create
        public ActionResult Create()
        {
            return View();
        }
        // GET: VeederRoot_ATG_Console_Handler_/Details/5
        public async Task<ActionResult> Details(int? tankNumber)
        {
            if (tankNumber.HasValue)
            {
                var reading = await mockAtgController.GetTankReadingAsync(tankNumber.Value);
                if (reading == null) return NotFound();
                return Ok(new[]
                {
                   new TankReadingDto(){
                        tankNumber=   tankNumber.Value,
                        Height=   reading.Height,
                        Water=   reading.Water,
                        Temperature=   reading.Temperature,
                        Volume=   reading.Volume,
                        WaterVolume=  reading.WaterVolume,
                        TimeStamp = DateTime.Now.ToString("HH:mm:ss"),
                   }
                });
            }
            else
            {
                List<TankReadingDto> tankReadings = new List<TankReadingDto>();
                foreach (var t in mockAtgController.Tanks)
                {
                    var reading = await mockAtgController.GetTankReadingAsync(t.TankNumber);
                    if (reading == null) continue;
                    var dto = new TankReadingDto()
                    {
                        tankNumber = t.TankNumber,
                        Height = reading.Height,
                        Water = reading.Water,
                        Temperature = reading.Temperature,
                        Volume = reading.Volume,
                        WaterVolume = reading.WaterVolume,
                        TimeStamp = DateTime.Now.ToString("HH:mm:ss"),
                    };
                    tankReadings.Add(dto);
                }

                return Ok(tankReadings);
            }
        }

        // POST: VeederRoot_ATG_Console_Handler_/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}