using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Hosting;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using PositioningWebApp.DBServices;
using PositioningWebApp.Models.Ajax;
using PositioningWebApp.Models.DB;
using PositioningWebApp.Models.ViewModels;
using PositioningWebApp.Services;

namespace PositioningWebApp.Pages.Home
{
    public class LogBookModel : PageModel
    {
		private DevicesOnMapBackgroundContext databaseManager;
		
		public List<DeviceUserLogBook> ListLogBooks { get; set; }
		public LogBookModel(DevicesOnMapBackgroundContext context)
		{
			try
			{
				databaseManager = context;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public void OnGet()
        {
			var idUser = Convert.ToInt32(Convert.ToInt32(User.Claims.ToList()[3].Value));
			List<ModelDevice> devices = new GetAllDevices().AllSharedDevices(idUser, databaseManager);
			ListLogBooks = new List<DeviceUserLogBook>();
			foreach (var device in devices)
			{
				var temp = new LogBookService().GetLogBooks(device.Id, databaseManager);
				if (temp.Count != 0)
				{
					var tempUser = new UserService().GetUser(device.IdOwner, databaseManager);
					ListLogBooks.Add(new DeviceUserLogBook
					{
						IdDevice = device.Id,
						Identificator = device.Identificator,
						LastRecord = temp.Last().Departure.Value,
						FirstRecord = temp.First().Departure.Value,
						FirstName = tempUser.FirstName,
						Surname = tempUser.Surname,
						NameDevice = device.Name
					});
				}

			}
		}
	}
}