package repostatus

import (
	"errors"
	"fmt"
	"time"

	"go.wit.com/log"
)

/*
func (rs *RepoStatus) gitBranchAll() {
	r := rs.Run([]string{"git", "branch", "--all"})
	if r.Error != nil {
		log.Log(WARN, "git branch failed string =", rs.Path())
		log.Log(WARN, "git branch failed realpath =", rs.realPath.String())
		return
	}
	for _, s := range r.Stdout {
		rs.targetBranch.AddText(s)
	}
}
*/

func (rs *RepoStatus) UpdateNew() {
	log.Info("gui update", rs.pb.GetFullPath())
	rs.updateNew()
}

func (rs *RepoStatus) updateNew() {
	if !rs.Ready() {
		log.Log(WARN, "can't update yet. ready is false")
		log.Error(errors.New("Update() is not ready yet"))
		return
	}

	pb := rs.pb

	// store the current checked out branch name and version
	rs.checkCurrentBranchName()
	rs.checkCurrentBranchVersion()

	// read in the tags
	rs.populateTags()

	// record if the repo is dirty
	pb.CheckDirty()

	// store the last tag version
	rs.setLastTagVersion()

	// store the master branch version
	ver := pb.GetMasterVersion()
	rs.mainBranchVersion.SetValue(ver)

	rs.develBranchVersion.SetValue(pb.GetDevelVersion())
	rs.userBranchVersion.SetValue(pb.GetUserVersion())

	// populates a string into the rs.gitState widget
	// todo: make the values from this function a bit cleaner
	rs.CheckGitState()
}

func (rs *RepoStatus) Update() {
	if !rs.Ready() {
		log.Log(WARN, "can't update yet. ready is false")
		log.Error(errors.New("Update() is not ready yet"))
		return
	}
	log.Log(INFO, "Update() START")
	duration := timeFunction(func() {
		rs.updateNew()
	})
	rs.setSpeed(duration)
	log.Log(INFO, "Update() END")
}

func (rs *RepoStatus) setSpeed(duration time.Duration) {
	s := fmt.Sprint(duration)
	if rs.speedActual == nil {
		log.Log(WARN, "rs.speedActual == nil")
		return
	}
	rs.speedActual.SetValue(s)

	if duration > 200*time.Millisecond {
		rs.speed.SetValue("SLOW")
	} else if duration > 50*time.Millisecond {
		rs.speed.SetValue("OK")
	} else {
		rs.speed.SetValue("FAST")
	}
}

// disable all things besides Update() button
func (rs *RepoStatus) DisableEverything() {
	log.Log(INFO, "DisableEverything()")

	// choosing a major, minor or revision
	rs.major.Disable()
	rs.minor.Disable()
	rs.revision.Disable()

	// disable adding a tag message
	rs.versionMessage.Disable()

	// disable the merge devel to master button
	rs.develMergeB.Disable()

	// disable the tag a new version button
	rs.releaseVersion.Disable()
}

// this means devel needs to be merged to master
func (rs *RepoStatus) EnableMergeDevel() {
	rs.DisableEverything()

	rs.develMergeB.Enable()
}

func (rs *RepoStatus) Disable() {
	rs.window.Disable()
}

func (rs *RepoStatus) Enable() {
	rs.window.Enable()
}

// this means you need to release a new version of the master repository
func (rs *RepoStatus) EnableSelectTag() {
	rs.DisableEverything()

	// choosing a major, minor or revision
	rs.major.Enable()
	rs.minor.Enable()
	rs.revision.Enable()

	// disable adding a tag message
	rs.versionMessage.Enable()

	rs.develMergeB.SetLabel("ready to release")

	if len(rs.versionMessage.String()) == 0 {
		// force there to be a commit message
		rs.releaseVersion.Disable()
	} else {
		// rs.generateCmd()
		rs.releaseVersion.Enable()
	}
}