using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PositioningWebApp.DBServices;
using PositioningWebApp.Models.Ajax;
using PositioningWebApp.Models.DB;
using PositioningWebApp.Models.ViewModels;
using PositioningWebApp.Services;

namespace PositioningWebApp.Pages.Home
{
    public class HomeModel : PageModel
    {
		private DevicesOnMapBackgroundContext databaseManager;
		public List<ModelDevice> ListDevices { get; set; }
		public HomeModel(DevicesOnMapBackgroundContext context)
		{
			try
			{
				databaseManager = context;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public void OnGet()
		{
			var id = Convert.ToInt32(Convert.ToInt32(User.Claims.ToList()[3].Value));
			var usersDevices = new DeviceService().GetAllDevicesOfUser(id, databaseManager);
			List<int> idGroup = new GroupUserService().GetGroupsOfUser(id, databaseManager);
			List<int> idDevicesFromGroup = new List<int>();
			foreach (var item in idGroup)
			{
				var temp = new GroupDeviceService().GetIdDevicesInGroup(item, databaseManager);
				foreach (var idDevice in temp)
				{
					idDevicesFromGroup.Add(idDevice);
				}
			}
			ListDevices = new List<ModelDevice>();
			foreach (var item in idDevicesFromGroup)
			{
				ListDevices.Add(new DeviceService().GetDeviceBasedOnIdDevice(item, databaseManager));
			}
			foreach (var item in usersDevices)
			{
				if (!idDevicesFromGroup.Contains(item.Id))
				{
					ListDevices.Add(item);
				}
			}
		}


		public JsonResult OnPostDevice([FromBody] SelectedDeviceTime select)
		{
			ModelDevice device = new DeviceService().GetDeviceBasedOnIdentificator(select.Identificator, databaseManager);
			List<Marker> devicePoints;
			if (!select.DateFrom.Equals("") && !select.DateTo.Equals(""))
			{
				devicePoints = new PointService().GetDevicePointsWhereDateIsInRange(
					device.Id, DateTime.Parse(select.DateFrom), DateTime.Parse(select.DateTo), databaseManager);
			}
			else if (select.DateFrom.Equals("") && select.DateTo.Equals(""))
			{
				devicePoints = new PointService().GetDevicePoints(device.Id, databaseManager);
			} else if(select.DateFrom.Equals("") && !select.DateTo.Equals(""))
			{
				devicePoints = new PointService().GetDevicePointsWhereDateIsInRange(
					device.Id, DateTime.MinValue, DateTime.Parse(select.DateTo), databaseManager);
			} else
			{
				devicePoints = new PointService().GetDevicePointsWhereDateIsInRange(
					device.Id, DateTime.Parse(select.DateFrom), DateTime.MaxValue, databaseManager);
			}

			DeviceZoneMarkers markers;
			List<int> idZones = new DeviceZoneService().GetDeviceZone(device.Id, databaseManager);
			if (idZones.Count != 0)
			{
				markers = new DeviceZoneMarkers
				{
					DeviceMarkers = devicePoints,
					ZoneMarkers = new List<List<Marker>>()
				};
				foreach (var id in idZones)
				{
					List<Marker> zonePoints = new PointService().GetZonePointsBasedOnIDZone(id, databaseManager);
					markers.ZoneMarkers.Add(zonePoints);
				}
				return new JsonResult(markers);
			} else
			{
				markers = new DeviceZoneMarkers
				{
					DeviceMarkers = devicePoints,
					ZoneMarkers = new List<List<Marker>>()
				};
				return new JsonResult(markers);
			}
			
		}
	}
}