
from ezdxf.entities import Line
import math
from pydantic import BaseModel, Field, field_validator
from typing import Optional
import plant2dxf 
class Omniflo(BaseModel):
    teileid:str
    x:float
    y:float

    sivasnummer:str
    laenge: Optional [float]
    drehung: float
    hoehe : Optional[float]
    h0: Optional[float] = Field(default = 0.0,description="Höhe unten im CSV")
    h1: Optional[float] = Field(default = 0.0, description="Höhe Oben im CSV")

    @classmethod
    def from_merkmale(cls, teileid,x,y, merkmale):
        sivasnummer = merkmale.get("SivasNummer")
        try:
            laenge = float(merkmale.get("Länge in Meter", "0").replace(",", ".")) * 1000  # Meter → mm
        except Exception:
            laenge = 0
        try:
            winkel = float(merkmale.get("Drehung"))
        except Exception:
            winkel = 0.0
        try:
            hoehe = float(merkmale.get("Höhe"))
        except Exception:
            hoehe = 0.0
        try:
            h0 = float(merkmale.get("Höhe unten")) 
        except Exception:
            h0 = 0.0
        try:
            h1 = float(merkmale.get("Höhe oben"))
        except Exception:
            h1 = 0.0
        return cls(
            teileid= teileid,
            x=x,
            y=y,
            sivasnummer = sivasnummer,
            laenge = laenge,
            drehung = winkel,
            hoehe = hoehe,
            h0 = h0,
            h1 = h1
        )
    def Omniflo_geraden_erstellung(msp, x, y, doc, tefsivas, omniflo_objekt):
        winkel_rad = math.radians(omniflo_objekt.drehung)
        halbe_laenge = omniflo_objekt.laenge / 2
            # Man muss bei sin -1 machen wegen des links koordinaten system
        dx = halbe_laenge * math.sin(winkel_rad * -1)
        dy = halbe_laenge * math.cos(winkel_rad)
        start = (x + dx, y + dy, omniflo_objekt.h1 )
        ende  = (x - dx, y - dy, omniflo_objekt.h0)
        if "A-2" not in doc.layers:
            doc.layers.add(name="A-2", color=2)
        if "F-1" not in doc.layers:
            doc.layers.add(name="F-1", color =1)
        linie=msp.add_line(start, ende)
        if omniflo_objekt.sivasnummer == tefsivas:
            linie.dxf.layer = "F-1"
        else:
            linie.dxf.layer = "A-2"
    def omniflo_foerdererstellung(msp, x, y, doc, lib_doc, omniflo_objekt, rotation):
        plant2dxf.import_block("bogen1",lib_doc,doc)
        plant2dxf.import_block("bogen2",lib_doc,doc)
            
        laenge = omniflo_objekt.laenge
        h0 = omniflo_objekt.h0
        h1 = omniflo_objekt.h1
        h_zwischen = (h0 + h1)/2
        blockname = (f"OF_Förderer_{laenge}_{h_zwischen}")
        winkel_rad = math.radians(rotation)
        halbe_laenge = laenge / 2
            # Man muss bei sin -1 machen wegen des links koordinaten system
        dx = halbe_laenge * math.sin(rotation * -1)
        dy = halbe_laenge * math.cos(rotation)
        start = (x + dx, y + dy, h1 )
        ende  = (x - dx, y - dy, h0)
        if blockname not in doc.blocks:
            block = doc.blocks.new(blockname, base_point=(0,0,0))
            block.add_blockref("bogen1",start)
            block.add_blockref("bogen2",ende)
            line = Line.new(dxfattribs={"start": start,"end":ende})
            copy = line.copy()
            copy.translate(-x,-y,-h_zwischen)
            block.add_entity(copy)
        msp.add_blockref(blockname,(x,y,h_zwischen),dxfattribs={"rotation": rotation})
    