
class VRunnable:
    def run(self):
        return None


class VTask:
    def __init__(self):
        self.id = None
        # try | crit
        self.level = 'crit'
        # init | ready | running | finished | failed
        self.status = 'init'
        self.payload = None
        self.result = None

    def init(self):
        self.status = 'ready'

    def run(self, task_manager, app):
        # self.result = self.payload.run(self)
        self.status = 'finished'

class VTaskManager:
    def __init__(self, osa):
        self.queue = []
        self.log = osa.log
        self.osa = osa
        self.next_pointer = 0

    def run_tasks(self, app):
        self.log.d(f"Execution {len(self.queue)} tasks staring...")

        while self.next_pointer < len(self.queue):
            task = self.queue[self.next_pointer]
            if task.status == 'init' and task.level == 'crit':
                self.log.f("Critical task not ready")

            if task.status == 'finished':
                self.log.d(f'Skipping finished task {task.id}')
                self.next_pointer = self.next_pointer + 1

            if task.status == 'ready':
                try:
                    self.next_pointer = self.next_pointer + 1
                    task.run(self, app)
                except:
                    task.status = 'failed'
                    if task.level == 'crit':
                        self.log.f(f'Task {task.id} with level {task.level} failed!')
                    else:
                        self.log.e(f'Task {task.id} with level {task.level} failed!')

        self.log.d(f"Execution {len(self.queue)} tasks finished!")
