from core.file_manager import FileManager
from utils.utc_date import UtcDate

class FileChangeLog():
    '''
        Change Log of a file, with change timestamps and usernames
    '''
    
    def __init__(self, filename):
        self.filename = filename
        self.changelog = []
        self.read()
    
    def add_log(self, username, comment):
        idx = len(self.changelog)
        timestamp = UtcDate.utcnow()
        new_log = [idx, timestamp, username, comment]
        self.changelog.append(new_log)
    
    def read(self):
        log_path = self.filename + ".log"
        if FileManager.file_exists(log_path):
            self.changelog = FileManager.file_read(log_path)
    
    def save(self):
        log_path = self.filename + ".log"
        FileManager.file_write(log_path, self.changelog)
    
    def lastlog_index(self):
        return len(self.changelog) - 1
