#
# Copyright (c) Daniel Sheffield 2023
# All rights reserved
#
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
from queue import Queue, Empty
from time import time
from threading import Lock
from typing import Callable

class CachedLoadingPage():
    
    value: str

    def __init__(self, initial_value: str, provider: Callable[[Queue], None]):
        self._created = time()
        self._queue = Queue()
        self._loaded = False
        self.value = initial_value
        self._lock = Lock()
        self.provider = provider

    @property
    def age(self) -> float:
        return time() - self._created

    @property
    def queue(self) -> Queue:
        return self._queue

    @property
    def loaded(self) -> bool:
        return self._loaded

    def _set_loaded(self, value: bool) -> bool:
        self._loaded = value
        return self._loaded
    
    @property
    def stale(self) -> bool:
        return self.age > 10*60
    
    def _start(self) -> None:
        if not self.provider:
            return
        
        self.provider(self.queue)
        self.provider = None

    def update(self) -> str:
        if not self._lock.acquire(blocking=True, timeout=0.5):
            return self.value
        try:
            self._start()
            item = self._queue.get(block=True, timeout=0.5)
            if item is None:
                self._queue.task_done()
                self._set_loaded(True)
            else:
                self.value = item
                self.queue.task_done()
        except Empty:
            pass
        self._lock.release()
        return self.value