package readPaymentProofs_test

import "testing"

import "seekia/internal/network/readPaymentProofs"

import "seekia/internal/helpers"
import "seekia/internal/network/createPaymentProofs"
import "seekia/internal/identity"

import "slices"
import "errors"

func TestReadPaymentProofs(t *testing.T){

	testNetworkType := byte(1)

	testCryptocurrency := "Ethereum"

	getContentOrIdentityHash := func()([]byte, error){

		proofTypesList := []string{"Identity", "Profile", "Message", "Report"}

		randomProofType, err := helpers.GetRandomItemFromList(proofTypesList)
		if (err != nil) { return nil, err }

		switch randomProofType{

			case "Identity":{

				testIdentityHash, err := identity.GetNewRandomIdentityHash(false, "")
				if (err != nil) { return nil, err }

				result := testIdentityHash[:]

				return result, nil
			}
			case "Profile":{

				testProfileHash, err := helpers.GetNewRandomProfileHash(false, "", false, false)
				if (err != nil) { return nil, err }

				result := testProfileHash[:]

				return result, nil
			}
			case "Message":{

				testMessageHash, err := helpers.GetNewRandomMessageHash()
				if (err != nil){ return nil, err }

				result := testMessageHash[:]

				return result, nil
			}
			case "Report":{

				testReportHash, err := helpers.GetNewRandomReportHash(false, "")
				if (err != nil){ return nil, err }

				result := testReportHash[:]

				return result, nil
			}
		}

		return nil, errors.New("GetRandomItemFromList returning item not in list.")
	}

	testContentOrIdentityHash, err := getContentOrIdentityHash()
	if (err != nil){
		t.Fatal("getContentOrIdentityHash failed: " + err.Error())
	}

	testTreeNodeList := make([][32]byte, 0)

	randomInt := helpers.GetRandomIntWithinRange(0, 30)

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

		testTreeNode, err := helpers.GetNewRandom32ByteArray()
		if (err != nil){
			t.Fatal("GetNewRandom32ByteArray failed: " + err.Error())
		}

		testTreeNodeList = append(testTreeNodeList, testTreeNode)
	}

	newPaymentProof, err := createPaymentProofs.CreatePaymentProof(testNetworkType, testCryptocurrency, testContentOrIdentityHash, testTreeNodeList)
	if (err != nil){
		t.Fatal("CreatePaymentProof failed: " + err.Error())
	}

	ableToRead, _, paymentProofVersion, networkType_Received, contentOrIdentityHash_Received, cryptocurrencyName_Received, _, _, err := readPaymentProofs.ReadPaymentProofAndHash(newPaymentProof)
	if (err != nil){
		t.Fatal("ReadPaymentProof failed: " + err.Error())
	}
	if (ableToRead == false){
		t.Fatal("Failed to read payment proof.")
	}
	if (paymentProofVersion != 1){
		t.Fatal("ReadPaymentProof returning incorrect payment proof version")
	}
	if (testNetworkType != networkType_Received){
		t.Fatal("Failed to read payment proof: Network types do not match.")
	}
	areEqual := slices.Equal(testContentOrIdentityHash, contentOrIdentityHash_Received)
	if (areEqual == false){
		t.Fatal("Failed to read payment proof: contentOrIdentityHashes do not match.")
	}
	if (testCryptocurrency != cryptocurrencyName_Received){
		t.Fatal("Failed to read payment proof: cryptocurrencyName does not match.")
	}

	//TODO: Add more testing, such as verifying a cryptocurrency address output for a set of hardcoded inputs
}

