package pingo

import (
	"testing"
	"time"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/stretchr/testify/assert"
)

func Test_InitialModel(t *testing.T) {
	model := InitialModel([]string{"test"}, time.Duration(time.Second*1), 10)

	assert.True(t, len(model.Addresses) == 1)
	assert.True(t, model.viewport.Width == 0)
	assert.Equal(t, model.UpdateSpeed, time.Duration(time.Second*1))
	assert.Equal(t, model.ChartHeight, 10)
}

func Test_Model_Init(t *testing.T) {
	testModel := Model{}
	result := testModel.Init()
	assert.IsType(t, new(tea.Cmd), &result)

	result2 := result()
	assert.IsType(t, new(tea.Msg), &result2)

	_, ok := result2.(tickMsg)
	assert.True(t, ok)
}

func Test_Model_Update_window_resize(t *testing.T) {
	testMsg := tea.WindowSizeMsg{Width: 10, Height: 10}
	model := tea.Model(InitialModel([]string{"test"}, time.Duration(time.Second*1), 10))
	assert.Equal(t, 0, model.(Model).width)
	assert.Equal(t, 0, model.(Model).viewport.YPosition)
	assert.Equal(t, 1, len(model.(Model).Addresses))
	assert.Equal(t, 80, model.(Model).Addresses[0].max_results)

	model, _ = model.Update(testMsg)
	assert.Equal(t, 10, model.(Model).width)
	assert.Equal(t, 10, model.(Model).viewport.Width)
	assert.Equal(t, 6, model.(Model).viewport.Height)
	assert.Equal(t, 1, model.(Model).viewport.YPosition)
	assert.Equal(t, 10, model.(Model).Addresses[0].max_results)
}

func Test_Model_Update_keymsg_j(t *testing.T) {
	t.Skip()
	testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
	testMsg := tea.KeyMsg(testKey)
	model := tea.Model(InitialModel([]string{"127.0.0.1", "127.0.0.1"}, time.Duration(time.Second*1), 0))
	scrollPercent := model.(Model).viewport.ScrollPercent()
	var msgs []tea.Msg
	var cmd tea.Cmd
	model, cmd = model.Update(tea.WindowSizeMsg{Width: 100, Height: 2000})
	if cmd != nil {
		msgs = append(msgs, cmd())
	}
	msgs = append(msgs, testMsg)
	model, _ = model.Update(msgs)
	assert.NotEqual(t, scrollPercent, model.(Model).viewport.ScrollPercent())
}

func Test_Model_View(t *testing.T)    {}
func Test_Model_content(t *testing.T) {}
func Test_Model_Poll(t *testing.T)    {}
