using PositioningWebApp.DBServices;
using PositioningWebApp.Models.DB;
using PositioningWebApp.Models.ViewModels;
using System.Collections.Generic;

namespace PositioningWebApp.Services.Group
{
	public class DevicesOfUser
	{
		public List<ModelDevice> GetAllDevicesOfUser(int idOwner, DevicesOnMapBackgroundContext context)
		{
			List<ModelDevice> ret = new DeviceService().GetAllDevicesOfUser(idOwner, context);
			List<int> idGroup = new GroupUserService().GetGroupsOfUser(idOwner, context);
			if (idGroup.Count == 0)
			{
				return ret;
			}
			List<int> idDevices = new List<int>();
			foreach (var item in idGroup)
			{
				foreach (var id in new GroupDeviceService().GetIdDevicesInGroup(item, context))
				{
					idDevices.Add(id);
				}
			}
			foreach (var item in idDevices)
			{
				ModelDevice temp = new DeviceService().GetDeviceBasedOnIdDevice(item, context);
				if (!ret.Contains(temp))
				{
					ret.Add(temp);
				}
			}
			return ret;
		}
	}
}
