#! /usr/bin/env python3
###############################################################################
# (c) Roger Whittaker 2015-2023
# roger@disruptive.org.uk
# Free software licenced under the GPL
###############################################################################
from cgi import FieldStorage
from html import escape
from subprocess import Popen, PIPE

###############################################################################
from checkform import good
from config import url

###############################################################################
print("Content-Type: text/html")
print()
print("<!DOCTYPE html>")
print("<html>")
print("<head>")
print('<meta charset="UTF-8">')
print("<title>Generalised Penrose Pattern Generator</title>")
print("</head>")
print("<body>")
print(
    '<h1 style="text-align:center;">Generalised Penrose pattern generator</h1>'
)
print('<table style="border:1px solid black;">')
print("<tr>")
print(
    '<td style="width:10%;background-color:beige;vertical-align:top;text-align:center;">'
)
print("<h3>gppg</h3>")
print("<hr />")

print('<form id="form" action="index.py" method="GET">')
print('    <label for="iterations">Iterations: (+ve integer)</label><br >')
print(
    '    <input type="text" size="3" name="iterations" id="iterations" /><br /><hr />'
)
print('    <label for="scale">Scale: (any number)</label><br />')
print('    <input type="text" size="3" name="scale" id="scale"/><br /><hr />')
print('    <label for="params">Parameters: (space separated)</label>')
print(
    '    <input type="text" size="28" name="params" id="params" /><br /><hr />'
)
print('    <input type="submit" value="go" /><br /><hr />')
print('    <a href="./index.py">Reset</a><hr />')
print("</form>")

print('<a href="./about.py">About gppg</a><hr />')
print("<h4>Sample patterns</h4>")
print(
    '<a href="'
    + url
    + 'index.py?iterations=11&amp;scale=1&amp;params=0.2+0.2+0.2+0.2+0.2">Classic Penrose (all g = 0.2)</a><br />'
)
print(
    '<a href="'
    + url
    + 'index.py?iterations=11&amp;scale=1&amp;params=0.4+0.4+0.4+0.4+0.4">Classic Penrose (all g = 0.4)</a><br />'
)
print(
    '<a href="'
    + url
    + 'index.py?iterations=11&amp;scale=1&amp;params=0.5+0.5+0.5+0.5+0.5">n = 5, all g = 0.5</a><br />'
)
print(
    '<a href="'
    + url
    + 'index.py?iterations=28&amp;scale=0.25&amp;params=0.125+0.125+0.125+0.125+0.125+0.125+0.125+0.125">n = 8, all g = 0.125</a><br />'
)
print(
    '<a href="'
    + url
    + 'index.py?iterations=16&amp;scale=0.5&amp;params=0.5+0.5+0.5+0.5+0.5+0.5+0.5">n = 7, all g = 0.5</a><br />'
)
print(
    '<a href="'
    + url
    + 'index.py?iterations=20&amp;scale=0.5&amp;params=0.1+0.2+0.3+0.4+0.7+0.3+0.1+0.55+0.23">n = 9, no central symmetry</a><br />'
)
print("<hr />")
print(
    'In memory of <a href="https://ejw.whittaker.info">EJW Whittaker</a></p>'
)
print("</td>")
print('<td style="width:80%;vertical-align:top;background-color:beige;">')


def stick(lst):
    out = lst[0]
    for i in lst[1:]:
        out = out + "+" + i
    return out


form = FieldStorage()

if not good(form):
    print("<p>Incomplete or incorrect form values entered: please retry</p>")
    print(
        '</td><td style="width:10%;background-color:beige;vertical-align:top;"><p></p></td></tr></table></body>'
    )
    print("</html>")
    exit()

else:
    parlist = []
    if "params" in form:
        parlist = form["params"].value.split()
    pars = ""
    for par in parlist:
        pars = pars + " " + par
    if "iterations" in form:
        it = form["iterations"].value
    else:
        print(
            '</td><td style="width:10%;background-color:beige;vertical-align:top;"></td></tr></table></body></html>'
        )
        exit()
    if "scale" in form:
        sc = form["scale"].value
    else:
        print(
            '</td><td style="width:10%;background-color:beige;vertical-align:top;"></td></tr></table></body></html>'
        )
        exit()
    command = "./gppg -t svg " + " " + it + " " + sc + " " + pars
    output = Popen(command.split(), stdout=PIPE).communicate()[0]
    it = escape(form["iterations"].value)
    sc = escape((form["scale"]).value)
    pa = escape(stick(parlist))
    v = "?iterations=" + it + "&scale=" + sc + "&params=" + pa
    lnk0 = '<a href="' + url + "inpdf.py" + escape(v) + '"' + ">view pdf</a>"
    lnk1 = (
        '<a href="' + url + "dlpdf.py" + escape(v) + '"' + ">download pdf</a>"
    )
    lnk2 = (
        '<a href="'
        + url
        + "ps.py"
        + escape(v)
        + '"'
        + ">download postscript</a><br />"
    )
    lnk3 = (
        '<a href="'
        + url
        + "svg.py"
        + escape(v)
        + '"'
        + ">download svg</a><br />"
    )
    print('<div style="text-align:center;border:solid 1px black;">\n')
    print(output.decode())
    print("</div>\n")
    print("</td>")
    print('<td style="width:10%;background-color:beige;vertical-align:top;">')
    print(
        "<p>"
        + lnk0
        + "</p>"
        + "<p>"
        + lnk1
        + "</p>"
        + "<p>"
        + lnk2
        + "</p>"
        + "<p>"
        + lnk3
        + "</p>"
    )
    print("</td></tr></table></body>")
    print("</html>")
