package datastore

import (
	"code.google.com/p/go.net/context"
	"github.com/drone/drone/shared/model"
)

type Commitstore interface {
	// GetCommit retrieves a commit from the
	// datastore for the given ID.
	GetCommit(id int64) (*model.Commit, error)

	// GetCommitSha retrieves a commit from the
	// datastore for the specified repo and sha
	GetCommitSha(repo *model.Repo, branch, sha string) (*model.Commit, error)

	// GetCommitLast retrieves the latest commit
	// from the datastore for the specified repository
	// and branch.
	GetCommitLast(repo *model.Repo, branch string) (*model.Commit, error)

	// GetCommitList retrieves a list of latest commits
	// from the datastore for the specified repository.
	GetCommitList(repo *model.Repo) ([]*model.Commit, error)

	// GetCommitListUser retrieves a list of latest commits
	// from the datastore accessible to the specified user.
	GetCommitListUser(user *model.User) ([]*model.CommitRepo, error)

	// GetCommitListActivity retrieves an ungrouped list of latest commits
	// from the datastore accessible to the specified user.
	GetCommitListActivity(user *model.User) ([]*model.CommitRepo, error)

	// PostCommit saves a commit in the datastore.
	PostCommit(commit *model.Commit) error

	// PutCommit saves a commit in the datastore.
	PutCommit(commit *model.Commit) error

	// DelCommit removes the commit from the datastore.
	DelCommit(commit *model.Commit) error

	// KillCommits updates all pending or started commits
	// in the datastore settings the status to killed.
	KillCommits() error
}

// GetCommit retrieves a commit from the
// datastore for the given ID.
func GetCommit(c context.Context, id int64) (*model.Commit, error) {
	return FromContext(c).GetCommit(id)
}

// GetCommitSha retrieves a commit from the
// datastore for the specified repo and sha
func GetCommitSha(c context.Context, repo *model.Repo, branch, sha string) (*model.Commit, error) {
	return FromContext(c).GetCommitSha(repo, branch, sha)
}

// GetCommitLast retrieves the latest commit
// from the datastore for the specified repository
// and branch.
func GetCommitLast(c context.Context, repo *model.Repo, branch string) (*model.Commit, error) {
	return FromContext(c).GetCommitLast(repo, branch)
}

// GetCommitList retrieves a list of latest commits
// from the datastore for the specified repository.
func GetCommitList(c context.Context, repo *model.Repo) ([]*model.Commit, error) {
	return FromContext(c).GetCommitList(repo)
}

// GetCommitListUser retrieves a list of latest commits
// from the datastore accessible to the specified user.
func GetCommitListUser(c context.Context, user *model.User) ([]*model.CommitRepo, error) {
	return FromContext(c).GetCommitListUser(user)
}

// GetCommitListActivity retrieves an ungrouped list of latest commits
// from the datastore accessible to the specified user.
func GetCommitListActivity(c context.Context, user *model.User) ([]*model.CommitRepo, error) {
	return FromContext(c).GetCommitListActivity(user)
}

// PostCommit saves a commit in the datastore.
func PostCommit(c context.Context, commit *model.Commit) error {
	return FromContext(c).PostCommit(commit)
}

// PutCommit saves a commit in the datastore.
func PutCommit(c context.Context, commit *model.Commit) error {
	return FromContext(c).PutCommit(commit)
}

// DelCommit removes the commit from the datastore.
func DelCommit(c context.Context, commit *model.Commit) error {
	return FromContext(c).DelCommit(commit)
}

// KillCommits updates all pending or started commits
// in the datastore settings the status to killed.
func KillCommits(c context.Context) error {
	return FromContext(c).KillCommits()
}