from threading import Timer
from .view import *

class TimelineView(PUINode):
    def __init__(self, ttl_sec):
        super().__init__()
        self.ttl_sec = ttl_sec

    def update(self, prev):
        if prev and hasattr(prev, "timer"):
            self.timer = prev.timer
        else:
            self.timer = Timer(self.ttl_sec, self.timer_cb)
            self.timer.start()

    def timer_cb(self):
        try:
            self.root.redraw()
            self.timer = Timer(self.ttl_sec, self.timer_cb)
            self.timer.start()
        except:
            pass

    @property
    def ui(self):
        return self.children[0].ui

    @ui.setter
    def ui(self, new_ui):
        pass

    def destroy(self):
        timer = self.timer
        self.timer = None
        if timer:
            timer.cancel()

    def addChild(self, idx, child):
        self.parent.addChild(idx, child)

    def removeChild(self, idx, child):
        self.parent.removeChild(idx, child)
