from flask import Flask, render_template, request, redirect, url_for, session
import random
import json
import os
import datetime

from templates.uni.subjects.halozatok.halozatok_app import halozatok_vizsga_teszt
from templates.uni.subjects.portalfejlesztes_net_ben.portalfejlesztes_app import portalfejlesztes_net_ben
from templates.uni.subjects.vallalati_informacios_rendszerek.vir_app import vallalati_informacios_rendszerek
from templates.uni.subjects.szamitogepek_mukodese.szgm_app import szamitogepek_mukodese

app = Flask(__name__)
app.secret_key = 'your_secret_key'



@app.route('/')
@app.route('/home')
def home():
    quotes = []
    with open('./static/text_files/home/quotes.json', 'r', encoding='utf-8') as f:
        quotes = json.load(f)
    quote = random.sample(quotes, 1)

    posts = []
    with open("posts.json", "r") as f:
        posts = json.load(f)

    posts.reverse()
    print(posts)
    return render_template('home/home.html', quote=quote[0], post_data=posts[:6])

@app.route('/about')
def about():
    return render_template('about/about.html')

@app.route('/uni')
def uni():
    session.pop('question_index', None)
    session.pop('answers', None)
    session.pop('quiz_questions', None)
    return render_template('uni/uni.html')

@app.route('/projects')
def projects():
    posts = []
    with open("posts.json", "r") as f:
        posts = json.load(f)

    posts.reverse()
    return render_template('projects/projects.html', post_data=posts)

@app.route('/projects/search', methods=['GET', 'POST'])
def search():
    searched = request.args.get('searched')
    searched = str(searched).lower()
    posts = []
    with open("posts.json", "r") as f:
        posts = json.load(f)
    posts.reverse()
    if searched == "":
        return render_template('projects/projects.html', post_data=posts)
    found = []
    for i in posts:
        if searched in  [j.lower() for j in i['topics']] and i not in found:
            found.append(i)
        if searched in i['title'].lower() and i not in found:
            found.append(i)
        if searched in i['content'].lower() and i not in found:
            found.append(i)
        if searched in i['date'] and i not in found:
            found.append(i)

    return render_template('projects/projects.html', post_data=found)

@app.route('/git')
def git():
    return render_template('git.html')

@app.route('/posts/<slug>')
def post(slug):
    posts = []
    with open("posts.json", "r") as f:
        posts =  json.load(f)
    # Find the post with the matching slug
    post = next((p for p in posts if p['slug'] == slug), None)
    
    if not post:
        abort(404)

    # Now we need to render a template that corresponds to the slug.
    # Let's assume that the HTML file is named like the slug (e.g., 'my-first-post.html').
    template_name = f"posts/{slug}.html"  # looks for a file like posts/my-first-post.html

    # Check if the template file exists before rendering
    if not os.path.exists(os.path.join('templates', template_name)):
        abort(404)  # If the file does not exist, show 404 error
    
    # Render the post template
    return render_template(template_name, post=post)


# Register the blueprints with appropriate URL prefixes
app.register_blueprint(halozatok_vizsga_teszt, url_prefix='/uni/halozatok')
app.register_blueprint(portalfejlesztes_net_ben, url_prefix='/uni/portalfejlesztes_net_ben')
app.register_blueprint(vallalati_informacios_rendszerek, url_prefix='/uni/vallalati_informacios_rendszerek')
app.register_blueprint(szamitogepek_mukodese, url_prefix='/uni/szamitogepek_mukodese')


if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port=5000)
