def route_answer_comment(answer_id):
    if 'id' not in session:
        return redirect(url_for('route_main'))
    if request.method == "POST":
        answer = data_manager.get_row_from_table('answer', int(answer_id))
        file = request.form['message']
        data_manager.add_comment_to_answer(file, int(answer_id), session['id'])
        return redirect(f"/question/{answer[0]['question_id']}/question")
    answer = data_manager.get_row_from_table('answer', int(answer_id))
    return render_template("answer_comment.html", answer=answer, answer_id=answer_id)
def route_question_view(question_id):
    question = data_manager.get_row_from_table('question', question_id)[0]
    if 'id' in session and session["id"] == question["user_id"]:
        pass
    else:
        data_manager.vote("question", int(question_id), 1, "view_number")
    return redirect(f"/question/{question_id}/question")
def route_answer_edit(answer_id):
    if 'id' not in session:
        return redirect(url_for('route_main'))
    if request.method == "GET":
        answer = data_manager.get_row_from_table('answer', answer_id)
        return render_template("edit_answer.html", answer=answer, answer_id=answer_id)
    if request.method == "POST":
        file = [request.form[item] for item in request.form]
        image = request.files["image"]
        if image.filename != "":
            filename = data_manager.get_name_of_image(image.filename)
            image.save(os.path.join("static", filename))
            file.append(f"/static/{image.filename}")
        data_manager.edit_answer(file, int(answer_id))
        answer = data_manager.get_row_from_table('answer', answer_id)
        return redirect(f"/question/{answer[0]['question_id']}/question")
def route_answer_delete(answer_id):
    if 'id' not in session:
        return redirect(url_for('route_main'))
    answer = data_manager.get_row_from_table('answer', int(answer_id))
    if os.path.exists(answer[0]['image'][1:]):
        os.remove(answer[0]['image'][1:])
    data_manager.delete_by_id('answer', 'id', int(answer_id))
    return redirect(f"/question/{answer[0]['question_id']}/question")
def route_answer_accept_change(answer_id, value):
    answer = data_manager.get_row_from_table('answer', answer_id)
    value = util.change_boolean_value(value)
    if value:
        data_manager.vote('users', int(answer[0]["user_id"]), 15, 'reputation')
    else:
        data_manager.vote('users', int(answer[0]["user_id"]), -15, 'reputation')
    data_manager.change_answer_accepted(int(answer_id), value)
    return redirect(f"/question/{answer[0]['question_id']}/question")
def route_search(tag=None):
    if tag is None:
        search = request.args.get("search")
        questions = data_manager.search_question(search=search)
        answers = data_manager.search_answer(search=search)
        questions_with_answers = []
        for answer in answers:
            question_list = [data_manager.get_row_from_table('question', answer["question_id"])[0], answer]
            questions_with_answers.append(question_list)
        return render_template('search.html', questions=questions, answers=questions_with_answers, search=search)
    else:
        list_of_question_id = data_manager.get_whole_tags(tag_name=tag)
        questions = []
        if list_of_question_id[0]['question_id'] is not None:
            for i in list_of_question_id:
                question = data_manager.get_row_from_table('question', i["question_id"])[0]
                questions.append(question)
        return render_template('search.html', questions=questions, answers=None, search=tag)
def route_edit_comment(comment_id):
    if 'id' not in session:
        return redirect(url_for('route_main'))
    if request.method == 'GET':
        comment = data_manager.get_comment('id', int(comment_id))
        if comment[0]['question_id'] is None:
            answer = data_manager.get_row_from_table('answer', int(comment[0]['answer_id']))
            return render_template("edit_comment.html", comment=comment, answer=answer, comment_id=comment_id)
        else:
            return render_template("edit_comment.html", comment=comment, comment_id=comment_id, answer='')
    else:
        comment = data_manager.get_comment('id', int(comment_id))
        message = request.form['message']
        data_manager.edit_comment(int(comment_id), message)
        if comment[0]['question_id'] is None:
            answer = data_manager.get_row_from_table('answer', int(comment[0]['answer_id']))
            comment = answer
        return redirect(f'/question/{comment[0]["question_id"]}/question')
def route_comment_delete(comment_id):
    if 'id' not in session:
        return redirect(url_for('route_main'))
    comment = data_manager.get_comment('id', int(comment_id))
    data_manager.delete_by_id('comment', 'id', int(comment_id))
    if comment[0]['question_id'] is None:
        answer = data_manager.get_row_from_table('answer', int(comment[0]['answer_id']))
        comment = answer
    return redirect(f"/question/{comment[0]['question_id']}/question")
def route_question_comment(question_id):
    if 'id' not in session:
        return redirect(url_for('route_main'))
    if request.method == "POST":
        file = request.form['message']
        data_manager.add_comment_to_question(file, int(question_id), session['id'])
        return redirect(f"/question/{question_id}/question")
    question = data_manager.get_row_from_table('question', int(question_id))
    return render_template("question_comment.html", question=question, question_id=question_id)
def route_question_delete(question_id):
    if 'id' not in session:
        return redirect(url_for('route_main'))
    answers = data_manager.get_answers_by_question_id(int(question_id))
    question = data_manager.get_row_from_table('question', int(question_id))
    for answer in answers:
        if os.path.exists(answer['image'][1:]):
            os.remove(answer['image'][1:])
    if os.path.exists(question[0]['image'][1:]):
        os.remove(question[0]['image'][1:])
    data_manager.delete_by_id('question', 'id', int(question_id))
    data_manager.tag_delete()
    return redirect("/")
def route_question_edit(question_id):
    if 'id' not in session:
        return redirect(url_for('route_main'))
    if request.method == "GET":
        question = data_manager.get_row_from_table('question', int(question_id))
        return render_template("edit_question.html", question=question, question_id=question_id)
    if request.method == "POST":
        file = [request.form[item] for item in request.form]
        image = request.files["image"]
        if image.filename != "":
            filename = data_manager.get_name_of_image(image.filename)
            image.save(os.path.join("static", filename))
            file.append(f"/static/{image.filename}")
        data_manager.edit_question(file, int(question_id))
        return redirect(f"/question/{question_id}/question")
def route_answer_vote_down(answer_id):
    owner_id = data_manager.get_user_id_by_id('answer', int(answer_id))[0]['user_id']
    if 'id' in session and int(session["id"]) != owner_id:
        if data_manager.check_if_user_voted_answer(int(answer_id), int(session["id"]), 'False'):
            data_manager.vote("answer", int(answer_id), 1, "vote_number")
            data_manager.vote('users', int(owner_id), 2, 'reputation')
            data_manager.delete_vote('answer_id', int(answer_id), session['id'])
        elif data_manager.check_if_user_voted_answer(int(answer_id), int(session["id"]), 'True'):
            data_manager.vote("answer", int(answer_id), -2, "vote_number")
            data_manager.vote('users', int(owner_id), -12, 'reputation')
            data_manager.delete_vote('answer_id', int(answer_id), session['id'])
            data_manager.user_vote_saving('answer_id', int(answer_id), int(session['id']), 'False')
        else:
            data_manager.vote("answer", int(answer_id), -1, "vote_number")
            data_manager.vote('users', int(owner_id), -2, 'reputation')
            data_manager.user_vote_saving('answer_id', answer_id, int(session["id"]), False)
    answer = data_manager.get_row_from_table('answer', int(answer_id))
    return redirect(f"/question/{answer[0]['question_id']}/question")
def route_from_comment_to_question(answer_id):
    answer = data_manager.get_row_from_table('answer', int(answer_id))
    return redirect(f"/question/{answer[0]['question_id']}")