package contact

import (
	"fmt"
	"io/ioutil"
	"gitea.inhome.blapointe.com/local/encryptor"
	"os"
	"testing"
)

func Test_Emailer_Send(t *testing.T) {
	em := NewEmailer("bbarl64@gmail.com")
	private, public := encryptor.NewKeyPair()
	if _, err := os.Stat("../system/keys/pri.key"); os.IsNotExist(err) {
		ioutil.WriteFile("../system/keys/pri.key", []byte(private), 0777)
	}
	if _, err := os.Stat("../system/keys/pub.key"); os.IsNotExist(err) {
		ioutil.WriteFile("../system/keys/pub.key", []byte(public), 0777)
	}

	if pribytes, err := ioutil.ReadFile("../system/keys/pri.key"); err == nil {
		private = string(pribytes)
	} else {
		panic(err)
	}
	if pubbytes, err := ioutil.ReadFile("../system/keys/pub.key"); err == nil {
		public = string(pubbytes)
	} else {
		panic(err)
	}
	enc := encryptor.NewEncryptor(private, public)

	msg := "hello to the world out there!"
	if err := em.Send("test_encryptor", enc.Encrypt(msg)); err != nil {
		panic(err)
	}
	ioutil.WriteFile("./last.msg", []byte(enc.Encrypt(msg)), 0777)
}

func Test_Emailer_Check(t *testing.T) {
	var private, public string
	if pribytes, err := ioutil.ReadFile("../system/keys/pri.key"); err == nil {
		private = string(pribytes)
	} else {
		panic(err)
	}
	if pubbytes, err := ioutil.ReadFile("../system/keys/pub.key"); err == nil {
		public = string(pubbytes)
	} else {
		panic(err)
	}
	enc := encryptor.NewEncryptor(private, public)
	em := NewEmailer("bbarl64@gmail.com")
	emails := em.Check()
	if len(emails) > 0 {
		fmt.Println(emails[0])
		fmt.Println("Decrypted:", enc.Decrypt(emails[0].Body))
	}
}
