package entities

import (
	"github.com/DonovanKelly/sugar-n-spice/all"
	"go-nkode/config"
)

type KeypadDimension struct {
	AttrsPerKey int `json:"attrs_per_key"`
	NumbOfKeys  int `json:"numb_of_keys"`
}

func (kp *KeypadDimension) TotalAttrs() int {
	return kp.AttrsPerKey * kp.NumbOfKeys
}

func (kp *KeypadDimension) IsDispersable() bool {
	return kp.AttrsPerKey <= kp.NumbOfKeys
}

func (kp *KeypadDimension) IsValidKeypadDimension() error {
	if KeypadMin.AttrsPerKey > kp.AttrsPerKey || KeypadMax.AttrsPerKey < kp.AttrsPerKey || KeypadMin.NumbOfKeys > kp.NumbOfKeys || KeypadMax.NumbOfKeys < kp.NumbOfKeys {
		return config.ErrInvalidKeypadDimensions
	}
	return nil
}

func (kp *KeypadDimension) ValidKeySelections(selectedKeys []int) bool {
	return all.All[int](selectedKeys, func(idx int) bool {
		return 0 <= idx && idx < kp.NumbOfKeys
	})
}

func (kp *KeypadDimension) ValidateAttributeIndices(attrIndicies []int) bool {
	return all.All[int](attrIndicies, func(i int) bool {
		return i >= 0 && i < kp.TotalAttrs()
	})
}

var (
	KeypadMax = KeypadDimension{
		AttrsPerKey: 16,
		NumbOfKeys:  15,
	}

	KeypadMin = KeypadDimension{
		AttrsPerKey: 5,
		NumbOfKeys:  4,
	}

	KeypadDefault = KeypadDimension{
		AttrsPerKey: 14,
		NumbOfKeys:  7,
	}
)
