using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Text;
using System.Net;
using System.Net.Mail;
using PersonSport.Models;


namespace PersonSport.Controllers {
    public class StartController : Controller {

        public IActionResult Index() {
            return View();
        }
        public IActionResult Priser() {
            return View();
        }
        public IActionResult Kurser() {
            return View();
        }
        public IActionResult Receptionen() {
            return View();
        }
        public IActionResult Oppettider() {
            return View();
        }


        [HttpPost]
        public IActionResult SendMail(IFormCollection fc) {
            MailViewModel mvm = new();

            if (ModelState.IsValid) {
                string formFrom = fc["Epost"];
                string formSubject = fc["Subject"];
                string formMessage = fc["Message"];


                string to = "christian.ohlsson@gmail.com";  // Mottagarens e-postadress
                string from = "imcoh@hv.se";                // Avsändarens e-postadress

                string mailIntro = "<h3>Detta är ett meddelande från formuläret på SportPalatset</h3>"
                                 + "Avsändare: <b>" + formFrom + "</b><br />"
                                 + "Meddelandet lyder:<br /><br />";

                string mailEnd = "<br /><br /><br /><br />"
                     + "-------------------------------------------------------------"
                     + "<p>Tänk på miljön och skriv inte ut detta mail</p>"
                     + "<img src='https://crille.org/sportpalatsetlogo.png' alt='Logo'>";

                MailMessage message = new(from, to);

                message.Subject = formSubject;
                message.Body = mailIntro + formMessage + mailEnd;
                message.BodyEncoding = Encoding.UTF8;
                message.IsBodyHtml = true;
                SmtpClient client = new("smtp.live.com", 587); // Outlook smtp    
                NetworkCredential basicCredential1 = new("imcoh@hv.se", "s3jsVYQc3Bs2N4MN");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = basicCredential1;
                try {
                    TempData["MeddelandeSuccess"] = "Ditt meddelande har skickats till SportPalatset.";
                    client.Send(message);
                }

                catch (Exception) {
                    throw;
                }
                return RedirectToAction(nameof(Index));
            }
            else {
                TempData["MeddelandeFail"] = "Mailet har inte skickats";
                return NotFound();
            }
        }
    }
}
