package clientcmd

import (
	"fmt"

	clientconfig "github.com/deranjer/gvc/client/clientconfig"
)

// CreateBranch creates a new branch with the supplied name
func CreateBranch(conf *clientconfig.Gvcconfig, branchName string) error {
	branches := conf.LocalBranches
	for _, branch := range branches {
		if branch == branchName {
			return fmt.Errorf("Branch already exists, unable to create, use the switch command to switch to this branch: %s", branchName)
		}
	}
	conf.LocalBranches = append(conf.LocalBranches, branchName) //add the branch to the config
	// TODO Create the branch
	//If success, switch to new branch
	err := SwitchBranch(conf, branchName)
	if err != nil {
		return fmt.Errorf("error switching to new branch: %s", err)
	}
	return nil
}
