"""main program for runnin web interface via flask"""

import random
import os
import requests
from flask import Flask, render_template, request

from MemeEngine import MemeEngine
from QuoteEngine import Ingestor

app = Flask(__name__)

meme = MemeEngine('./static')


def setup():
    """ Load all resources """

    quote_files = ['./_data/DogQuotes/DogQuotesTXT.txt',
                   './_data/DogQuotes/DogQuotesDOCX.docx',
                   './_data/DogQuotes/DogQuotesPDF.pdf',
                   './_data/DogQuotes/DogQuotesCSV.csv']

    quotes = []
    for f in quote_files:
        quotes.extend(Ingestor.parse(f))

    images_path = "./_data/photos/dog/"

    imgs = []
    for root, dirs, files in os.walk(images_path):
        imgs = [os.path.join(root, name) for name in files]

    return quotes, imgs


quotes, imgs = setup()


@app.route('/')
def meme_rand():
    """ Generate a random meme """

    img = random.choice(imgs)
    quote = random.choice(quotes)

    out_path = meme.make_meme(img, quote.body, quote.author)
    #return render_template('meme.html', path=out_path)
    return render_template('meme.html', path=os.path.relpath(out_path))


@app.route('/create', methods=['GET'])
def meme_form():
    """ User input for meme information """
    return render_template('meme_form.html')


@app.route('/create', methods=['POST'])
def meme_post():
    """ Create a user defined meme

    Help was obtained from knowledge Area
    """
    
    image_url = request.form['image_url']
    img_request = requests.get(image_url, allow_redirects=True)
    
    body = request.form['body']
    author = request.form['author']
    

    # form param to a temp local file.
    tmp = f'./static/{random.randint(0, 1000000)}.jpg'

    with open(tmp, 'wb') as img_file:

        img_file.write(img_request.content)

    # generate a meme using this temp file.
    path = meme.make_meme(tmp, body, author)

    # Remove the temporary saved image.
    os.remove(tmp)

    return render_template('meme.html', path=path)


if __name__ == "__main__":
    app.run()



"""main program for runnin web interface via flask"""

import random
import os
import requests
from flask import Flask, render_template, request

from MemeEngine import MemeEngine
from QuoteEngine import Ingestor

app = Flask(__name__)

meme = MemeEngine('./static')


def setup():
    """ Load all resources """

    quote_files = ['./_data/DogQuotes/DogQuotesTXT.txt',
                   './_data/DogQuotes/DogQuotesDOCX.docx',
                   './_data/DogQuotes/DogQuotesPDF.pdf',
                   './_data/DogQuotes/DogQuotesCSV.csv']

    quotes = []
    for f in quote_files:
        quotes.extend(Ingestor.parse(f))

    images_path = "./_data/photos/dog/"

    imgs = []
    for root, dirs, files in os.walk(images_path):
        imgs = [os.path.join(root, name) for name in files]

    return quotes, imgs


quotes, imgs = setup()


@app.route('/')
def meme_rand():
    """ Generate a random meme """

    img = random.choice(imgs)
    quote = random.choice(quotes)

    out_path = meme.make_meme(img, quote.body, quote.author)
    #return render_template('meme.html', path=out_path)
    return render_template('meme.html', path=os.path.relpath(out_path))


@app.route('/create', methods=['GET'])
def meme_form():
    """ User input for meme information """
    return render_template('meme_form.html')


@app.route('/create', methods=['POST'])
def meme_post():
    """ Create a user defined meme

    Help was obtained from knowledge Area
    """
    
    image_url = request.form['image_url']
    img_request = requests.get(image_url, allow_redirects=True)
    
    body = request.form['body']
    author = request.form['author']
    

    # form param to a temp local file.
    tmp = f'./static/{random.randint(0, 1000000)}.jpg'

    with open(tmp, 'wb') as img_file:

        img_file.write(img_request.content)

    # generate a meme using this temp file.
    path = meme.make_meme(tmp, body, author)

    # Remove the temporary saved image.
    os.remove(tmp)

    return render_template('meme.html', path=path)


if __name__ == "__main__":
    app.run()



main program for runnin web interface via flask Load all resources Generate a random meme #return render_template('meme.html', path=out_path) User input for meme information Create a user defined meme      Help was obtained from knowledge Area # form param to a temp local file. # generate a meme using this temp file. # Remove the temporary saved image.