import os
import datetime as dt

class ItemDescriptor():
    def __init__(self, name=None, id=None, type=None, value=None, unit=None, label=None, definition=None, block_id=None, entity_type=None, shape=None, metadata=None, data_type=None, source_id=None, created_at=None, updated_at=None) -> None:
        super().__init__()
        self.name = name
        self.type = type
        self.id = id
        self.block_id= block_id
        self.definition = definition
        self.value = value
        self.unit = unit
        self.label = label
        self.entity_type = entity_type
        self.data_type = data_type
        self.shape = shape
        self.metadata_id = metadata
        self.source_id = source_id
        self.created_at = created_at
        self.updated_at = updated_at
        self.best_xdim = None
        self.suggested_plotter = None


    def to_html(self):
        descr = "<html><h4>%s: %s</h4>" % (self.type, self.name)
        descr += "<ol style='list-style-type:none'>"
        descr += "<li><small><b>id:</b> %s</small></li>" % (self.id)
        descr += "<li><small><b>entity type:</b> %s</small></li>" % (self.entity_type.value)
        descr += "<li><small><b>data type:</b> %s</small></li>" % (str(self.data_type))
        descr += "<li><small><b>data shape:</b> %s</small></li>" % (str(self.shape))
        descr += "<hr>"
        descr += "<p><small><b>definition:</b> %s</small></p>" % (self.definition)
        descr += "<hr>"
        descr += "<li><small><b>metadata id:</b> %s</small></li>" % (self.metadata_id)
        descr += "<li><small><b>source id:</b> %s</small></li>" % (self.source_id)
        descr += "<hr>"
        descr += "<li><small><b>created at:</b> %s</small></li>" % (str(dt.datetime.fromtimestamp(self.created_at)) if self.created_at else "")
        descr += "<li><small><b>updated at:</b> %s</small></li>" % (str(dt.datetime.fromtimestamp(self.updated_at)) if self.updated_at else "")
        descr += "</ol>"
        
        descr += "</html>"
        return descr
    
    def __str__(self) -> str:
        return "Entity: %s, id: %s, type: %s " % (self.name, self.id, self.entity_type)


class FileDescriptor():
    def __init__(self, filename, format, version, created_at, updated_at, size) -> None:
        super().__init__()
        self.name = filename
        self.size = None
        self.format = format
        self.version = version
        self.created_at = created_at
        self.updated_at = updated_at
        self.size = size
        self.block_count = 0
        self.data_array_count = 0
        self.tag_count = 0
        self.group_count = 0
        self.data_frame_count = 0
    
    def toHtml(self):
        def namAndPath(filename):
            parts = filename.split(os.sep)
            name = parts[-1]
            path = ""
            if len(parts) > 1:
                path = os.sep.join(parts[:-1])
            return name, path

        name, path = namAndPath(self.name)
        descr = "<html><h4>%s</h4>" % name
        descr += "<ol style='list-style-type:none'>"
        descr += "<li><small><b>location:</b> %s</small></li>" % (path if len(path) > 1 else ".")
        descr += "<li><small><b>format:</b> %s</small></li>" % (self.format)
        descr += "<li><small><b>nix format version:</b> %s</small></li>" % (str(self.version))
        descr += "<li><small><b>file size:</b> %.2f MB</small></li>" % (self.size)
        descr += "<hr>"
        descr += "<li>File contents</li>"
        descr += "<li><small><b>blocks:</b> %i</small></li>" % self.block_count
        descr += "<li><small><b>groups:</b> %i</small></li>" % self.group_count
        descr += "<li><small><b>data arrays:</b> %i</small></li>" % self.data_array_count
        descr += "<li><small><b>data frames:</b> %i</small></li>" % self.data_frame_count
        descr += "<li><small><b>tags:</b> %i</small></li>" % self.tag_count
        descr += "<hr>"
        descr += "<li><small><b>created at:</b> %s</small></li>" % (str(dt.datetime.fromtimestamp(self.created_at)))
        descr += "<li><small><b>updated at:</b> %s</small></li>" % (str(dt.datetime.fromtimestamp(self.updated_at)))
        descr += "</ol>"
        descr += "</html>"
        return descr

