package cmdstr

import (
	"strconv"
	"strings"
)

type BROptions struct {
	Storage string
	PD      []string
}

type BR struct {
	path    string
	subcmds []string
	options []string
}

// NewBR creates a BR cmdstr object.
func NewBR(path string) *BR {
	return &BR{path: path}
}

func (br *BR) BackupFull() *BR {
	br.subcmds = []string{"backup", "full"}
	return br
}

func (br *BR) RestoreFull() *BR {
	br.subcmds = []string{"restore", "full"}
	return br
}

func (br *BR) CommonOptions(opts *BROptions) *BR {
	for _, pd := range opts.PD {
		br.options = append(br.options, []string{"--pd", pd}...)
	}
	// Storage string may look like: `s3://xx/yy?access-key=xx&secret-access-key=xx`.
	br.options = append(br.options, []string{"--storage", strconv.Quote(opts.Storage)}...)
	return br
}

func (br *BR) String() string {
	arr := []string{}
	arr = append(arr, br.path)
	arr = append(arr, br.subcmds...)
	arr = append(arr, br.options...)
	return strings.Join(arr, " ")
}
