package main

import (
	"testing"
	"time"
)

type fakeTicker struct {
	CChan chan time.Time
}

func (f fakeTicker) C() <-chan time.Time { return f.CChan }
func (f fakeTicker) Stop()               {}

type fakeTimeProvider struct {
	currentTime time.Time
	sleepCalls  []time.Duration
	tickers     []fakeTicker
}

func (f *fakeTimeProvider) Now() time.Time {
	return f.currentTime
}

func (f *fakeTimeProvider) Sleep(d time.Duration) {
	f.sleepCalls = append(f.sleepCalls, d)
	f.currentTime = f.currentTime.Add(d)
}

func (f *fakeTimeProvider) NewTicker(d time.Duration) Ticker {
	c := make(chan time.Time, 1)
	t := fakeTicker{CChan: c}
	f.tickers = append(f.tickers, t)
	return t
}

func TestCreateSecondTickers(t *testing.T) {
	t.Skip("to long while dev")
	// Vorbereitung
	fakeNow := time.Date(2025, 7, 30, 12, 34, 45, 0, time.UTC)
	ftp := &fakeTimeProvider{currentTime: fakeNow}

	// Funktion aufrufen
	ch0, ch50 := createTickers(ftp)

	time.Sleep(15 * time.Second)
	// Channels auslösen
	if len(ftp.tickers) != 2 {
		t.Fatalf("expected 2 tickers, got %d", len(ftp.tickers))
	}

	// Simuliere Zeitereignisse
	ftp.tickers[0].CChan <- ftp.Now().Add(time.Minute)
	ftp.tickers[1].CChan <- ftp.Now().Add(time.Minute)

	select {
	case v := <-ch0:
		if v.Second() != 0 {
			t.Errorf("unexpected second on tick0: got %d", v.Second())
		}
	case <-time.After(time.Second):
		t.Error("timeout waiting for tick0")
	}

	select {
	case v := <-ch50:
		if v.Second() != 50 {
			t.Errorf("unexpected second on tick50: got %d", v.Second())
		}
	case <-time.After(time.Second):
		t.Error("timeout waiting for tick50")
	}
}
