import math
import txt_reader
import haiku
import constants
import nltk
import re


def wrap_string_in_html(output_file_name, source_file):
    from webbrowser import open_new_tab

    filename = output_file_name + '.html'
    f = open(filename, 'w')

    wrapper = """<html>
    <head>
        <title>%s output</title>
        <style>
            h2 {
                text-align: center;
            }
            body {
                font-family: Georgia;
                word-wrap: break-word;
            }            
            #poem {
                background-color: white;
            }
            .container {
                width: 80%s;
                height: 200px;
                margin: auto;
                padding: 10px;
            }
            .blackout-float {
                width: 500px;
                padding: 10px 10px 10px 10px;
                background-color: #101010;
                color: black;
                float: right;
            }
            .haiku-float {
                width: 300px;
                padding: 10px 10px 10px 10px;
                font-size: 20px;
                float: left
            }
        </style>
    </head>
        <body>  
            <section class="container">
                <h2>
                    Poem Generated By 
                    <a href=\"https://github.com/kabramow/roboetry\">Roboetry</a> 
                </h2>
                <h2>
                    from %s
                </h2>
                <div class=\"haiku-float\"><p>%s</p></div>
                <div class=\"blackout-float\"><p>%s</p></div>

            </section>

        </body>
    </html>"""

    # text = txt_reader.get_random_selection(source_file)
    # poem_words = txt_reader.get_up_to_20_random_words(text)

    title = txt_reader.get_title(source_file)

    haiku_poem = haiku.Haiku(source_file)

    whole = wrapper % (output_file_name, "%", title, poem_generator(haiku_poem), body_generator(haiku_poem))
    f.write(whole)
    f.close()

    open_new_tab(filename)


def body_generator(haiku_poem):
    """docString
    purpose statement
    None -> String"""

    text_words = haiku_poem.chosen_lines_list
    poem = nltk.word_tokenize(haiku_poem.poem)
    out_text = ""

    for line in text_words:
        for word in line:
            if len(poem) != 0:
                if word == poem[0]:
                    poem = poem[1:]
                    out_text += "<span id=\"poem\">" + word + "</span>" + " "
                else:
                    out_text += word + " "
            else:
                out_text += word + " "

        out_text += "<br>"

    return out_text


def poem_generator(haiku_poem):
    poem_text = haiku_poem.poem
    poem_text = poem_text.replace("\n", "\n</p><p>\n")
    return poem_text


def poem_generator_for_non_haiku(poem_words):
    words_per_line = math.trunc(len(poem_words) / 3)
    first_line = poem_words[0:words_per_line]
    middle_line = poem_words[words_per_line:(-1 * words_per_line)]
    last_line = poem_words[(-1 * words_per_line):]
    ret_string = ""
    for word in first_line:
        ret_string += word + " "
    ret_string += "\n</p><p>\n"
    for word in middle_line:
        ret_string += word + " "
    ret_string += "\n</p><p>\n"
    for word in last_line:
        ret_string += word + " "

    return ret_string
