package main

import "log"

// BoardType Status
const (
	BoardTypeDummy   uint = 0
	BoardTypeGPIO    uint = 1
	BoardTypeI2CGPIO uint = 2
	BoardTypeI2CADC  uint = 3
)

// Board def
type Board struct {
	ChannelCount uint   `json:"channelcount"`
	ID           string `json:"id"`
	Name         string `json:"name"`
	Type         uint   `json:"type"`
	Bus          uint   `json:"bus"`
	Address      uint   `json:"address"`
	dummyValue   map[uint]bool
}

// ChannelName - return the name of a channel, useful for onboard GPIO
func (b *Board) ChannelName(num uint) string {
	return string(num)
}

// PowerON def
func (b *Board) PowerON(num uint) error {
	switch b.Type {
	case BoardTypeDummy:
		if b.dummyValue == nil {
			b.dummyValue = make(map[uint]bool)
		}
		b.dummyValue[num] = true
		return nil
	}

	return nil
}

// PowerOFF def
func (b *Board) PowerOFF(num uint) error {
	switch b.Type {
	case BoardTypeDummy:
		if b.dummyValue == nil {
			b.dummyValue = make(map[uint]bool)
		}
		b.dummyValue[num] = false
		return nil
	}
	return nil
}

// PowerToggle def
func (b *Board) PowerToggle(num uint) error {
	switch b.Type {
	case BoardTypeDummy:
		if b.dummyValue == nil {
			b.dummyValue = make(map[uint]bool)
		}
		v, _ := b.PowerStatus(num)
		log.Printf("toggle prestatus %v:%v", num, b.dummyValue[num])
		b.dummyValue[num] = !v
		log.Printf("toggle poststatus %v:%v", num, b.dummyValue[num])
		return nil
	}
	return nil
}

// PowerStatus def
func (b *Board) PowerStatus(num uint) (bool, error) {
	switch b.Type {
	case BoardTypeDummy:
		if b.dummyValue == nil {
			b.dummyValue = make(map[uint]bool)
		}
		val, ok := b.dummyValue[num]
		if !ok {
			b.dummyValue[num] = false
			val = false
		}
		log.Printf("status %v:%v", num, val)
		return val, nil
	}
	return false, nil
}
