package readMemos_test


import "seekia/internal/memos/readMemos"
import "seekia/internal/identity"
import "seekia/internal/encoding"

import "bytes"
import "testing"


func TestReadMemo(t *testing.T){

	testMemo := `| << Seekia Memo >>
|
|- Signature:
|    MrVojUgnBTQp0RWoqCQGEeVLIn8gAs
|    B1OxP3AxOwKGW5AYH3ZjSJiThfcupq
|    hG2j3UkvQIu4SBIeSV3j95yBCg==
|
|- Identity Key:
|    b26327a3e00e97a2759ac0a08a39c84f
|    8edb833eec43669a81e27509f4ff6636
|
|- Author:
|    p2qsphq4m72wr6trbyibwggsfem
|
|- Memo:
|
|    Seekia: Be genetics aware.
|
|    This is a Seekia memo.
|
| << End Of Memo >>`


	memoIsValid, memoHash, authorIdentityHash, unarmoredMemoContent, err := readMemos.ReadMemo(testMemo)
	if (err != nil){
		t.Fatal("Failed to read memo: " + err.Error())
	}
	if (memoIsValid == false){
		t.Fatal("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)
	}

	expectedUnarmoredMemoContent := `Seekia: Be genetics aware.

This is a Seekia memo.`

	if (unarmoredMemoContent != expectedUnarmoredMemoContent){
		t.Fatal("Unexpected unarmoredMemoContent: " + unarmoredMemoContent)
	}
}


// We use this to verify a legacy memo, where lines with no content were preceded with a pipe and 4 whitespace characters
// Now memos do not contain the 4 whitespace characters for these lines. They only contain a single pipe.
func TestReadMemo_Legacy(t *testing.T){

	testMemo := `| «« Seekia Memo »»
|
|- Signature:
|    BZ8XRI1MpMsIUSAXKwCcl_kvi_InPU
|    llBmnV7_xhJhhUtu8ICltCarCNDAhD
|    8dzOIywc6txZWyU9mAVNjnUMBQ==
|
|- Identity Key:
|    c7cb2d0edfb3191dd2b6b6519cd04d49
|    7552415e4d02fe5bb6b485dd0bf8c1f3
|
|- Author:
|    cohqu7plj76i6ndyw5mfg6ypd6r
|
|- Memo:
|
|    This is a legacy Seekia memo.
|    
|    It is encoded differently from current memos.
|    
|    In legacy memos, lines without any content contain unnecessary whitespace.
|
| «« End Of Memo »»`

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

	expectedAuthorIdentityHashString := "cohqu7plj76i6ndyw5mfg6ypd6r"

	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 := "5c25357e28a331044f9ac3fa045d5d510c4dd2f3cf42d9bbaf32e477a1674ab0"

	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 != "0xe15139F38b36b7c82c7F61F8d819971dDBBfA698"){
		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 != "addr1v8d6v893q05j6d47kz3dkvl9fpgy67r5c3qe4tgd4tyga0c27zq33"){
		t.Fatal("GetBlockchainAddressFromMemoHash returning unexpected Cardano address: " + cardanoAddress)
	}

	expectedUnarmoredMemoContent := `This is a legacy Seekia memo.

It is encoded differently from current memos.

In legacy memos, lines without any content contain unnecessary whitespace.`

	if (unarmoredMemoContent != expectedUnarmoredMemoContent){
		t.Fatal("Unexpected unarmoredMemoContent: " + unarmoredMemoContent)
	}
}



