from flask import render_template

TEMPLATE = 'components/modul_tvum.html'

def get_formular_html():
    return render_template(TEMPLATE)

def berechne(form_data):
    pos = []
    inst_snr = _int(form_data.get('inst_snr', 0))
    gf_innen = _int(form_data.get('gf_innen_bef', 0))
    tvum_spl = _int(form_data.get('tvum_anz_spl', 0))
    tvum_mess = _int(form_data.get('tvum_anz_mess', 0))
    ftth_spl = _int(form_data.get('ftth_anz_spl', 0))
    nvt_spl = _int(form_data.get('nvt_anz_spl', 0))
    ausbl = _float(form_data.get('ausbl_m', 0))
    einbl = _float(form_data.get('einbl_m', 0))

    if form_data.get('tvum_ap_mont') == 'an':
        pos.append(dict(pos_nr='10037700', kurztext='TVUM-AP montieren Wand', menge=1, einheit='ST'))
    if inst_snr > 0:
        pos.append(dict(pos_nr='10037701', kurztext='Kanäle/SNR befestigen', menge=inst_snr, einheit='ST'))
    if gf_innen > 0:
        pos.append(dict(pos_nr='10037702', kurztext='GF-Innenkabel befestigen/einziehen', menge=gf_innen, einheit='M'))
    if tvum_spl > 0:
        pos.append(dict(pos_nr='10037703', kurztext=f'Spleiße TVUM-AP ({tvum_spl} Stk)', menge=tvum_spl, einheit='ST'))
    if tvum_mess > 0:
        pos.append(dict(pos_nr='10037704', kurztext=f'Messungen ({tvum_mess} Stk)', menge=tvum_mess, einheit='ST'))
    if ftth_spl > 0:
        pos.append(dict(pos_nr='10037705', kurztext=f'FTTH Spleiße ({ftth_spl} Stk)', menge=ftth_spl, einheit='ST'))
    if nvt_spl > 0:
        pos.append(dict(pos_nr='10037706', kurztext=f'NVT Spleiße ({nvt_spl} Stk)', menge=nvt_spl, einheit='ST'))
    if ausbl > 0:
        pos.append(dict(pos_nr='10037707', kurztext='Kabel ausblasen', menge=ausbl, einheit='M'))
    if einbl > 0:
        pos.append(dict(pos_nr='10037708', kurztext='Kabel einblasen', menge=einbl, einheit='M'))
    return pos

def _float(val, default=0):
    try: return float(str(val).replace(',', '.'))
    except: return default

def _int(val, default=0):
    try: return int(float(str(val).replace(',', '.')))
    except: return default
