package gogopro

import (
	"io/ioutil"
)

type Power struct {
	APIRequester *APIRequester
}

func (p *Power) Init() *Power {
	return p
}

func CreatePower(APIRequester *APIRequester) *Power {
	power := &Power{}
	power.APIRequester = APIRequester
	return power
}

func (p *Power) GetPowerStatus() (string, error) {
	resp, err := p.APIRequester.get("/bacpac/se")
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if body[len(body)-1] == 0 {
		return "off", nil
	}
	return "on", nil
}

package gogopro

import ()

type Power struct {
	APIRequester   *APIRequester
	StatusCommands map[string]StatusCommand
}

func (p *Power) Init() *Power {
	return p
}

func CreatePower(APIRequester *APIRequester) *Power {
	power := &Power{}
	power.APIRequester = APIRequester
	statusCommands := CreateStatusCommands()
	power.StatusCommands = statusCommands
	return power
}

func CreateStatusCommands() map[string]StatusCommand {
	sc := make(map[string]StatusCommand)
	sc["power"] = StatusCommand{Endpoint: "/bacpac/se", ResultByte: -1,
		Translaters: []StatusTranslater{
			StatusTranslater{
				Result:         0,
				ExpectedReturn: "off"},
			StatusTranslater{
				Result:         1,
				ExpectedReturn: "on"}}}
	return sc
}

func (p *Power) GetPowerStatus() (string, error) {
	result, err := p.StatusCommands["power"].RunStatusCommand(p.APIRequester)
	if err != nil {
		return "", err
	}
	return result, nil
}

Move status command to generic object
Move status command to generic object

- The StatusCommand type now runs api command and determines the
  correct value to return.
- The Power type will create predetermined StatusCommands with
  translaters. Power functions will use the correct StatusCommand
  matching the function called.
