using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using PositioningWebApp.DBServices;
using PositioningWebApp.Models.DB;
using PositioningWebApp.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using PositioningWebApp.Models.Ajax;

namespace PositioningWebApp.Services
{
	public class Excel
	{
		public void Get(List<LogBook> logBooks, List<List<TripAjax>> trips, IHostingEnvironment _hostingEnvironment, DevicesOnMapBackgroundContext databaseManager)
		{
			string rootFolder = _hostingEnvironment.ContentRootPath + "/wwwroot/Download";
			string fileName = @"LogBook.xlsx";
			string f = rootFolder + "/" + fileName;
			string path = Path.Combine(rootFolder, fileName);
			if (File.Exists(path))
			{
				File.Delete(path);
			}

			FileInfo file = new FileInfo(path);
			ExcelPackage excel = new ExcelPackage(file);

			if (trips == null)
			{
				GetLogBook(logBooks, excel, databaseManager);
			} else
			{
				GetTrips(trips, excel);
			}
		}

		private void GetLogBook(List<LogBook> logBooks, ExcelPackage excel, DevicesOnMapBackgroundContext databaseManager)
		{
			Distance distance = new Distance();
			var workSheet = excel.Workbook.Worksheets.Add("LogBook");
			workSheet.TabColor = System.Drawing.Color.Black;
			workSheet.DefaultRowHeight = 12;
			workSheet.Row(1).Height = 20;
			workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
			workSheet.Row(1).Style.Font.Bold = true;
			workSheet.Cells[1, 1].Value = "S.No";
			workSheet.Cells[1, 2].Value = "Purpos";
			workSheet.Cells[1, 3].Value = "Start";
			workSheet.Cells[1, 4].Value = "End";
			workSheet.Cells[1, 5].Value = "Distance [m]";
			int recordIndex = 2;
			foreach (var item in logBooks)
			{
				
				List<Marker> markers = new PointService().GetDevicePointsWhereDateIsInRange(item.IdDevice, item.Departure.Value, item.Arrival.Value, databaseManager);
				var distTemp = distance.Coumpiute(markers);
				distTemp = Math.Ceiling(distTemp);
				
				
				workSheet.Cells[recordIndex, 1].Value = recordIndex - 1;
				workSheet.Cells[recordIndex, 2].Value = item.Purpose;
				workSheet.Cells[recordIndex, 3].Value = item.Departure.Value.Hour.ToString() + ":" + item.Departure.Value.Minute.ToString() + " " +
					item.Departure.Value.Day.ToString() + ". " + item.Departure.Value.Month.ToString() + ". " + item.Departure.Value.Year.ToString();
				workSheet.Cells[recordIndex, 4].Value = item.Arrival.Value.Hour.ToString() + ":" + item.Arrival.Value.Minute.ToString() + " " +
					item.Arrival.Value.Day.ToString() + ". " + item.Departure.Value.Month.ToString() + ". " + item.Arrival.Value.Year.ToString();
				workSheet.Cells[recordIndex, 5].Value = distTemp;
				recordIndex++;
			}
			workSheet.Column(1).AutoFit();
			workSheet.Column(2).AutoFit();
			workSheet.Column(3).AutoFit();
			workSheet.Column(4).AutoFit();
			workSheet.Column(5).AutoFit();

			excel.Save();
		}

		private void GetTrips(List<List<TripAjax>> trips, ExcelPackage excel)
		{
			foreach (var trip in trips)
			{
				var workSheet = excel.Workbook.Worksheets.Add(trip.First().Identificator);
				workSheet.TabColor = System.Drawing.Color.Black;
				workSheet.DefaultRowHeight = 12;
				workSheet.Row(1).Height = 20;
				workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
				workSheet.Row(1).Style.Font.Bold = true;
				workSheet.Cells[1, 1].Value = "S.No";
				workSheet.Cells[1, 2].Value = "User";
				workSheet.Cells[1, 3].Value = "Start";
				workSheet.Cells[1, 4].Value = "Start Destination";
				workSheet.Cells[1, 5].Value = "End";
				workSheet.Cells[1, 6].Value = "End Destination";
				workSheet.Cells[1, 7].Value = "Distance [m]";
				int recordIndex = 2;
				foreach (var item in trip)
				{
					workSheet.Cells[recordIndex, 1].Value = recordIndex - 1;
					workSheet.Cells[recordIndex, 2].Value = item.User;
					workSheet.Cells[recordIndex, 3].Value = item.Start;
					workSheet.Cells[recordIndex, 4].Value = item.PlaceFrom;
					workSheet.Cells[recordIndex, 5].Value = item.End;
					workSheet.Cells[recordIndex, 6].Value = item.PlaceTo;
					workSheet.Cells[recordIndex, 7].Value = Convert.ToInt32(item.Distance);
					recordIndex++;
				}
				workSheet.Column(1).AutoFit();
				workSheet.Column(2).AutoFit();
				workSheet.Column(3).AutoFit();
				workSheet.Column(4).AutoFit();
				workSheet.Column(5).AutoFit();
				workSheet.Column(6).AutoFit();
				workSheet.Column(7).AutoFit();
			}
			excel.Save();
		}
	}
}
