from flask import Flask, render_template, request
import os
import json
import mistletoe
import random

app = Flask(__name__, static_folder='static')
contents = []
rut = "./static/assets/"
rutDetour = "./static/assets_detour/"
box = 'box'
showbar = 'showbar'

f = open('db.json',)
data = json.load(f,)

fDetour = open('dbDetour.json',)
dataDetour = json.load(fDetour,)

dictio = {}
dictioDetour = {}

folders = os.listdir(rut)
foldersDetour = os.listdir(rutDetour)


for folder in folders:
    dictio[folder] = {}
    subfolders = os.listdir(rut+folder)
    # os.system("find . -name '.DS_Store' -type f -delete")
    for subfolder in subfolders:
        # os.system("find . -name '.DS_Store' -type f -delete")
        if subfolder not in dictio[folder]:
            dictio[folder][subfolder] = []
        files = os.listdir(rut+folder+'/'+subfolder)
        # print(files)
        # os.system("find . -name '.DS_Store' -type f -delete")
        for file in files:
            # print(file)

            if file not in dictio[folder][subfolder]:
                file_path = rut+folder+'/'+subfolder+'/'+file
                # print(file_path)
                if file.endswith("txt"):
                    with open(file_path) as txt:
                        output = txt.read()
                        dictio[folder][subfolder].append(output)
                elif file.endswith("md"):
                    with open(file_path, 'r') as md:
                        output = mistletoe.markdown(md)
                        dictio[folder][subfolder].append(output)
                else:
                    dictio[folder][subfolder].append(file)

n = 0


coins = ['stones', 'rare stones', 'bold people',
         'peanuts', 'wild pigs', 'Devis', 'Birbis tears']


price = {}

for keys in dataDetour:
    price[keys] = []
    r = random.randint(1, 140)
    r2 = random.randint(0, (len(coins)-1))
    price[keys] = f'{r} {coins[r2]}'


for folderDetour in foldersDetour:
    dictioDetour[folderDetour] = {}
    subfoldersDetour = os.listdir(rutDetour+folderDetour)
    os.system("find . -name '.DS_Store' -type f -delete")
    for subfolderDetour in subfoldersDetour:
        if subfolderDetour.startswith('.'):
            os.remove(rutDetour+folderDetour+'/'+subfolderDetour)
        else:
            dictioDetour[folderDetour][subfolderDetour] = []
            filesDetour = os.listdir(
                rutDetour+folderDetour+'/'+subfolderDetour)
            for fileDetour in filesDetour:
                file_path = rutDetour+folderDetour+'/'+subfolderDetour+'/'+fileDetour
                # print(file_path)
                if fileDetour.endswith("txt"):
                    with open(file_path) as txt:
                        output = txt.read()
                        dictioDetour[folderDetour][subfolderDetour].append(
                            output)
                elif fileDetour.endswith("md"):
                    with open(file_path, 'r') as md:
                        output = mistletoe.markdown(md)
                        dictioDetour[folderDetour][subfolderDetour].append(
                            output)
                else:
                    dictioDetour[folderDetour][subfolderDetour].append(
                        fileDetour)


with open("db.json", "w") as outfile:
    json.dump(dictio, outfile)

with open("dbDetour.json", "w") as outfileDetour:
    json.dump(dictioDetour, outfileDetour)

# stack tecnico compiling
with open("./static/stack_tecnico.md", 'r') as mdStack:
    stack = mistletoe.markdown(mdStack)
# colophon compiling
with open("./static/colophon.md", 'r') as mdCol:
    colophon = mistletoe.markdown(mdCol)


# for key in data:
#     for z in data[key][box]:
#         print(rut+key+box+z)


@app.route("/", methods=['GET', 'POST'])
def platform():
    # return render_template("index.html", data=data, folder=folder, box=box, showbar=showbar, rut=rut, stack=stack, folderDetour=folderDetour, dataDetour=dataDetour, rutDetour=rutDetour, )
    return render_template("index.html", data=data, folder=folder, box=box, showbar=showbar, rut=rut, stack=stack, folderDetour=folderDetour, colophon=colophon )


@app.route('/print/', methods=['POST', 'GET'])
def dat():
    classes = []
    if request.method == 'GET':
        return f'nope, go back to home'
    if request.method == 'POST':
        form_data = request.form.getlist('test')
        return render_template('prin.html', form_data=form_data, data=data, folder=folder, box=box, showbar=showbar, rut=rut, colophon=colophon,stack=stack)


@app.route("/ebay/")
def ebay():
    return render_template("ebay.html", dataDetour=dataDetour, box=box, price=price)


if __name__ == '__main__':
    app.run(host="0.0.0.0")
