package nkode

import (
	"github.com/stretchr/testify/assert"
	"go-nkode/core/model"
	"testing"
)

func TestCustomer(t *testing.T) {
	testNewCustomerAttributes(t)
	testCustomerValidKeyEntry(t)
	testCustomerIsValidNKode(t)
}

func testNewCustomerAttributes(t *testing.T) {
	// keypad := m.KeypadDimension{AttrsPerKey: 10, NumbOfKeys: 5}
	_, nil := model.NewCustomerAttributes()
	assert.NoError(t, nil)
}

func testCustomerValidKeyEntry(t *testing.T) {
	kp := model.KeypadDimension{AttrsPerKey: 10, NumbOfKeys: 9}
	nkodePolicy := model.NewDefaultNKodePolicy()
	customer, err := model.NewCustomer(nkodePolicy)
	assert.NoError(t, err)
	newUserInterface, err := model.NewUserInterface(&kp)
	assert.NoError(t, err)
	username := model.Username("testing123")
	passcodeIdx := []int{0, 1, 2, 3}
	user, err := NewUser(*customer, username, passcodeIdx, *newUserInterface, kp)
	assert.NoError(t, err)
	userLoginInterface, err := user.GetLoginInterface()
	assert.NoError(t, err)
	selectedKeys, err := model.SelectKeyByAttrIdx(userLoginInterface, passcodeIdx, kp)
	assert.NoError(t, err)
	validatedPasscode, err := ValidKeyEntry(*user, *customer, selectedKeys)
	assert.NoError(t, err)
	assert.Equal(t, len(validatedPasscode), len(passcodeIdx))
	for idx := range validatedPasscode {
		assert.Equal(t, validatedPasscode[idx], passcodeIdx[idx])
	}
}

func testCustomerIsValidNKode(t *testing.T) {
	kp := model.KeypadDimension{AttrsPerKey: 10, NumbOfKeys: 7}
	nkodePolicy := model.NewDefaultNKodePolicy()
	customer, err := model.NewCustomer(nkodePolicy)
	assert.NoError(t, err)
	newUserInterface, err := model.NewUserInterface(&kp)
	assert.NoError(t, err)
	username := model.Username("testing123")
	passcodeIdx := []int{0, 1, 2, 3}
	user, err := NewUser(*customer, username, passcodeIdx, *newUserInterface, kp)
	assert.NoError(t, err)
	err = customer.IsValidNKode(user.Kp, passcodeIdx)
	assert.NoError(t, err)
}
