import numpy as np
import pandas as pd
from flask import Flask, render_template_string, render_template


#####################################################################  html_01
def html_01(Dh, s, ncx, ncy, P, fpc, fy, fs, fyh, confined, B, H, clb, MLR):
    html_template = """
        <div class="txtRep">
        <h2 style="text-align: center;">Steel Properties</h2>
        <table>
            <tr><th>Property</th><th>Value</th></tr>
            <tr><td>Diameter of transverse steel</td><td>{{ Dh|round(1) }} mm</td></tr>
            <tr><td>Spacing of transverse steel</td><td>{{ s|round(1) }} mm</td></tr>
            <tr><td># Legs transv. steel x_dir (confinement)</td><td>{{ ncx|round(1) }}</td></tr>
            <tr><td># Legs transv. steel y_dir (shear)</td><td>{{ ncy|round(1) }}</td></tr>
            <tr><td>Axial load</td><td>{{ (P/1000)|round(2) }} kN</td></tr>
            <tr><td>Concrete compressive strength</td><td>{{ fpc|round(2) }} MPa</td></tr>
            <tr><td>Long steel yielding stress</td><td>{{ fy|round(2) }} MPa</td></tr>
            <tr><td>Long steel max. stress</td><td>{{ fs|round(2) }} MPa</td></tr>
            <tr><td>Transverse steel yielding stress</td><td>{{ fyh|round(2) }} MPa</td></tr>
        </table>

        <h2 style="text-align: center;">Rectangular Section</h2>
        <p style="text-align: center;">{{ 'Lightweight Concrete' if confined.lower() == 'mclw' else 'Normalweight Concrete' }}</p>
        <table>
            <tr><th>Width</th><th>Height</th><th>Cover to Longitudinal Bars</th></tr>
            <tr><td>{{ B|round(1) }} mm</td><td>{{ H|round(1) }} mm</td><td>{{ clb|round(1) }} mm</td></tr>
        </table>

        <h2 style="text-align: center;">Longitudinal Reinforcement</h2>
        <table>
            <tr><th>Dist. Top (mm)</th><th># Long Bars</th><th>Diameter (mm)</th></tr>
            {% for row in MLR %}
            <tr>
                <td>{{ row[0]|round(1) }}</td>
                <td>{{ row[1]|round(1) }}</td>
                <td>{{ row[2]|round(2) }}</td>
            </tr>
            {% endfor %}
        </table>
        </div>
    """
    return render_template_string(html_template, Dh=Dh, s=s, ncx=ncx, ncy=ncy, P=P, fpc=fpc, fy=fy, fs=fs, fyh=fyh, confined=confined, B=B, H=H, clb=clb, MLR=MLR)


#####################################################################  html_02
def html_02(Dh, s, ncx, ncy, P, fpc, fy, fs, fyh):
    html_template = """
        <div class="txtRep">
        <h2 style="text-align: center;">Steel Properties</h2>
        <table>
            <tr><th>Property</th><th>Value</th></tr>
            <tr><td>Diameter of transverse steel</td><td>{{ Dh|round(1) }} mm</td></tr>
            <tr><td>Spacing of transverse steel</td><td>{{ s|round(1) }} mm</td></tr>
            <tr><td># Legs transv. steel x_dir (confinement)</td><td>{{ ncx|round(1) }}</td></tr>
            <tr><td># Legs transv. steel y_dir (shear)</td><td>{{ ncy|round(1) }}</td></tr>
            <tr><td>Axial load</td><td>{{ (P/1000)|round(2) }} kN</td></tr>
            <tr><td>Concrete compressive strength</td><td>{{ fpc|round(2) }} MPa</td></tr>
            <tr><td>Long steel yielding stress</td><td>{{ fy|round(2) }} MPa</td></tr>
            <tr><td>Long steel max. stress</td><td>{{ fs|round(2) }} MPa</td></tr>
            <tr><td>Transverse steel yielding stress</td><td>{{ fyh|round(2) }} MPa</td></tr>
        </table>
        </div>

    """    
    return render_template_string( html_template, Dh=Dh, s=s, ncx=ncx, ncy=ncy, P=P, fpc=fpc, fy=fy, fs=fs, fyh=fyh)

#####################################################################  html_03
def html_03(bending, ductilitymode, LongSteelRatio, TransvSteelRatioAverage, AxialRatio, output):
    return render_template('display3.html', bending=bending, ductilitymode=ductilitymode, LongSteelRatio=LongSteelRatio, TransvSteelRatioAverage=TransvSteelRatioAverage, AxialRatio=AxialRatio, output=output)

#####################################################################  html_04
def html_04(outputbilin, message, fyM, fycurv, Mn, eqcurv, SectionCurvatureDuctility, DisplDuct):
    return render_template('display4.html', outputbilin=outputbilin, message=message, fyM=fyM, fycurv=fycurv, Mn=Mn, eqcurv=eqcurv, SectionCurvatureDuctility=SectionCurvatureDuctility, DisplDuct=DisplDuct)


#####################################################################  html_05
def html_05(criteria, faildispl, failduct, failforce, failcurv, failCuDu, failmom):   
    return render_template('display5.html', criteria=criteria, faildispl=faildispl, failduct=failduct, failforce=failforce, failcurv=failcurv, failCuDu=failCuDu, failmom=failmom)



#####################################################################  html_07  
def html_07(outputlimit, ecser, esser, ecdam, esdam, confined, ecumander, Ec, G, Agross, Ieq, Bi, Lp, PTid, PCid, Mn):
    outputlimit = outputlimit.reshape(-1, 8)
    return render_template('display7.html', outputlimit=outputlimit, ecser=ecser, esser=esser, ecdam=ecdam, esdam=esdam, confined=confined, ecumander=ecumander, Ec=Ec, G=G, Agross=Agross, Ieq=Ieq, Bi=Bi, Lp=Lp, PTid=PTid, PCid=PCid, Mn=Mn)
    
#####################################################################  mainOut
def mainOut(coverstrain, corestrain, ejen, steelstrain, mom, curv, Force, displsh, displf, displ, V, Vd, curvbilin, mombilin, displbilin, forcebilin, Hcore, Bcore, AsLong, csid, ecun, fcun, Agross, ec, es, fc, fs, ssid):
    output = np.vstack([coverstrain, corestrain, ejen, steelstrain, mom, curv, Force, displsh, displf, displ, V, Vd])
    outputbilin = np.vstack([curvbilin, mombilin, displbilin, forcebilin])
    Acore = Hcore * Bcore
    #return f"csid={csid} - ec={ec} - fc={fc}"
    PCid = np.interp(csid, ec, fc) * (Acore - AsLong) + np.interp(csid, ecun, fcun) * (Agross - Acore) + AsLong * np.interp(csid, es, fs)
    PTid = AsLong * np.interp(ssid, es, fs)
    #//////////print('$$ 0 $$$$$$$$$')

    output_df = pd.DataFrame(output.T, columns=['coverstrain', 'corestrain', 'ejen', 'steelstrain', 'mom', 'curv', 'Force', 'displsh', 'displf', 'displ', 'V', 'Vd'])
    outputbilin_df = pd.DataFrame(outputbilin.T, columns=['curvbilin', 'mombilin', 'displbilin', 'forcebilin'])   
    
    html_1 = html_01(Dh=12.5, s=200, ncx=4, ncy=6, P=50000, fpc=30, fy=420, fs=450, fyh=250, confined='mclw', B=500, H=700, clb=40, MLR=[[100, 4, 16], [200, 6, 20], [300, 8, 25]])
    html_2 = html_02(Dh=12.5, s=200, ncx=4, ncy=6, P=50000, fpc=30, fy=420, fs=450, fyh=250)
        
    bending = 'single'
    ductilitymode = 'uniaxial'
    LongSteelRatio = 0.35
    TransvSteelRatioAverage = 0.25
    AxialRatio = 0.5
    output = [
        [0.001, 0.002, 0.5, 0.01, 10.2, 0.05, 15.3, 0.02, 0.03, 0.04, 0.5, 0.6],
        [0.003, 0.004, 0.6, 0.02, 12.3, 0.06, 18.4, 0.03, 0.04, 0.05, 0.6, 0.7]
    ]
    html_3 = html_03(bending=bending, ductilitymode=ductilitymode, LongSteelRatio=LongSteelRatio, TransvSteelRatioAverage=TransvSteelRatioAverage, AxialRatio=AxialRatio, output=output) 
    
    return html_1+html_2+html_3

#####################################################################  html_08
def html_08(csid, ssid, outputi, PTid, PCid, PB, MB, PB13, MB13, PB23, MB23):
    outputi = list(zip(outputi[0], outputi[1]))
    
    return render_template("display8.html", 
                           csid=csid, ssid=ssid, 
                           outputi=outputi, 
                           PTid=PTid, PCid=PCid, PB=PB, MB=MB, 
                           PB13=PB13, MB13=MB13, PB23=PB23, MB23=MB23)


