using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PositioningWebApp.DBServices;
using PositioningWebApp.Models.Ajax;
using PositioningWebApp.Models.DB;
using PositioningWebApp.Models.UserProfile;
using PositioningWebApp.Models.ViewModels;

namespace PositioningWebApp.Pages.Home.Profile
{
    public class EditGroupModel : PageModel
    {
		private DevicesOnMapBackgroundContext databaseManager;
		public List<UserProfile> Users { get; set; }
		public List<ModelDevice> Devices { get; set; }
		public List<ModelDevice> PersonalDevices { get; set; }
		public int IdUser { get; set; }
		public ModelGroup Group { get; set; }
		public List<UserGroupDevice> UsersDevices { get; set; }
		public EditGroupModel (DevicesOnMapBackgroundContext context)
		{
			try
			{
				databaseManager = context;
			} catch (Exception ex)
			{
				throw ex;
			}
		}
		public void OnGet(int id)
        {
			IdUser = Convert.ToInt32(User.Claims.ToList()[3].Value);
			List<int> idUsersInGroup = new GroupUserService().GetAllUsersInGroup(id, databaseManager);
			List<int> idDeviesInGroup = new GroupDeviceService().GetIdDevicesInGroup(id, databaseManager);
			Users = new List<UserProfile>();
			Devices = new List<ModelDevice>();
			foreach (var item in idUsersInGroup)
			{
				Users.Add(new UserService().GetUser(item, databaseManager));
			}
			foreach (var item in idDeviesInGroup)
			{
				Devices.Add(new DeviceService().GetDeviceBasedOnIdDevice(item, databaseManager));
			}
			var uDevies = new DeviceService().GetAllDevicesOfUser(IdUser, databaseManager);
			PersonalDevices = new List<ModelDevice>();
			foreach (var item in uDevies)
			{
				if (!idDeviesInGroup.Contains(item.Id))
				{
					PersonalDevices.Add(item);
				}
			}

			UsersDevices = new List<UserGroupDevice>();

			foreach (var item in Devices)
			{
				UserGroupDevice newItem = new UserGroupDevice {
					DeviceName = item.Name,
					IdDevice = item.Id,
					Identificator = item.Identificator
				};
				foreach (var user in Users)
				{
					if (user.ID == item.IdOwner)
					{
						newItem.Surname = user.Surname;
						newItem.FirstName = user.FirstName;
						newItem.IdUser = user.ID;
						UsersDevices.Add(newItem);
						break;
					}
				}
			}
			

			Group = new GroupService().GetGroup(id, databaseManager);
        }
		
		public JsonResult OnPostAddNewUser([FromBody] AddToGroup addUser)
		{
			var user = new UserService().GetProfile(addUser.Value, databaseManager);
			if (user == null)
			{
				return new JsonResult("User with this email does not exist.");
			}
			List<int> usersInGroup = new GroupUserService().GetAllUsersInGroup(addUser.IdGroup, databaseManager);
			if (usersInGroup.Contains(user.ID))
			{
				return new JsonResult("User is already in group.");
			}
			new GroupUserService().AddUserToGroup(addUser.IdGroup, user.ID, databaseManager);
			return new JsonResult("done");
		}

		public JsonResult OnPostAddNewDevice([FromBody] AddToGroup addDevice)
		{
			ModelDevice device = new DeviceService().GetDeviceBasedOnIdentificator(addDevice.Value, databaseManager);
			List<int> devicesInGrup = new GroupDeviceService().GetIdDevicesInGroup(addDevice.IdGroup, databaseManager);
			if (devicesInGrup.Contains(device.Id))
			{
				return new JsonResult("Device is already in group.");
			}
			new GroupDeviceService().AddDeviceIntoGroup(addDevice.IdGroup, device.Id, databaseManager);
			return new JsonResult(true);
		}

		public JsonResult OnPostDeleteDevice([FromBody] AddToGroup deleteDevice)
		{
			new GroupDeviceService().DeleteDeviceFromGroup(deleteDevice.IdGroup, Convert.ToInt32(deleteDevice.Value), databaseManager);
			return new JsonResult(true);
		}
	}
}