"""
Copyright (C) 2026 xanderhopp; xanderhopp@gmail.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""

import xml.etree.ElementTree as ElementTree
import uuid as uuidly


# ident = createId.createUuid()
def create_ident():
    ident = '{' + uuidly.uuid1().urn[9:] + '}'
    return ident


def diagram_template(xmltree):
    try:
        template = []
        tree = xmltree.getroot()
        newdiagrams = tree.find("newdiagrams")
        inset = newdiagrams.find("inset")
        template.append(inset.get("author"))
        template.append(inset.get("titleblocktemplate"))
        template.append(inset.get("titleblocktemplateCollection"))
        template.append(inset.get("displayAt"))
        return template
    except(IndentationError, TypeError) as e:
        print("Something went wrong!", e.args[0])


def new_diagram(xmltree):
    try:
        new_diagram_data = []
        tree = xmltree.getroot()
        newdiagrams = tree.find("newdiagrams")
        for propertys in newdiagrams.iter("property"):
            new_diagram_data.append([propertys.get("name"), propertys.text])
        return new_diagram_data
    except(IndentationError, TypeError) as e:
        print("Something went wrong!", e.args[0])


# ------------------------------------------------------------------------------
# die Seite der Tabelle wird erstellt  ...  the page of a table is to be createt
# ------------------------------------------------------------------------------


def create_category():
    category = ElementTree.Element("category", {"name":"Klemmenplaene"})
    names = ElementTree.SubElement(category, "names")
    name = ElementTree.SubElement(names, "name", {"lang":"de"})
    name.text = "Klemmenpläne"
    name = ElementTree.SubElement(names, "name", {"lang":"en"})
    name.text = "terminal diagrams"
    name = ElementTree.SubElement(names, "name", {"lang":"fr"})
    name.text = "Schémas de terminaux"
    return category


def create_page(terminal_row_obj, pagenumberP, originPages, template, diagramData, dateP, part, identstr):
    qetElementName = "Klemmleiste " + terminal_row_obj.terminal_name + str(part) + ".elmt"
    qetElemtntTitle = "Klemmleiste " + terminal_row_obj.terminal_name + " / " + str(part)
    diagram = ElementTree.Element("diagram", {"order": str(pagenumberP), "displayAt":"bottom",
                                 "height":"1070", "colsize":"200", "displaycols":"true",
                                 "author": template[0], "plant":terminal_row_obj.plant, "rows":"6",
                                 "displayrows":"true",
                                 "title": qetElemtntTitle,
                                 "folio":"K" + str(pagenumberP - originPages), "rowsize":"175", "cols":"8",
                                 "freezeNewElement":"true", "filename":"", "version":"0.90",
                                 "freezeNewConductor":"false", "auto_page_num":"",
                                 "titleblocktemplateCollection":"embedded",
                                 "titleblocktemplate":template[1], "date": dateP,
                                 "indexrev":"", "locmach":terminal_row_obj.locmach, })
    properties1 =ElementTree.SubElement(diagram, "properties")
    for i in diagramData[0::]:
        property1 = ElementTree.SubElement(properties1, "property", {"name":i[0], "show":"1"})
        property1.text = "V"

    defaultconductor = ElementTree.SubElement(diagram, "defaultcoductor", {"vertical-alignment":"AlignRight",
                                                    "onetextperfolio":"0",
                                                    "color2":"#000000", "horizontal-alignment":"AlignBottom",
                                                    "formula":"", "cable":"", "conductor_section":"",
                                                    "dash-size":"2", "horizrotatetext":"0",
                                                    "bicolor":"false", "conductor_color":"",
                                                    "vertirotatetext":"270", "displaytext":"1",
                                                    "function":"", "type":"multi", "bus":"",
                                                    "condsize":"1", "numsize":"7", "num":"",
                                                    "tension_protocol":"", "text_color":"#000000"})
    elements1 = ElementTree.SubElement(diagram, "elements")
    element1 = ElementTree.SubElement(elements1, "element", {"y":"30", "z":"10", "uuid": identstr,
                                                            "orientation":"0", "frezelabel":"true",
                                                        "x":"70", "prefic":"", "type":"embed://import/Klemmenplaene/" + qetElementName})
    terminals = ElementTree.SubElement(element1, "terminals")
    inputs = ElementTree.SubElement(element1, "inputs")
    elementInformations1 = ElementTree.SubElement(element1, "elementInformations")
    elementInformation1 = ElementTree.SubElement(elementInformations1, "elementInfomation")
    dynamic_texts = ElementTree.SubElement(element1, "dynamic_texts")
    texts_groups = ElementTree.SubElement(element1, "texts_groups")
    return diagram



# ------------------------------------------------------------------------------
# der Rahmen der Tabelle wird erstellt  ...  the frame of a table is to be createt
# ------------------------------------------------------------------------------


def create_table(terminal_row_obj, part, parts, identstr):
    qet_element_name = "Klemmleiste " + terminal_row_obj.terminal_name + str(part) + ".elmt"
    qet_elemtnt_title = "Klemmleiste " + terminal_row_obj.terminal_name + " / " + str(part)

    element = ElementTree.Element("element", {"name": qet_element_name})
    definition = ElementTree.SubElement(element, "definition", {"link_type": "simple",
                                                                "hotspot_y": "10", "height": "1060",
                                                                "version": "0.90", "hotspot_x": "2",
                                                                "width": "1560", "type": "element"})
    uuid = ElementTree.SubElement(definition, "uuid", {"uuid": identstr})
    names = ElementTree.SubElement(definition, "names")
    name = ElementTree.SubElement(names, "name", {"lang": "de"})
    name.text = qet_elemtnt_title
    element_informations = ElementTree.SubElement(definition, "elementInformations")
    informations = ElementTree.SubElement(definition, "informations")
    informations.text = ""
    description = ElementTree.SubElement(definition, "description")

    # rectangles: 0 = pageframe, 1 = cablelistframe, 2 = chosenterminalframe, 3 = nameterminalstripframe
    # [y-pos, width, x-pos, height]
    rectangles = (["-5", "1555", "0", "1050"],
                  ["-5", "600", "0", "180"],
                  ["-5", "600", "955", "180"],
                  ["-5", "355", "600", "60"])
    for s in rectangles:
        rect = ElementTree.SubElement(description, "rect",
                                      {"y": s[0], "width": s[1], "antialias": "false",
                                       "ry": "0", "x": s[2], "rx": "0",
                                       "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                       "height": s[3]})
    # lines
    lines = (["15", "0", "600", "15", "thin"],  # Unter Überschrift Kabelliste
             ["35", "0", "600", "35", "thin"],  # Zeilen Kabelliste
             ["55", "0", "600", "55", "thin"],
             ["75", "0", "600", "75", "thin"],
             ["95", "0", "600", "95", "thin"],
             ["115", "0", "600", "115", "thin"],
             ["135", "0", "600", "135", "thin"],
             ["155", "0", "600", "155", "thin"],
             ["242", "0", "1555", "242", "normal"],  # Unter Überschrift Klemmentabelle 2 Linien
             ["245", "0", "1555", "245", "normal"],
             ["215", "80", "80", "245", "normal"],  # senkrechte Trennlinien / nach Planverweis
             ["215", "290", "290", "245", "normal"], # nach Zuordnungstabelle
             ["215", "580", "580", "245", "normal"], # nach Ziehlbezeichnung
             ["215", "980", "980", "245", "normal"], # vor Ziehlbezeichnung
             ["215", "1270", "1270", "245", "normal"], # vor Zuordnungstabelle
             ["215", "1480", "1480", "245", "normal"], # vor Planverweis
             ["235", "110", "110", "245", "thin"],  # zwischenlinien Kabelzuordnung
             ["235", "140", "140", "245", "thin"],
             ["235", "170", "170", "245", "thin"],
             ["235", "200", "200", "245", "thin"],
             ["235", "230", "230", "245", "thin"],
             ["235", "260", "260", "245", "thin"],
             ["242", "600", "600", "245", "thin"],
             ["235", "690", "690", "245", "thin"],
             # ["235", "865", "865", "245", "thin"], kein Planverweis für die Klemme
             ["242", "960", "960", "245", "thin"],
             ["235", "1300", "1300", "245", "thin"],
             ["235", "1330", "1330", "245", "thin"],
             ["235", "1360", "1360", "245", "thin"],
             ["235", "1390", "1390", "245", "thin"],
             ["235", "1420", "1420", "245", "thin"],
             ["235", "1450", "1450", "245", "thin"],
             ["220", "810", "810", "245", "hight"],  # dicke linien an Klemmennummern
             ["220", "870", "870", "245", "hight"],
             ["15", "955", "1555", "15", "normal"])  # unter Überschrift Rahmen verwendete Klemme
    for s in lines:
        line = ElementTree.SubElement(description, "line",
                                      {"y1": s[0],
                                       "style": f"line-style:normal;line-weight:{s[4]};filling:none;color:black",
                                       "Length1": "1.5", "x1": s[1], "end1": "none", "antialias": "false",
                                       "x2": s[2], "length2": "1.5", "y2": s[3], "end2": "none"})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,28,-1,5,75,0,0,0,0,0",
                                   "rotation": "0", "y": "40", "color": "#000000",
                                   "x": "770", "text": terminal_row_obj.terminal_name})  # Rahmen 4 / Klemmleistenbezeichnung
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,75,0,0,0,0,0",
                                   "rotation": "0", "y": "80", "color": "#000000",
                                   "x": "615",
                                   "text": "Blatt " + str(part) + " von " + str(parts)})  # Blatt-NR / Anzahl Blätter

    texts = (["9", "75", "15", "10", "Verwendete Kabel oder Einzelader:"],  # Texte im Rahmen Kabelliste
             ["9", "50", "20", "30", "Bezeichnung"],
             ["9", "50", "120", "30", "Ziehl"],
             ["9", "50", "190", "30", "Typ"],
             ["9", "50", "340", "30", "Querschnitt"],
             ["9", "50", "415", "30", "Aderzahl"],
             ["9", "50", "475", "30", "Spannung/Potential"],
             ["9", "75", "5", "50", "1"],
             ["9", "75", "5", "70", "2"],
             ["9", "75", "5", "90", "3"],
             ["9", "75", "5", "110", "4"],
             ["9", "75", "5", "130", "5"],
             ["9", "75", "5", "150", "6"],
             ["9", "75", "5", "170", "7"],
             ["9", "75", "615", "25", "Klemmleiste:"],  # Überschrift
             ["9", "75", "970", "10",
              "Verwendete Klemme:                                                         Bemerkungen:"],
             ["9", "50", "10", "220", "Plan-"],  # Spaltenüberschriften rechts
             ["9", "50", "10", "235", "verweis"],
             ["9", "75", "120", "215", "Kabel"],
             ["9", "50", "170", "215", "Ader-Nr.  /  Farbe"],
             ["9", "75", "92", "235", "1"],
             ["9", "75", "122", "235", "2"],
             ["9", "75", "152", "235", "3"],
             ["9", "75", "182", "235", "4"],
             ["9", "75", "212", "235", "5"],
             ["9", "75", "242", "235", "6"],
             ["9", "75", "272", "235", "7"],
             ["9", "75", "415", "215", "Ziehl"],
             ["9", "50", "295", "235", "= Anlage   /"],
             ["9", "50", "370", "235", "+ Ort   /"],
             ["9", "50", "417", "235", "- Betriebsmittel /"],
             ["9", "50", "515", "235", ": Anschluss"],
             ["9", "50", "700", "235", "Funktion"],
             ["9", "50", "592", "235", "Brücke / Buchse"],
             ["9", "50", "510", "215", "extern"],
             ["11", "75", "810", "200", "Klemme"],
             ["9", "50", "1012", "215", "intern"],
             # ["9", "50", "813", "220", "Plan-"],  # aus Platzgründen verworfen
             # ["9", "50", "813", "235", "verweis"],
             ["9", "50", "880", "235", "Brücke / Buchse"],
             ["9", "50", "1490", "220", "Plan-"],
             ["9", "50", "1490", "235", "verweis"],
             ["9", "75", "1097", "215", "Ziehl"],
             ["9", "50", "985", "235", "= Anlage   /"],
             ["9", "50", "1060", "235", "+ Ort   /"],
             ["9", "50", "1107", "235", "- Betriebsmittel /"],
             ["9", "50", "1205", "235", ": Anschluss"],
             ["9", "75", "1310", "215", "Kabel"],
             ["9", "50", "1360", "215", "Ader-Nr.  /  Farbe"],
             ["9", "75", "1282", "235", "1"],
             ["9", "75", "1312", "235", "2"],
             ["9", "75", "1342", "235", "3"],
             ["9", "75", "1372", "235", "4"],
             ["9", "75", "1402", "235", "5"],
             ["9", "75", "1432", "235", "6"],
             ["9", "75", "1462", "235", "7"])
    for s in texts:
        text = ElementTree.SubElement(description, "text",
                                      {"font": f"Sans Serif,{s[0]},-1,5,{s[1]},0,0,0,0,0",
                                       "rotation": "0", "y": s[3], "color": "#000000",
                                       "x": s[2], "text": s[4]})
    return element


# --------------------------------------------------------------------------------
# Erstellen der Tabellenzeile ... create the tablecol
# --------------------------------------------------------------------------------


def create_row_outer(connection_obj, conductor_obj, element_obj, connected_cables,
                     terminal_counter, rows_per_terminal, sf, give_feedback):
    try:
        x = 239 + (terminal_counter * 20) + (rows_per_terminal[1].index(connection_obj.eet_name) * 20)
    except:
        give_feedback("Eine Klemme mit verkehrtem Extern-Intern wurde verwendet!" + connection_obj.terminal_name + ":" + connection_obj.ei_terminal_nr)
    c = 0
    print (connected_cables)
    # check if cables are in the cable list, if, than set the place, where the colorlabel has to be in the table
    if conductor_obj.cable != "":
        c = 83
        for i in connected_cables:
            if conductor_obj.cable in i[0]:
                break
            else:
                c += 30
    else:
        c = 83
        give_feedback("Achtung! Keine Kabel hinterlegt! \nAdern werden in spalte 1 abgelegt.")

    if "-X" in element_obj.label:
        element_obj.connectionpoint = str(element_obj.x_nr) + " " + str(element_obj.connectionpoint)
        element_obj.plant = element_obj.x_plant
        element_obj.location = element_obj.x_locmach

    description = ElementTree.Element("description")
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "8", "text": sf.reference(element_obj)} ) # Seitenverweis
    if conductor_obj.conductor_color != "":
        text = ElementTree.SubElement(description, "text",
                                      {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                       "rotation": "0", "y": str(x), "color": "#000000",
                                       "x": str(c), "text": conductor_obj.conductor_color})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "295", "text": ("="+element_obj.plant)})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "370", "text": ("+"+element_obj.location)})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "440", "text": element_obj.label})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "530", "text": (":"+element_obj.connectionpoint)})
    return description


def create_row_inner(connection_obj, conductor_obj, element_obj, connected_cables, terminal_counter,
                     rows_per_terminal, sf, give_feedback):

    x = 239 + (terminal_counter * 20) + (rows_per_terminal[2].index(connection_obj.eet_name) * 20)
    c = 0
    if conductor_obj.cable != "":
        c = 1273
        for i in connected_cables:
            if conductor_obj.cable not in i[0]:
                c += 30
            else:
                break
    else:
        c = 1273
        give_feedback("Achtung! Keine Kabel hinterlegt! \nAdern werden in spalte 1 abgelegt.")

    if "-X" in element_obj.label:
        element_obj.connectionpoint = str(element_obj.x_nr) + " " + str(element_obj.connectionpoint)
        element_obj.plant = element_obj.x_plant
        element_obj.location = element_obj.x_locmach

    if element_obj.label == "-X%":
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    description = ElementTree.Element("description")
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "1490", "text": sf.reference(element_obj)})  # Seitenverweis
    if conductor_obj.conductor_color != "":
        text = ElementTree.SubElement(description, "text",
                                      {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                       "rotation": "0", "y": str(x), "color": "#000000",
                                       "x": str(c), "text": conductor_obj.conductor_color})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "985", "text": ("="+element_obj.plant)})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "1060", "text": ("+"+element_obj.location)})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "1130", "text": element_obj.label})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "1220", "text": (":"+element_obj.connectionpoint)})
    return description


def create_terminal(terminal_obj, linepos, rows, list_of_connectionpoints ):
    x = 240 + (linepos * 20) + ((rows-1) * 10)
    y = 239 + linepos * 20

    description = ElementTree.Element("description")
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,11,-1,5,75,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "820", "text": terminal_obj.terminal_nr})
    # auf den Verweis wird aus Platzgründen verzichtet
    # text = ElementTree.SubElement(description, "text",
    #                               {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
    #                                "rotation": "0", "y": str(x), "color": "#000000",
    #                                "x": "812", "text": sf.reference(terminal_obj)})
    text = ElementTree.SubElement(description, "text",
                                  {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                   "rotation": "0", "y": str(x), "color": "#000000",
                                   "x": "695", "text": terminal_obj.function})
    zo = 0
    zi = 0
    for lc in list_of_connectionpoints:
        if lc[1] == "Outer":
            text = ElementTree.SubElement(description, "text",
                                          {"font": "Sans Serif,9,-1,5,75,0,0,0,0,0",
                                           "rotation": "0", "y": str(y + zo * 20), "color": "#000000",
                                           "x": "585", "text": lc[0]})
            zo += 1
        if lc[1] == "Inner":
            text = ElementTree.SubElement(description, "text",
                                          {"font": "Sans Serif,9,-1,5,75,0,0,0,0,0",
                                           "rotation": "0", "y": str(y + zi * 20), "color": "#000000",
                                           "x": "965", "text": lc[0]})
            zi += 1

    created_terminal = terminal_obj.terminal_name, terminal_obj.terminal_nr, x
    return description, created_terminal


def create_terminal_bridge(bridges, number_of_terminals, terminal_counter, give_feedback):
    description = ElementTree.Element("description")
    xb = None
    for b in bridges:
        # select, where the bridge has to be
        print("Brücke b: ", b)
        if b[3] == "e":
            xb = 585+30*int(b[2])
        if b[3] == "i":
            xb = 945-30*int(b[2])

        if xb == None:
            give_feedback("!Fehler!\nAn einer Brücke bei Klemme " + str(b[4]) + " ist die \"Lagebezeichnung\" falsch. \nSie muss (1-3)e oder (1-3)i heißen.")

        # a bridge could be drawed from lower terminal to upper terminal or the other way
        if int(b[0]) < int(b[1]):
            y_start = int(b[0]) - 9
            y_end = int(b[1]) - 1
            y2_start = y_start + 11
            y2_end = y_end - 11
        else:
            y_start = int(b[1]) - 9
            y_end = int(b[0]) - 1
            y2_start = y_start + 11
            y2_end = y_end - 11

        # if the bridge is going to rise over the end of the page, there must be no circle at the end of the bridge
        # and te drawing should end at the last terminal
        end_start = "circle"
        end_end = "circle"
        if y_start == -9:
            end_start = "none"
            y_start = 247
            y2_start = 247
        if y_end == 9998:
            end_end = "none"
            y_end = 223 + 20 * terminal_counter
            y2_end = 223 + 20 * terminal_counter




        line = ElementTree.SubElement(description, "line",
                                      {"x1": str(xb),
                                       "style": "line-style:normal;line-weight:eleve;filling:black;color:black",
                                       "length2": "4.0", "y1": str(y_start), "end1": end_start, "antialias": "false",
                                       "y2": str(y_end), "length1": "4.0", "x2": str(xb),
                                       "end2": end_end})  # sehr dicker Strich mit gefüllten Kreis-Enden
        line = ElementTree.SubElement(description, "line",
                                      {"x1": str(xb),
                                       "style": "line-style:normal;line-weight:heigh;filling:white;color:white",
                                       "length2": "4.0", "y1": str(y2_start), "end1": "none", "antialias": "false",
                                       "y2": str(y2_end), "length1": "4.0", "x2": str(xb),
                                       "end2": "none"})  # dicker weißer Strich
    return description


def create_lines(linepos, rows):
    x = 245 + (linepos * 20)
    if rows > 1:
        x = 245 + (linepos * 20)
    description = ElementTree.Element("description")
    while rows > 1:
        line = ElementTree.SubElement(description, "line",
                                      {"y1": str(x-12),
                                       "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                       "Length2": "1.5", "x1": "900", "end1": "none", "antialias": "false",
                                       "x2": "900", "length2": "1.5", "y2": str(x-8),
                                       "end2": "none"})  # Andeuten Fach für Steckbrücke
        line = ElementTree.SubElement(description, "line",
                                      {"y1": str(x-12),
                                       "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                       "Length2": "1.5", "x1": "930", "end1": "none", "antialias": "false",
                                       "x2": "930", "length2": "1.5", "y2": str(x-8),
                                       "end2": "none"})  # Andeuten Fach für Steckbrücke
        line = ElementTree.SubElement(description, "line",
                                      {"y1": str(x),
                                       "style": "line-style:dashed;line-weight:thin;filling:none;color:black",
                                       "Length2": "1.5", "x1": "0", "end1": "none", "antialias": "false",
                                       "x2": "600", "length2": "1.5", "y2": str(x),
                                       "end2": "none"})  # Trennstrich gestrichelt
        line = ElementTree.SubElement(description, "line",
                                      {"y1": str(x-12),
                                       "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                       "Length2": "1.5", "x1": "630", "end1": "none", "antialias": "false",
                                       "x2": "630", "length2": "1.5", "y2": str(x-8),
                                       "end2": "none"})  # Andeuten Fach für Steckbrücke
        line = ElementTree.SubElement(description, "line",
                                      {"y1": str(x-12),
                                       "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                       "Length2": "1.5", "x1": "660", "end1": "none", "antialias": "false",
                                       "x2": "660", "length2": "1.5", "y2": str(x-8),
                                       "end2": "none"})  # Andeuten Fach für Steckbrücke

        line = ElementTree.SubElement(description, "line",
                                      {"y1": str(x),
                                       "style": "line-style:dashed;line-weight:thin;filling:none;color:black",
                                       "Length2": "1.5", "x1": "960", "end1": "none", "antialias": "false",
                                       "x2": "1555", "length2": "1.5", "y2": str(x),
                                       "end2": "none"})  # Trennstrich gestrichelt

        lines = (["215", "80", "80", "1045", "normal"],  # senkrechte Trennlinien / nach Planverweis
                ["215", "290", "290", "1045", "normal"], # nach Zuordnungstabelle
                ["215", "580", "580", "1045", "normal"], # nach Ziehlbezeichnung
                ["215", "980", "980", "1045", "normal"], # vor Ziehlbezeichnung
                ["215", "1270", "1270", "1045", "normal"], # vor Zuordnungstabelle
                ["215", "1480", "1480", "1045", "normal"], # vor Planverweis
                ["235", "110", "110", "1045", "thin"],  # zwischenlinien Kabelzuordnung
                ["235", "140", "140", "1045", "thin"],
                ["235", "170", "170", "1045", "thin"],
                ["235", "200", "200", "1045", "thin"],
                ["235", "230", "230", "1045", "thin"],
                ["235", "260", "260", "1045", "thin"],
                ["242", "600", "600", "1045", "thin"],
                ["235", "690", "690", "1045", "thin"],
                # ["235", "865", "865", "1045", "thin"], kein Planverweis für die Klemme
                ["242", "960", "960", "1045", "thin"],
                ["235", "1300", "1300", "1045", "thin"],
                ["235", "1330", "1330", "1045", "thin"],
                ["235", "1360", "1360", "1045", "thin"],
                ["235", "1390", "1390", "1045", "thin"],
                ["235", "1420", "1420", "1045", "thin"],
                ["235", "1450", "1450", "1045", "thin"],
                ["220", "810", "810", "1044", "hight"],  # dicke linien an Klemmennummern
                ["220", "870", "870", "1044", "hight"])

        for s in lines:
            line = ElementTree.SubElement(description, "line",
        {"y1": str(x - 20),
         "style": f"line-style:normal;line-weight:{s[4]};filling:none;color:black",
         "Length1": "1.5", "x1": s[1], "end1": "none", "antialias": "false",
         "x2": s[2], "length2": "1.5", "y2": str(x), "end2": "none"})

        x += 20
        rows -= 1
    line = ElementTree.SubElement(description, "line",
                                  {"y1": str(x - 12),
                                   "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                   "Length2": "1.5", "x1": "900", "end1": "none", "antialias": "false",
                                   "x2": "900", "length2": "1.5", "y2": str(x - 8),
                                   "end2": "none"})  # Andeuten Fach für Steckbrücke
    line = ElementTree.SubElement(description, "line",
                                  {"y1": str(x - 12),
                                   "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                   "Length2": "1.5", "x1": "930", "end1": "none", "antialias": "false",
                                   "x2": "930", "length2": "1.5", "y2": str(x - 8),
                                   "end2": "none"})  # Andeuten Fach für Steckbrücke
    line = ElementTree.SubElement(description, "line",
                                  {"y1": str(x - 12),
                                   "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                   "Length2": "1.5", "x1": "630", "end1": "none", "antialias": "false",
                                   "x2": "630", "length2": "1.5", "y2": str(x-8),
                                   "end2": "none"})  # Andeuten Fach für Steckbrücke
    line = ElementTree.SubElement(description, "line",
                                  {"y1": str(x - 12),
                                   "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                   "Length2": "1.5", "x1": "660", "end1": "none", "antialias": "false",
                                   "x2": "660", "length2": "1.5", "y2": str(x-8),
                                   "end2": "none"})  # Andeuten Fach für Steckbrücke
    line = ElementTree.SubElement(description, "line",
                                  {"y1": str(x),
                                   "style": "line-style:normal;line-weight:normal;filling:none;color:black",
                                   "Length2": "1.5", "x1": "0", "end1": "none", "antialias": "false",
                                   "x2": "1555", "length2": "1.5", "y2": str(x), "end2": "none"})  # Trennstrich normal

    lines = (["215", "80", "80", "1045", "normal"],  # senkrechte Trennlinien / nach Planverweis
            ["215", "290", "290", "1045", "normal"], # nach Zuordnungstabelle
            ["215", "580", "580", "1045", "normal"], # nach Ziehlbezeichnung
            ["215", "980", "980", "1045", "normal"], # vor Ziehlbezeichnung
            ["215", "1270", "1270", "1045", "normal"], # vor Zuordnungstabelle
            ["215", "1480", "1480", "1045", "normal"], # vor Planverweis
            ["235", "110", "110", "1045", "thin"],  # zwischenlinien Kabelzuordnung
            ["235", "140", "140", "1045", "thin"],
            ["235", "170", "170", "1045", "thin"],
            ["235", "200", "200", "1045", "thin"],
            ["235", "230", "230", "1045", "thin"],
            ["235", "260", "260", "1045", "thin"],
            ["242", "600", "600", "1045", "thin"],
            ["235", "690", "690", "1045", "thin"],
            # ["235", "865", "865", "1045", "thin"], kein Planverweis für die Klemme
            ["242", "960", "960", "1045", "thin"],
            ["235", "1300", "1300", "1045", "thin"],
            ["235", "1330", "1330", "1045", "thin"],
            ["235", "1360", "1360", "1045", "thin"],
            ["235", "1390", "1390", "1045", "thin"],
            ["235", "1420", "1420", "1045", "thin"],
            ["235", "1450", "1450", "1045", "thin"],
            ["220", "810", "810", "1044", "hight"],  # dicke linien an Klemmennummern
            ["220", "870", "870", "1044", "hight"])

    for s in lines:
        line = ElementTree.SubElement(description, "line",
    {"y1": str(x - 20),
     "style": f"line-style:normal;line-weight:{s[4]};filling:none;color:black",
     "Length1": "1.5", "x1": s[1], "end1": "none", "antialias": "false",
     "x2": s[2], "length2": "1.5", "y2": str(x), "end2": "none"})

    return description



def create_cablelist_part(a):
    y = 30
    description = ElementTree.Element("description")
    for i in a:
        y += 20
        text = ElementTree.SubElement(description, "text",  # Bezeichnung
                                      {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                       "rotation": "0", "y": str(y), "color": "#000000",
                                       "x": "25", "text": str(i[0])})
        # text = ElementTree.SubElement(description, "text",  # Ziehl
        #                               {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
        #                                "rotation": "0", "y": str(y), "color": "#000000",
        #                                "x": "110", "text": str(i[3])})
        text = ElementTree.SubElement(description, "text",  # Querschnitt
                                      {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                       "rotation": "0", "y": str(y), "color": "#000000",
                                       "x": "350", "text": str(i[2])})
        text = ElementTree.SubElement(description, "text",  # Spannung/Potential
                                      {"font": "Sans Serif,9,-1,5,50,0,0,0,0,0",
                                       "rotation": "0", "y": str(y), "color": "#000000",
                                       "x": "485", "text": str(i[1])})
    return description


eTableaccessories = ("""
                    <category name="accesories">
                        <names>
                            <name lang="de">Zubehör</name>
                            <name lang="en">accesories</name>
                            <name lang="fr">accessoires</name>
                        </names>
                        <element name="test_socked_bl.elmt">
                            <definition hotspot_y="15" width="20" link_type="simple" version="0.100.0" height="20" hotspot_x="5" type="element">
                                <uuid uuid="{3d2aeebf-183e-496e-bb2b-d8f9d3d0ddd0}"/>
                                <names>
                                    <name lang="de">Steckbuchse blau</name>
                                    <name lang="en">Test socket blue</name>
                                    <name lang="fr">Prise de test bleue</name>
                                </names>
                                <elementInformations/>
                                <description>
                                    <arc width="16" y="-13" style="line-style:normal;line-weight:hight;filling:none;color:blue" height="16" start="183.938" antialias="true" x="-3" angle="355.875"/>
                                    <line y1="-12" x1="5" y2="2" style="line-style:normal;line-weight:eleve;filling:none;color:white" length2="1.5" end2="none" antialias="false" x2="5" end1="none" length1="1.5"/>
                                </description>
                            </definition>
                        </element>
                        <element name="test_socked_gn.elmt">
                            <definition hotspot_y="15" width="20" link_type="simple" version="0.100.0" height="20" hotspot_x="5" type="element">
                                <uuid uuid="{6532a278-8cf5-4a98-a0a1-e6502bcc000c}"/>
                                <names>
                                    <name lang="de">Steckbuchse grün</name>
                                    <name lang="en">Test socket green</name>
                                    <name lang="fr">Prise de test verte</name>
                                </names>
                                <elementInformations/>
                                <description>
                                    <arc width="16" y="-13" style="line-style:normal;line-weight:hight;filling:none;color:HTMLGreenLimeGreen" height="16" start="183.938" antialias="true" x="-3" angle="355.875"/>
                                    <line y1="-12" x1="5" y2="2" style="line-style:normal;line-weight:eleve;filling:none;color:white" length2="1.5" end2="none" antialias="false" x2="5" end1="none" length1="1.5"/>
                                </description>
                            </definition>
                        </element>
                        <element name="test_socked_vt.elmt">
                            <definition hotspot_y="15" width="20" link_type="simple" version="0.100.0" height="20" hotspot_x="5" type="element">
                                <uuid uuid="{5c892802-2018-487f-aa96-e5a25b9cb60c}"/>
                                <names>
                                    <name lang="de">Steckbuchse violet</name>
                                    <name lang="en">Test socket violet</name>
                                    <name lang="fr">Prise de test violette</name>
                                </names>
                                <elementInformations/>
                                <description>
                                    <arc width="16" y="-13" style="line-style:normal;line-weight:hight;filling:none;color:purple" height="16" start="183.938" antialias="true" x="-3" angle="355.875"/>
                                    <line y1="-12" x1="5" y2="2" style="line-style:normal;line-weight:eleve;filling:none;color:white" length2="1.5" end2="none" antialias="false" x2="5" end1="none" length1="1.5"/>
                                </description>
                            </definition>
                        </element>
                        <element name="test_socked_yo.elmt">
                            <definition hotspot_y="15" width="20" link_type="simple" version="0.100.0" height="20" hotspot_x="5" type="element">
                                <uuid uuid="{fcede7ff-0e01-480f-a484-5eff8e8b0beb}"/>
                                <names>
                                    <name lang="de">Steckbuchse gelb</name>
                                    <name lang="en">Test socket yellow</name>
                                    <name lang="fr">Prise de test jaune</name>
                                </names>
                                <elementInformations/>
                                <description>
                                    <arc width="16" y="-13" style="line-style:normal;line-weight:hight;filling:none;color:yellow" height="16" start="183.938" antialias="true" x="-3" angle="355.875"/>
                                    <line y1="-12" x1="5" y2="2" style="line-style:normal;line-weight:eleve;filling:none;color:white" length2="1.5" end2="none" antialias="false" x2="5" end1="none" length1="1.5"/>
                                </description>
                            </definition>
                        </element>
                        <element name="test_socket_bk.elmt">
                            <definition hotspot_y="15" width="20" link_type="simple" version="0.100.0" height="20" hotspot_x="5" type="element">
                                <uuid uuid="{6c2e58f6-d811-4a67-8ed7-c1e5d2487785}"/>
                                <names>
                                    <name lang="de">Steckbuchse schwarz</name>
                                    <name lang="en">Test socket black</name>
                                    <name lang="fr">Prise de test noire</name>
                                </names>
                                <elementInformations/>
                                <description>
                                    <arc width="16" y="-13" style="line-style:normal;line-weight:hight;filling:none;color:black" height="16" start="183.938" antialias="true" x="-3" angle="355.875"/>
                                    <line y1="-12" x1="5" y2="2" style="line-style:normal;line-weight:eleve;filling:none;color:white" length2="1.5" end2="none" antialias="false" x2="5" end1="none" length1="1.5"/>
                                </description>
                            </definition>
                        </element>
                        <element name="test_socket_gy.elmt">
                            <definition hotspot_y="15" width="20" link_type="simple" version="0.100.0" height="20" hotspot_x="5" type="element">
                                <uuid uuid="{9d3ea151-e6e3-4354-bb9c-53716e5d8a8b}"/>
                                <names>
                                    <name lang="de">Steckbuchse grau</name>
                                    <name lang="en">Test socket gray</name>
                                    <name lang="fr">Prise de test grise</name>
                                </names>
                                <elementInformations/>
                                <description>
                                    <arc width="16" y="-13" style="line-style:normal;line-weight:hight;filling:none;color:gray" height="16" start="183.938" antialias="true" x="-3" angle="355.875"/>
                                    <line y1="-12" x1="5" y2="2" style="line-style:normal;line-weight:eleve;filling:none;color:white" length2="1.5" end2="none" antialias="false" x2="5" end1="none" length1="1.5"/>
                                </description>
                            </definition>
                        </element>
                        <element name="test_socket_rd.elmt">
                            <definition hotspot_y="15" width="20" link_type="simple" version="0.100.0" height="20" hotspot_x="5" type="element">
                                <uuid uuid="{d6827121-2d8a-4edf-9e4a-dfb2b86f7fcd}"/>
                                <names>
                                    <name lang="de">Steckbuchse rot</name>
                                    <name lang="en">Test socket red</name>
                                    <name lang="fr">Prise de test rouge</name>
                                </names>
                                <elementInformations/>
                                <description>
                                    <arc width="16" y="-13" style="line-style:normal;line-weight:hight;filling:none;color:red" height="16" start="183.938" antialias="true" x="-3" angle="355.875"/>
                                    <line y1="-12" x1="5" y2="2" style="line-style:normal;line-weight:eleve;filling:none;color:white" length2="1.5" end2="none" antialias="false" x2="5" end1="none" length1="1.5"/>
                                </description>
                            </definition>
                        </element>
                        <element name="end cover.elmt">
                            <definition width="410" hotspot_y="0" version="0.90" link_type="simple" height="10" hotspot_x="205" type="element">
                                <uuid uuid="{d8d22eae-0626-4648-bb73-217d5b5b2ad1}"/>
                                <names>
                                    <name lang="de">End-Deckel / Trennplatte</name>
                                    <name lang="fr">Capuchon / Rondelle de déconnexion</name>
                                    <name lang="en">End cap / Disconnect washer</name>
                                </names>
                                <elementInformations/>
                                <informations/>
                                <description>
                                    <line y1="5" x1="-200" y2="5" length2="1.5" style="line-style:normal;line-weight:eleve;filling:black;color:black" end2="diamond" antialias="false" x2="200" end1="diamond" length1="1.5"/>
                                    <line y1="5" x1="-200" y2="5" length2="1.5" style="line-style:dashed;line-weight:hight;filling:none;color:white" end2="none" antialias="false" x2="200" end1="none" length1="1.5"/>
                                </description>
                            </definition>
                        </element>
                    </category>
                    """)