//icomsys callback timer
package icscbtimer

import (
	"time"
)

type IcsCBTimerFunc func(t *IcsCBTimer)

type IcsCBTimer struct {
	ticker *time.Ticker
	d      time.Duration
	cbfunc IcsCBTimerFunc
}

func NewCBTimer(d time.Duration, callbackfunc IcsCBTimerFunc) *IcsCBTimer {
	if d <= 0 || callbackfunc == nil {
		return nil
	}

	it := IcsCBTimer{d: d, cbfunc: callbackfunc}

	return &it
}

func (t IcsCBTimer) GetTick() <-chan time.Time {
	return t.ticker.C
}

func (t *IcsCBTimer) Start() {
	if t == nil {
		return
	}

	t.ticker = time.NewTicker(t.d)
	go t.cbfunc(t)
}

func (t *IcsCBTimer) Stop() {
	if t == nil || t.ticker == nil {
		return
	}

	t.ticker.Stop()
}
