
// myLocalDesires provides functions to manage a user's mate desires

package myLocalDesires

import "seekia/internal/myDatastores/myMap"
import "seekia/internal/mySettings"

import "errors"

var myDesiresMapDatastore *myMap.MyMap

// This function must be called whenever we sign in to an app user
func InitializeMyDesiresDatastore()error{

	newMyDesiresMapDatastore, err := myMap.CreateNewMap("MyDesires")
	if (err != nil) { return err }

	myDesiresMapDatastore = newMyDesiresMapDatastore

	return nil
}

//Outputs:
//	-bool: Desire exists
//	-string: Desire data
//	-error
func GetDesire(desireName string)(bool, string, error){

	if (desireName == ""){
		return false, "", errors.New("GetDesire called with empty desireName.")
	}

	exists, desireValue, err := myDesiresMapDatastore.GetMapEntry(desireName)
	if (err != nil) { return false, "", err }
	if (exists == false){

		return false, "", nil
	}

	return true, desireValue, nil
}


func SetDesire(desireName string, content string)error{

	if (desireName == ""){
		return errors.New("SetDesire called with empty desireName.")
	}
	if (content == ""){
		return errors.New("SetDesire called with empty content.")
	}

	err := myDesiresMapDatastore.SetMapEntry(desireName, content)
	if (err != nil) { return err }

	// Matches must be regenerated
	err = mySettings.SetSetting("MatchesGeneratedStatus", "No")
	if (err != nil) { return err }

	return nil
}

func DeleteDesire(desireName string)error{

	if (desireName == ""){
		return errors.New("DeleteDesire called with empty desireName.")
	}

	err := myDesiresMapDatastore.DeleteMapEntry(desireName)
	if (err != nil) { return err }

	// Matches must be regenerated
	err = mySettings.SetSetting("MatchesGeneratedStatus", "No")
	if (err != nil) { return err }
	
	return nil
}



