from plantuml import PlantUML
from os.path import abspath


class Request:
    def __init__(self, server_url: str):
        self._server_url = server_url

        self._init_server()

    def _init_server(self):
        self.server = PlantUML(url=self._server_url)
        self.clear()

    def _startUML(self):
        self.reqArr.append('@startuml')

    def _endUML(self):
        self.reqArr.append('@enduml')

    def clear(self):
        self.timestamps = {}
        self.reqArr = []
        self.variables = []
        self.lineStyles = {}
        self.stringUML = ''
        self._startUML()
    
    def addAnalog(self, name, string):
        self.variables.append(f'analog "{string}" as {name}')

    def addBinary(self, name, string, style = ''):
        if style: name = name + '<<' + style + '>>'
        self.variables.append(f'binary "{string}" as {name}')

    def addClock(self, name, string):
        self.variables.append(f'clock "{string}" as {name}')

    def addConcise(self, name, string):
        self.variables.append(f'concise "{string}" as {name}')

    def addRobust(self, name, string):
        self.variables.append(f'robust "{string}" as {name}')

    def appendStr(self, string = ''):
        self.variables.append(string)

    def addLineStyle(self, name, color = 'green', thicknes = 1):
        self.lineStyles[name] = [f'LineColor {color}', f'LineThickness {thicknes}']

    def generateSVG(self, name):
        self._compileUML(name)
        filename = abspath(f'{name}.txt')
        self.server.processes_file(filename, outfile=f'{name}.svg')
        #result = self.server.processes(self.stringUML)
        #return result

    def setTimestamps(self, name, input):
        for time, state in input:
            try:
                self.timestamps[f'@{time}'].append(f'{name} is {state}')
            except KeyError:
                self.timestamps[f'@{time}'] = [f'{name} is {state}']

    def _addTimestamp(self, timecode, vars):
        self.reqArr.append(timecode)
        for var in vars:
                self.reqArr.append(var)

    def _addHeader(self):
        self.reqArr.append('<style>')
        if self.lineStyles:
            self.reqArr.append('timingDiagram {')
            for name, style in self.lineStyles.items():
                self.reqArr.append(f' .{name} ' + '{')
                for param in style: self.reqArr.append('   ' + str(param))
                self.reqArr.append(' }')
            self.reqArr.append('}')

        self.reqArr.append('</style>')

    def _addVariables(self):
        for var in self.variables: self.reqArr.append(str(var))

    def _compileUML(self, name):
        self._addHeader()
        self._addVariables()
            
        if self.timestamps:
            for key, item in self.timestamps.items():
                self._addTimestamp(key, item)
                
        self._endUML()
        self.stringUML = [line + '\n' for line in self.reqArr]
        with open(f'{name}.txt', 'w', encoding='utf-8') as file:
            file.writelines(self.stringUML)



