import string
from array import array
from datetime import timedelta

from Display import Display
from Utils import find_storage_usage


class SingleDisplay(Display):
    SWITCH_INTERVAL: timedelta = timedelta(seconds=10)
    drives: array
    
    maxLabelWidth: int
    currentTime: float
    currentIndex: int
    
    def __init__(self, drives):
        self.drives = drives
        self.maxLabelWidth = self.getMaxLabelWidth()
        self.currentIndex = 0
        self.currentTime = 0
    
    def render(self, displayInterval: timedelta) -> string:
        self.currentTime = self.currentTime + displayInterval.total_seconds()
        if self.currentTime < self.SWITCH_INTERVAL.total_seconds():
            return self.renderCurrentDrive()
        
        self.currentTime = 0
        self.currentIndex = (self.currentIndex + 1) % len(self.drives)
        
        return self.renderCurrentDrive()
        
    def renderCurrentDrive(self):
        currentDrive = self.drives[self.currentIndex]
        label = currentDrive['label']
        path = currentDrive['path']
        
        usage = find_storage_usage(path)
        
        formatString = "{:>"+ self.maxLabelWidth.__str__() +"} {}"
        return formatString.format(label, usage)

    def getMaxLabelWidth(self):
        maxLabelWidth = 0
        for i, drive in enumerate(self.drives):
            maxLabelWidth = max(maxLabelWidth, len(drive['label']))
        return maxLabelWidth
