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 GroupService
	{
		public int CreateNewGrop(int idOwner, string name, DevicesOnMapBackgroundContext context)
		{
			Groups newG = new Groups
			{
				OwnerId = idOwner,
				NameGroup = name
			};
			context.Groups.Add(newG);
			context.SaveChanges();
			return newG.Id;
		}

		public void RemoveGroup(int idOwner, int idGroup, DevicesOnMapBackgroundContext context)
		{
			var toDelete = context.Groups.Where(g => g.Id == idGroup && g.OwnerId == idOwner);
			if (toDelete != null)
			{
				context.Groups.Remove(toDelete.ToList().First());
				context.SaveChanges();
			}
		}

		public ModelGroup GetGroup (int idGroup, DevicesOnMapBackgroundContext context)
		{
			var group = context.Groups.Where(g => g.Id == idGroup);
			return new ModelGroup
			{
				ID = group.First().Id,
				Title = group.First().NameGroup,
				IdOwner = group.First().OwnerId.Value
			};
		}
	}
}
