
// backgroundDownloads provides functions to check what content the app is downloading in the background
// One example is checking if the app can determine a message/identity's verdict
// To determine an identity's verdict, the app must be downloading all moderator profiles, all moderator ban reviews, and all reviews for the identity

package backgroundDownloads

import "seekia/internal/helpers"
import "seekia/internal/myRanges"
import "seekia/internal/network/appNetworkType/getAppNetworkType"
import "seekia/internal/network/temporaryDownloads"

import "errors"

// This function will tell us if we are downloading the required reviews to determine this identity's moderation verdict
// This includes the verdict for all profiles created by this identity
// To do this, we must download all moderator profiles, all moderator-banning-moderator reviews, and all reviews of the identity
func CheckIfAppCanDetermineIdentityVerdicts(identityHash [16]byte)(bool, error){

	// We first check if the provided identity hash is in our host/moderator range
	// If it is, we will download all reviews/reports for the identity and its profiles
	// In host/moderator mode, we download all moderator profiles and and all moderator-banning-moderator reviews
	// In both modes, if the identity is within our range, we will download all of this identity's profiles
	// If we are in moderator mode, we will delete their profiles after we have reviewed them, and retain each profile's metadata

	hostModeEnabled, isWithinRange, err := myRanges.CheckIfIdentityHashIsWithinMyHostedRange(identityHash)
	if (err != nil) { return false, err }
	if (hostModeEnabled == true && isWithinRange == true){
		return true, nil
	}

	moderatorModeEnabled, isWithinRange, err := myRanges.CheckIfIdentityHashIsWithinMyModerationRange(identityHash)
	if (err != nil) { return false, err }
	if (moderatorModeEnabled == true && isWithinRange == true){
		return true, nil
	}

	if (hostModeEnabled == false && moderatorModeEnabled == false){
		// We cannot determine the identity's verdict
		// This is because we are not downloading all moderator profiles and all moderator-banning-moderator reviews
		return false, nil
	}

	// Now we check our temporary downloads

	isWithinTemporaryDownloads, err := temporaryDownloads.CheckIfIdentityReviewsAreWithinMyTemporaryDownloads(identityHash)
	if (err != nil) { return false, err }
	if (isWithinTemporaryDownloads == true){
		return true, nil
	}

	return false, nil
}

// This function will tell us if we are downloading the required reviews to determine this message's moderation verdict
// To do this, we must download all moderator profiles, all moderator-banning-moderator reviews, and all reviews of the message
func CheckIfAppCanDetermineMessageVerdict(messageNetworkType byte, messageInbox [10]byte, messageHashProvided bool, messageHash [26]byte)(bool, error){

	isValid := helpers.VerifyNetworkType(messageNetworkType)
	if (isValid == false){
		messageNetworkTypeString := helpers.ConvertByteToString(messageNetworkType)
		return false, errors.New("CheckIfAppCanDetermineMessageVerdict called with invalid messageNetworkType: " + messageNetworkTypeString)
	}

	// We first check to see if our client is connected to the same network type the message was broadcasted on

	appNetworkType, err := getAppNetworkType.GetAppNetworkType()
	if (err != nil) { return false, err }
	if (appNetworkType != messageNetworkType){
		return false, nil
	}

	// We then check if the provided inbox is in our host/moderator range
	// If it is, we will download all reviews/reports for any messages sent to the inbox
	// In host/moderator mode, we download all moderator profiles and and all moderator-banning-moderator reviews
	// If we are in Host mode, we will also download all messages sent to the inbox, regardless of if they are reviewed/reported.
	// If we are in moderator mode, we will download only reviewed/reported messages,
	// 		delete the messages after we have reviewed them, and retain each message's metadata

	hostModeEnabled, isWithinRange, err := myRanges.CheckIfMessageInboxIsWithinMyHostedRange(messageInbox)
	if (err != nil) { return false, err }
	if (hostModeEnabled == true && isWithinRange == true){
		return true, nil
	}

	moderatorModeEnabled, isWithinRange, err := myRanges.CheckIfMessageInboxIsWithinMyModerationRange(messageInbox)
	if (err != nil) { return false, err }
	if (moderatorModeEnabled == true && isWithinRange == true){
		return true, nil
	}

	if (hostModeEnabled == false && moderatorModeEnabled == false){
		// We cannot determine the message/inbox verdict
		// This is because we are not downloading all moderator profiles and all moderator-banning-moderator reviews
		return false, nil
	}

	// Now we check our temporary downloads

	if (messageHashProvided == false){
		// Temporary downloads do not exist for message inboxes
		// Thus, we are not download the required reviews for this inbox
		return false, nil
	}

	isWithinMyTemporaryDownloads, err := temporaryDownloads.CheckIfMessageReviewsAreWithinMyTemporaryDownloads(messageHash)
	if (err != nil) { return false, err }
	if (isWithinMyTemporaryDownloads == true){
		return true, nil
	}

	return false, nil
}




