
// myIdentity provides functions to retrieve a user's identity keys and identity hashes

package myIdentity

import "seekia/internal/encoding"
import "seekia/internal/identity"
import "seekia/internal/mySeedPhrases"

import "errors"

//Outputs:
//	-bool: My identity hash exists for provided identityType
//	-[16]byte: My identity hash
//	-error
func GetMyIdentityHash(myIdentityType string) (bool, [16]byte, error){

	if (myIdentityType != "Mate" && myIdentityType != "Moderator" && myIdentityType != "Host"){
		return false, [16]byte{}, errors.New("GetMyIdentityHash called with invalid identity type: " + myIdentityType)
	}

	exists, myPublicIdentityKey, _, err := GetMyPublicPrivateIdentityKeys(myIdentityType)
	if (err != nil) { return false, [16]byte{}, err }
	if (exists == false) {
		return false, [16]byte{}, nil
	}

	identityHash, err := identity.ConvertIdentityKeyToIdentityHash(myPublicIdentityKey, myIdentityType)
	if (err != nil) { return false, [16]byte{}, err }

	return true, identityHash, nil
}

//Outputs:
//	-bool: Identity exists
//	-[32]byte: Public identity key
//	-[64]byte: Private identity key
//	-error
func GetMyPublicPrivateIdentityKeys(myIdentityType string) (bool, [32]byte, [64]byte, error){

	if (myIdentityType != "Mate" && myIdentityType != "Moderator" && myIdentityType != "Host"){
		return false, [32]byte{}, [64]byte{}, errors.New("GetMyPublicPrivateIdentityKeys called with invalid identity type: " + myIdentityType)
	}

	exists, mySeedPhraseHash, err := mySeedPhrases.GetMySeedPhraseHash(myIdentityType)
	if (err != nil) { return false, [32]byte{}, [64]byte{}, err }
	if (exists == false) {
		return false, [32]byte{}, [64]byte{}, nil
	}

	publicKeyArray, privateKeyArray, err := identity.GetPublicPrivateIdentityKeysFromSeedPhraseHash(mySeedPhraseHash)
	if (err != nil) { return false, [32]byte{}, [64]byte{}, err }

	return true, publicKeyArray, privateKeyArray, nil
}


// This function will verify that the identity key matches the requested identity hash
// This is necessary when you need to guarantee that the identity keys belong to the requested identity hash
//Outputs:
//	-bool: Identity exists
//	-[32]byte: Public identity key
//	-[64]byte: Private identity key
//	-error
func GetMyPublicPrivateIdentityKeysFromIdentityHash(inputIdentityHash [16]byte)(bool, [32]byte, [64]byte, error){

	identityType, err := identity.GetIdentityTypeFromIdentityHash(inputIdentityHash)
	if (err != nil) {
		identityHashHex := encoding.EncodeBytesToHexString(inputIdentityHash[:])
		return false, [32]byte{}, [64]byte{}, errors.New("GetMyPublicPrivateIdentityKeysFromIdentityHash called with invalid identity hash: " + identityHashHex)
	}

	exists, publicIdentityKey, privateIdentityKey, err := GetMyPublicPrivateIdentityKeys(identityType)
	if (err != nil) { return false, [32]byte{}, [64]byte{}, err }
	if (exists == false) {
		return false, [32]byte{}, [64]byte{}, nil
	}
	
	myIdentityHash, err := identity.ConvertIdentityKeyToIdentityHash(publicIdentityKey, identityType)
	if (err != nil) { return false, [32]byte{}, [64]byte{}, err }

	if (inputIdentityHash != myIdentityHash) {
		// Identity must have been replaced after identity hash was initially retrieved
		return false, [32]byte{}, [64]byte{}, nil
	}

	return true, publicIdentityKey, privateIdentityKey, nil
}


//Outputs:
//	-bool: Identity hash is mine
//	-string: IdentityType
//	-error
func CheckIfIdentityHashIsMine(inputIdentityHash [16]byte)(bool, string, error){

	identityType, err := identity.GetIdentityTypeFromIdentityHash(inputIdentityHash)
	if (err != nil) {
		inputIdentityHashHex := encoding.EncodeBytesToHexString(inputIdentityHash[:])
		return false, "", errors.New("CheckIfIdentityHashIsMine called with invalid identity hash: " + inputIdentityHashHex)
	}

	exists, myIdentityHash, err := GetMyIdentityHash(identityType)
	if (err != nil) { return false, "", err }
	if (exists == false) {
		return false, identityType, nil
	}
	if (myIdentityHash != inputIdentityHash) {
		return false, identityType, nil
	}

	return true, identityType, nil
}



