package sync

import (
	"fmt"
	"time"

	"git.csclub.uwaterloo.ca/public/merlin/config"
)

// Start sync job for the repo if more than repo.Frequency seconds have elapsed since its last job
// and is not currently running. Returns true iff a job is started.
func SyncIfPossible(repo *config.Repo, force bool) bool {
	if repo.State.IsRunning {
		return false
	}

	curTime := time.Now().Unix()
	if force || curTime-repo.State.LastAttemptStartTime > int64(repo.Frequency) {
		repo.State.IsRunning = true
		repo.State.LastAttemptStartTime = curTime
		repo.SaveState()
		repo.Logger.Info(fmt.Sprintf("Repo %s has started syncing", repo.Name))
		go startRepoSync(repo)
		return true
	}
	return false
}

// Called after a repo completes a sync. Update the repo's status using the current time and the exit code.
func SyncCompleted(repo *config.Repo, exit int) {
	repoState := repo.State

	syncTook := time.Now().Unix() - repoState.LastAttemptStartTime
	nextSync := repo.Frequency - int(syncTook)
	if nextSync < 0 {
		nextSync = 0
	}
	repoState.IsRunning = false
	repoState.LastAttemptExit = exit
	repoState.LastAttemptRunTime = syncTook
	repo.SaveState()

	exitStr := "failed"
	if exit == config.SUCCESS {
		exitStr = "completed"
	} else if exit == config.TERMINATED {
		exitStr = "terminated"
	}
	repo.Logger.Info(fmt.Sprintf("Sync %s after running for %d seconds, will run again in %d seconds", exitStr, syncTook, nextSync))

	if exit == config.SUCCESS {
		go zfsSync(repo)
	}
}
