
// setAppNetworkType provides functions to change the Seekia application network type.
// Examples of network types: "Mainnet" and "Testnet1".
// The Seekia client will only interface with and download content for the current app network type.

package setAppNetworkType

// Each network type is represented by a single byte
// 1 = Mainnet
// 2 = Testnet1

import "seekia/internal/backgroundJobs"
import "seekia/internal/globalSettings"
import "seekia/internal/helpers"
import "seekia/internal/moderation/bannedModeratorConsensus"
import "seekia/internal/moderation/enabledModerators"
import "seekia/internal/moderation/verdictHistory"
import "seekia/internal/moderation/verifiedStickyStatus"
import "seekia/internal/mySettings"
import "seekia/internal/network/enabledHosts"

import "time"
import "errors"

func SetAppNetworkType(newNetworkType byte)error{

	isValid := helpers.VerifyNetworkType(newNetworkType)
	if (isValid == false){
		newNetworkTypeString := helpers.ConvertByteToString(newNetworkType)
		return errors.New("SetAppNetworkType called with unknown network type: " + newNetworkTypeString)
	}

	err := backgroundJobs.StopBackgroundJobs()
	if (err != nil) { return err }

	newNetworkTypeString := helpers.ConvertByteToString(newNetworkType)

	err = globalSettings.SetSetting("AppNetworkType", newNetworkTypeString)
	if (err != nil) { return err }

	// Now we update cache variables which are different for each network type
	// TODO: Add more

	err = bannedModeratorConsensus.UpdateBannedModeratorsList(newNetworkType)
	if (err != nil) { return err }

	err = enabledModerators.UpdateEnabledModeratorsList(newNetworkType)
	if (err != nil) { return err }

	err = enabledHosts.UpdateEnabledHostsList(newNetworkType)
	if (err != nil) { return err }

	//TODO: Clear temporary downloads for profiles/messages/attributes

	verdictHistory.DeleteVerdictHistory()

	verifiedStickyStatus.DeleteVerifiedStickyStatuses()

	// We must check for new chat messages
	// This is because when we generate chat messages, we only import messages for the current app network type
	err = mySettings.SetSetting("MateChatMessagesReadyStatus", "No")
	if (err != nil) { return err }

	err = mySettings.SetSetting("ModeratorChatMessagesReadyStatus", "No")
	if (err != nil) { return err }

	err = mySettings.SetSetting("MateChatConversationsGeneratedStatus", "No")
	if (err != nil) { return err }

	err = mySettings.SetSetting("ModeratorChatConversationsGeneratedStatus", "No")
	if (err != nil) { return err }

	
	err = mySettings.SetSetting("MatchesGeneratedStatus", "No")
	if (err != nil) { return err }

	err = mySettings.SetSetting("ViewedContentGeneratedStatus", "No")
	if (err != nil) { return err }

	err = mySettings.SetSetting("ViewedModeratorsGeneratedStatus", "No")
	if (err != nil) { return err }

	err = mySettings.SetSetting("ViewedHostsGeneratedStatus", "No")
	if (err != nil) { return err }

	err = backgroundJobs.StartBackgroundJobs()
	if (err != nil) { return err }

	// We simulate time, which we will remove eventually
	time.Sleep(time.Second)

	return nil
}

