using System;
using System.Collections.Generic;
using PositioningWebApp.Models.UserProfile;
using PositioningWebApp.Models.ViewModels;
using PositioningWebApp.Models.Api;
using System.Net.Mail;
using System.Net;

namespace PositioningWebApp.Services.Send
{
	public class Email
	{
		public void SendEmail(List<UserProfile> profiles, ModelDevice device, Point point)
		{
			MailMessage mailMessage = new MailMessage();
			SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
			mailMessage.From = new MailAddress("positioningwebapp@gmail.com");
			foreach (var profile in profiles)
			{
				mailMessage.To.Add(profile.Email);
			}
			mailMessage.Subject = "Device " + device.Name + " out of zone";
			mailMessage.Body = CreateBody(device, point);
			smtpClient.Credentials = new NetworkCredential("positioningwebapp@gmail.com", "tBgdD5h97vVJGDT");
			smtpClient.EnableSsl = true;
			smtpClient.Send(mailMessage);
		}

		private string CreateBody(ModelDevice device, Point p)
		{
			var point = p;
			if (!point.DateTime.HasValue)
			{
				point.DateTime = DateTime.Now;
			}
			return "Hello" + Environment.NewLine + Environment.NewLine +
				"your device " + device.Name + " with identificator " + device.Identificator +
				" is out of zone." + Environment.NewLine + Environment.NewLine +
				"Last known position is: " + Environment.NewLine +
				"Latitude: " + point.Latitude + Environment.NewLine +
				"Longitude: " + point.Longitude + Environment.NewLine +
				"Time: " + point.DateTime.Value.Hour + ":" + point.DateTime.Value.Minute + ":" + point.DateTime.Value.Second + Environment.NewLine +
				"Date: " + point.DateTime.Value.Day + ". " + point.DateTime.Value.Month + ". " + point.DateTime.Value.Year +
				Environment.NewLine + Environment.NewLine +
				"If got this email by accident than please ignore this email.";
		}
	}
}

