from flask import render_template,redirect,flash,url_for,abort,request
from ..request import get_quotes
from .forms import  BlogForm, CommentsForm,UpdateProfile
from . import main
from .. import db,photos
from flask_login import login_required,current_user
from ..models import User,Blog,Comment,Subscribe
from ..email import mail_message
import markdown2

@main.route('/')
def index():
    '''
    Functin that returns index page and its data
    '''
    title = 'BlogApp-Welcome to blogapp your trusted site for creativity'
    
    quote_item = get_quotes()
    blog_items = Blog.get_all_blogs()
    print(quote_item)
    return render_template('index.html',title = title , quote_item=quote_item,blog_items=blog_items) 

@main.route('/about')
def about():
    '''
    Function that returns the about page and its contents
    '''
    title = 'About'
    return render_template('about.html')

@main.route('/post/<int:blog_id>',methods=['GET','POST'])
def post(blog_id):
    '''
    Function that retuns the blog page and its data
    '''
    blog_item = Blog.query.filter_by(id=blog_id).one()
    post_comments = Comment.get_comments(blog_id)
    
    return render_template('post.html',blog_item=blog_item,post_comments=post_comments)

@main.route('/createblog',methods=['GET','POST'])
@login_required
def createblog():
    '''
    View root page that returns the createblog post page and its data
    '''
    form = BlogForm()
    if form.validate_on_submit():
        title=form.title.data
        content=form.content.data
        
        new_post = Blog(title=title,content=content, user=current_user)
        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for('main.index'))
    return render_template('createblog.html',form=form)

@main.route('/blog/comments/newblog/<int:id>',methods=['GET','POST'])
@login_required
def new_comment(id):
    form = CommentsForm()
    
    if form.validate_on_submit():
        new_comment = Comment(blog_id=id,comment=form.comment.data)
        new_comment.save_comments()
        return redirect(url_for('main.post',blog_id=id))
    return render_template('new_comment.html',comment_form=form)

@main.route('/user/<uname>/update/pic',methods =['POST'])
@login_required
def update_pic(uname):
    user = User.query.filter_by(username=uname).first()
    if 'photo' in request.files:
        filename = photos.save(request.files['photo'])
        path= f'photos/{filename}'
        user.profile_pic_path = path
        db.session.commit()
    return redirect(url_for('main.profile',uname=uname))

@main.route('/user/<uname>') 
def profile(uname):
    
    user = User.query.filter_by(username = uname).first()
    
    if user is None:
        abort(404)
    
    return render_template("profile/profile.html",user=user)
 
@main.route('/user/<uname>/update',methods = ['GET','POST'])
@login_required
def update_profile(uname):
    user = User.query.filter_by(username = uname).first()
    if user is None:
        abort(404)

    form = UpdateProfile()

    if form.validate_on_submit():
        user.bio = form.bio.data

        db.session.add(user)
        db.session.commit()

        return redirect(url_for('.profile',uname=user.username))
    
    return render_template('profile/update.html',form =form)

@main.route("/post/<int:blog_id>/update", methods=['GET', 'POST'])
def update_post(blog_id):
    post = Blog.query.get_or_404(blog_id)
    if post.user != current_user:
        abort(403)
    form = BlogForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('main.post', blog_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    return render_template('createblog.html', title='Update Post', form=form)

@main.route("/post/<int:blog_id>/delete", methods=['POST'])
@login_required
def delete_post(blog_id):
    post = Blog.query.get_or_404(blog_id)
    if post.user != current_user:
        abort(403)
    db.session.delete(post)
    db.session.commit()
    
    flash('Your post has been deleted!', 'success')
    return redirect(url_for('main.index'))



#sSubscribe
@main.route('/subscribe', methods=['GET', 'POST'])
def subscribe():
    '''
    Function to send email upon subscription
    '''
    if request.method == 'POST':
        email = request.form['email']
        new_email = Subscribe(email=email)
        db.session.add(new_email)
        db.session.commit()
        # mail_message("Welcome to Blogging site","email/welcome_subscriber", new_email.email,new_email=new_email)
        flash('Thank you for your subscribing')
        return redirect(url_for('main.index'))
        