using Microsoft.EntityFrameworkCore;
using PositioningWebApp.Models.Api;
using PositioningWebApp.Models.DB;
using PositioningWebApp.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PositioningWebApp.DBServices
{
	public class PointService
	{
		public void AddPointFromApi(Point point, int deviceId, string nameLocation, DevicesOnMapBackgroundContext context)
		{
			try
			{
				var time = new DateTime();
				if (point.DateTime == null)
				{
					time = DateTime.Now;
				} else
				{
					time = point.DateTime.Value;
				}
				Points newPoint = new Points
				{
					Latitude = point.Latitude,
					Longitude = point.Longitude,
					DeviceId = deviceId,
					CreatedDateTime = time,
					NameLocation = nameLocation
					
				};
				context.Points.Add(newPoint);
				context.SaveChanges();
			} catch (Exception ex)
			{
				throw ex;
			}
		}

		public void AddPointsForZone (List<Marker> points, int zoneId, DevicesOnMapBackgroundContext context)
		{
			foreach (var item in points)
			{
				context.Points.Add(new Points
				{
					Latitude = item.Latitude,
					Longitude = item.Longitude,
					ZoneId = zoneId,
					CreatedDateTime = DateTime.Now
				});
			}
			context.SaveChanges();
		}

		public List<Marker> GetZonePointsBasedOnIDZone (int zoneId, DevicesOnMapBackgroundContext context)
		{
			var points = from p in context.Points where p.ZoneId == zoneId select p;
			List<Marker> ret = new List<Marker>();
			foreach (var item in points)
			{
				ret.Add(new Marker
				{
					IdMarker = item.Id,
					Latitude = item.Latitude,
					Longitude = item.Longitude,
					
				});
			}
			return ret;
		}

		public void DeletePointsBasedOnDeviceID(int deviceId, DevicesOnMapBackgroundContext context)
		{
			var pointsDelete = from p in context.Points where p.DeviceId == deviceId select p;
			foreach (var item in pointsDelete)
			{
				context.Points.Remove(item);
			}
			context.SaveChanges();
		}

		public void UpdatePoint(int idPoint, double lat, double lgn, DevicesOnMapBackgroundContext context)
		{
			var point = context.Points.FirstOrDefault(p => p.Id == idPoint);
			if (point != null)
			{
				point.Latitude = lat;
				point.Longitude = lgn;
			};
			context.Entry(point).State = EntityState.Modified;
			context.SaveChanges();
		}

		public void DeletePoint(int idPoint, DevicesOnMapBackgroundContext context)
		{
			var point = context.Points.FirstOrDefault(p => p.Id == idPoint);
			if (point != null)
			{
				context.Remove(point);
				context.SaveChanges();
			}
		}

		public void AddSingleZonePoint(int idZone, double lat, double lgn, DevicesOnMapBackgroundContext context)
		{
			Points p = new Points
			{
				CreatedDateTime = DateTime.Now,
				Latitude = lat,
				Longitude = lgn,
				ZoneId = idZone
			};
			context.Points.Add(p);
			context.SaveChanges();
		}

		public List<Marker> GetDevicePoints(int idDevice, DevicesOnMapBackgroundContext context)
		{
			var points = from p in context.Points where p.DeviceId == idDevice select p;
			List<Marker> ret = new List<Marker>();
			foreach (var item in points)
			{
				ret.Add(new Marker
				{
					IdMarker = item.Id,
					Latitude = item.Latitude,
					Longitude = item.Longitude,
					DateTime = item.CreatedDateTime.Value,
					LocationName = item.NameLocation
				});
			}
			return ret;
		}

		public List<Marker> GetDevicePointsWhereDateIsInRange(int idDevice, DateTime dateFrom, DateTime dateTo, DevicesOnMapBackgroundContext context)
		{
			var po = context.Points.Where(p => p.DeviceId == idDevice)
				.OrderByDescending(x => x.CreatedDateTime.Value).ToList();
			var points = new List<Points>();
			
			var to = dateTo.AddSeconds(60 - dateTo.Second);
			var from = dateFrom.AddSeconds(-dateFrom.Second);
			foreach (var point in po)
			{
				DateTime dateToCheck = point.CreatedDateTime.Value;
				if (dateToCheck >= from && dateToCheck < to)
				{
					points.Add(point);
				}
			}
			List<Marker> ret = new List<Marker>();
			foreach (var item in points)
			{
				ret.Add(new Marker
				{
					IdMarker = item.Id,
					Latitude = item.Latitude,
					Longitude = item.Longitude,
					DateTime = item.CreatedDateTime.Value,
					LocationName = item.NameLocation
				});
			}
			return ret;
		}

		public void DeleteZonePoints(int idZone, DevicesOnMapBackgroundContext context)
		{
			var points = (from p in context.Points where p.ZoneId == idZone select p).ToList();
			if (points.Count != 0)
			{
				foreach (var item in points)
				{
					context.Points.Remove(item);
				}
				context.SaveChanges();
			}
		}
	}
}
