package createMemos_test

import "seekia/internal/memos/createMemos"
import "seekia/internal/memos/readMemos"
import "seekia/internal/encoding"
import "seekia/internal/generate"
import "seekia/internal/helpers"
import "seekia/internal/identity"

import "bytes"
import "testing"


func TestCreateMemos(t *testing.T){

	for i := 0; i < 1000; i++{

		memoContent, err := generate.GetRandomText(100, true)
		if (err != nil){
			t.Fatal("Failed to GetRandomText: " + err.Error())
		}

		authorIdentityType, err := helpers.GetRandomItemFromList([]string{"Mate", "Host", "Moderator"})
		if (err != nil){
			t.Fatal("Failed to get new random identityType: " + err.Error())
		}

		authorIdentityPublicKey, authorIdentityPrivateKey, err := identity.GetNewRandomPublicPrivateIdentityKeys()
		if (err != nil){
			t.Fatal("Failed to get new random identity public/private keys: " + err.Error())
		}

		authorIdentityHash, err := identity.ConvertIdentityKeyToIdentityHash(authorIdentityPublicKey, authorIdentityType)
		if (err != nil){
			t.Fatal("Failed to get author identity hash: " + err.Error())
		}

		newMemo, err := createMemos.CreateMemo(authorIdentityPublicKey, authorIdentityPrivateKey, authorIdentityType, "««", "»»", memoContent)
		if (err != nil){
			t.Fatal("Failed to create memo: " + err.Error())
		}

		memoIsValid, _, receivedAuthorIdentityHash, unarmoredMemoContent, err := readMemos.ReadMemo(newMemo)
		if (err != nil){
			t.Fatal("Failed to read memo: " + err.Error())
		}
		if (memoIsValid == false){
			t.Fatal("Created memo is not valid.")
		}
		if (authorIdentityHash != receivedAuthorIdentityHash){
			receivedAuthorIdentityHashHex := encoding.EncodeBytesToHexString(receivedAuthorIdentityHash[:])
			t.Fatal("Received author identity hash does not match: " + receivedAuthorIdentityHashHex)
		}

		if (unarmoredMemoContent != memoContent){
			t.Fatal("Received unarmoredMemoContents does not match memoContents ")
		}
	}

	memoMessage := `Seekia: Be genetics aware.

This is a Seekia memo.`

	authorIdentityPublicKeyHex := "b26327a3e00e97a2759ac0a08a39c84f8edb833eec43669a81e27509f4ff6636"

	authorIdentityPublicKeyBytes, err := encoding.DecodeHexStringToBytes(authorIdentityPublicKeyHex)
	if (err != nil){
		t.Fatal("authorIdentityPublicKeyHex is invalid: Not Hex: " + err.Error())
	}

	authorIdentityPublicKeyArray := [32]byte(authorIdentityPublicKeyBytes)

	authorIdentityPrivateKeyHex := "c27bc4be0d7fdf18653f5ea4d81b56c89b9003013abaee9aa3afddc53ebf9726b26327a3e00e97a2759ac0a08a39c84f8edb833eec43669a81e27509f4ff6636"

	authorIdentityPrivateKeyBytes, err := encoding.DecodeHexStringToBytes(authorIdentityPrivateKeyHex)
	if (err != nil){
		t.Fatal("authorIdentityPrivateKeyHex is invalid: Not Hex: " + err.Error())
	}

	authorIdentityPrivateKeyArray := [64]byte(authorIdentityPrivateKeyBytes)

	newMemo, err := createMemos.CreateMemo(authorIdentityPublicKeyArray, authorIdentityPrivateKeyArray, "Mate", "<<", ">>", memoMessage)
	if (err != nil){
		t.Fatal("Failed to create memo: " + err.Error())
	}

	memoIsValid, memoHash, authorIdentityHash, _, err := readMemos.ReadMemo(newMemo)
	if (err != nil){
		t.Fatal("Failed to read memo: " + err.Error())
	}
	if (memoIsValid == false){
		t.Fatal("Created memo is not valid.")
	}

	expectedAuthorIdentityHashString := "p2qsphq4m72wr6trbyibwggsfem"
	
	expectedAuthorIdentityHash, _, err := identity.ReadIdentityHashString(expectedAuthorIdentityHashString)
	if (err != nil){
		t.Fatal("Failed to read expectedAuthorIdentityHashString: " + err.Error())
	}

	if (authorIdentityHash != expectedAuthorIdentityHash){
		authorIdentityHashHex := encoding.EncodeBytesToHexString(authorIdentityHash[:])
		t.Fatal("Received author identity hash does not match: " + authorIdentityHashHex)
	}

	memoHashExpected := "212e91ddf1103c58b229e0273e7e3063aa59112a8ff66b2451d856084f50ed7b"

	memoHashBytes, err := encoding.DecodeHexStringToBytes(memoHashExpected)
	if (err != nil){
		t.Fatal("Unable to read memoHashExpected: " + err.Error())
	}

	areEqual := bytes.Equal(memoHash[:], memoHashBytes)
	if (areEqual == false){
		memoHashHex := encoding.EncodeBytesToHexString(memoHash[:])
		t.Fatal("Received unexpected memo hash: " + memoHashHex)
	}

	ethereumAddress, err := readMemos.GetBlockchainAddressFromMemoHash("Ethereum", memoHash)
	if (err != nil){
		t.Fatal("GetBlockchainAddressFromMemoHash failed: " + err.Error())
	}

	if (ethereumAddress != "0x3e91f0CB0C2DC5b4b731756120fbDBf280Aa420e"){
		t.Fatal("GetBlockchainAddressFromMemoHash returning unexpected Ethereum address: " + ethereumAddress)
	}

	cardanoAddress, err := readMemos.GetBlockchainAddressFromMemoHash("Cardano", memoHash)
	if (err != nil){
		t.Fatal("GetBlockchainAddressFromMemoHash failed: " + err.Error())
	}

	if (cardanoAddress != "addr1v80q4u828wp7vgwhdtdp2vtf3yhet56ek5n46kpxf3p4fys7magv9"){
		t.Fatal("GetBlockchainAddressFromMemoHash returning unexpected Cardano address: " + cardanoAddress)
	}
}




