package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"regexp"
	"strings"

	"github.com/sendgrid/sendgrid-go"
	"github.com/sendgrid/sendgrid-go/helpers/mail"
)

//SEE https://github.com/xhit/go-simple-mail

// CheckAddressMaillist ...
func CheckAddressMaillist(maillist string) string {
	strings.Replace(maillist, "noreply@aske-futurskill.com;", "", -1)
	maillistArray := strings.Split(maillist, ";")

	var index = 0
	re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
	var newMaillist = ""
	for _, elem := range maillistArray {
		if re.MatchString(elem) {
			if index > 0 {
				newMaillist += ";"
			}
			newMaillist += elem
			index++
		}
	}
	return newMaillist
}

// GetMailtoFile ...
func GetMailtoFile(filename string) []string {
	file, err := os.Open(filename)
	if err != nil {
		log.Panic(err)
	}
	scanner := bufio.NewScanner(file)
	scanner.Split(bufio.ScanLines)
	var txtlines []string

	for scanner.Scan() {
		txtlines = append(txtlines, scanner.Text())
	}
	file.Close()
	return txtlines
}

// FileHTMLToString ...
func FileHTMLToString(filehtml string) string {
	b, err := ioutil.ReadFile(filehtml) // just pass the file name
	if err != nil {
		fmt.Print(err)
	}
	return string(b)
}

func main() {
	filemail := os.Args[1]
	subject := os.Args[2]
	filehtml := os.Args[3]

	from := mail.NewEmail("Support ASkE", "noreply@aske-futurskill.com")
	subject := subject
	to := mail.NewEmail("Example User", "test@example.com")
	plainTextContent := "and easy to do anywhere, even with Go"
	htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
	message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
	client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
	response, err := client.Send(message)
	if err != nil {
		log.Println(err)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}

/*
func main() {
	filemail := os.Args[1]
	subject := os.Args[2]
	filehtml := os.Args[3]

	bodyHTML := FileHTMLToString(filehtml)
	maillingList := GetMailtoFile(filemail)
	for _, email := range maillingList {
		mailjetClient := mailjet.NewMailjetClient(os.Getenv("c4832eafdd5b13635a5d1273cb998d3f"), os.Getenv("ad985e178e340b85e192097b02026429"))
		messagesInfo := []mailjet.InfoMessagesV31{
			mailjet.InfoMessagesV31{
				From: &mailjet.RecipientV31{
					Email: "noreply@aske-futurskill.com",
					Name:  "Support ASkE",
				},
				To: &mailjet.RecipientsV31{
					mailjet.RecipientV31{
						Email: email,
					},
				},
				Subject:  subject,
				HTMLPart: bodyHTML,
			},
		}
		messages := mailjet.MessagesV31{Info: messagesInfo}
		res, err := mailjetClient.SendMailV31(&messages)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("Data: %+v\n", res)
	}
}
*/
/*
func main() {

	filemail := os.Args[1]
	subject := os.Args[2]
	filehtml := os.Args[3]

	smtpServ := xmail.NewSMTPClient()
	smtpServ.Host = "in-v3.mailjet.com"
	smtpServ.Port = 587
	smtpServ.Username = "c4832eafdd5b13635a5d1273cb998d3f"
	smtpServ.Password = "ad985e178e340b85e192097b02026429"
	smtpServ.Encryption = mail.EncryptionTLS

<<<<<<< HEAD
	// smtpServ.Authentication = mail.AuthPlain

	smtpServ.KeepAlive = true
=======
	smtpServ.KeepAlive = false
>>>>>>> b054138d39de67cfda98ddbb637bce7e1463bc7e
	smtpServ.ConnectTimeout = 10 * time.Second
	smtpServ.SendTimeout = 10 * time.Second

	smtpClient, err := smtpServ.Connect()
	if err != nil {
		log.Fatal(err)
	}

	bodyHTML := FileHTMLToString(filehtml)
	maillingList := GetMailtoFile(filemail)
	for _, email := range maillingList {
		fmt.Println(email)
		mail := PrepareMail(email, subject, bodyHTML)
		err = mail.Send(smtpClient)

		if err != nil {
			log.Println(err)
		} else {
			log.Println("Email sent")
		}
	}
}
*/
