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

namespace PositioningWebApp.DBServices
{
	public class DeviceService
	{
		public int AddDevice (Devices device, DevicesOnMapBackgroundContext context)
		{
			var d = device;
			try
			{
				context.Devices.Add(d);
				context.SaveChanges();
				return d.Id;
			} catch (Exception ex)
			{
				throw ex;
			}
		}

		public List<string> GetDevicesIndetificators (DevicesOnMapBackgroundContext context)
		{
			var devices = from d in context.Devices select d.Identifier;
			return devices.ToList();

		}

		public List<ModelDevice> GetAllDevicesOfUser(int ownerID, DevicesOnMapBackgroundContext context)
		{
			var device = from d in context.Devices where d.OwnerId == ownerID select d;
			List<ModelDevice> ret = new List<ModelDevice>();
			foreach (var dev in device.ToList())
			{
				ret.Add(
					new ModelDevice
					{
						Id = dev.Id,
						AlertEmail = dev.ZoneEmailAlert.ToString(),
						AlertSMS = dev.ZoneSmsalert.ToString(),
						Identificator = dev.Identifier,
						Name = dev.Title,
						IdOwner = dev.OwnerId.Value

					});
			}
			return ret;
		}

		public List<ModelDevice> GetAllDevicesOfUserWhereZoneIsNull(int ownerID, DevicesOnMapBackgroundContext context)
		{
			var device = from d in context.Devices where d.OwnerId == ownerID select d;
			List<ModelDevice> ret = new List<ModelDevice>();
			foreach (var d in device.ToList())
			{
				ret.Add(
					new ModelDevice
					{
						Identificator = d.Identifier,
						Name = d.Title
					});
			}
			return ret;
		}

		public List<int> GetDeviceIDBasedOnIdentificator(string identificator, DevicesOnMapBackgroundContext context)
		{
			var device = from d in context.Devices where d.Identifier == identificator select d.Id;
			var ret = device.ToList();
			return ret;
		}

		public ModelDevice GetDeviceBasedOnIdentificator(string identificator, DevicesOnMapBackgroundContext context)
		{
			var device = from d in context.Devices where d.Identifier == identificator select d;
			var dev = device.ToList().FirstOrDefault();
			return new ModelDevice
			{
				Id = dev.Id,
				AlertEmail = dev.ZoneEmailAlert.ToString(),
				AlertSMS = dev.ZoneSmsalert.ToString(),
				Identificator = dev.Identifier,
				Name = dev.Title,
				IdOwner = dev.OwnerId.Value,
				AlertSended = dev.AlertSended
			};
		}

		public void UpdateSendedAlert(int idDevice, bool alert, DevicesOnMapBackgroundContext context)
		{
			(from d in context.Devices where d.Id == idDevice select d).ToList().ForEach(x => x.AlertSended = alert);
			context.SaveChanges();
		}

		public ModelDevice GetDeviceBasedOnIdDevice(int idDevice, DevicesOnMapBackgroundContext context)
		{
			var device = from d in context.Devices where d.Id == idDevice select d;
			var dev = device.ToList().First();
			ModelDevice ret = new ModelDevice
			{
				Id = dev.Id,
				AlertEmail = dev.ZoneEmailAlert.ToString(),
				AlertSMS = dev.ZoneSmsalert.ToString(),
				Identificator = dev.Identifier,
				Name = dev.Title,
				IdOwner = dev.OwnerId.Value
			};
			return ret;
		}

		public void UpdateDeviceBasedOnIdDevice(Devices device, DevicesOnMapBackgroundContext context)
		{
			var result = context.Devices.FirstOrDefault(u => u.Id == device.Id);
			if (result != null)
			{
				result.Title = device.Title;
				result.ZoneEmailAlert = device.ZoneEmailAlert;
				result.ZoneSmsalert = device.ZoneSmsalert;
			}
			context.Entry(result).State = EntityState.Modified;
			context.SaveChanges();
		}

		public void DeleteDeviceBasedOnDeviceId (int deviceId, DevicesOnMapBackgroundContext context)
		{
			var deviceToDelete = (from d in context.Devices where d.Id == deviceId select d).SingleOrDefault();
			context.Devices.Remove(deviceToDelete);
			context.SaveChanges();
		}
	}
}
