using PositioningWebApp.DBServices;
using PositioningWebApp.Models.DB;
using PositioningWebApp.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PositioningWebApp.Services
{
	public class GetAllDevices
	{
		public List<ModelDevice> AllSharedDevices(int idUser, DevicesOnMapBackgroundContext databaseManager)
		{
			List<int> groups = new GroupUserService().GetGroupsOfUser(idUser, databaseManager);
			List<int> idDevices = new List<int>();
			foreach (var idGroup in groups)
			{
				List<int> temp = new GroupDeviceService().GetIdDevicesInGroup(idGroup, databaseManager);
				foreach (var idDevice in temp)
				{
					if (!idDevices.Contains(idDevice))
					{
						idDevices.Add(idDevice);
					}
				}
			}
			List<ModelDevice> devices = new List<ModelDevice>();
			foreach (var device in new DeviceService().GetAllDevicesOfUser(idUser, databaseManager))
			{
				if (!idDevices.Contains(device.Id))
				{
					devices.Add(device);
				}
			}
			foreach (var id in idDevices)
			{
				devices.Add(new DeviceService().GetDeviceBasedOnIdDevice(id, databaseManager));
			}
			return devices;
		}
	}
}
