using DAL;
using HeyRed.MarkdownSharp;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ver_post : System.Web.UI.Page
{
    public int post_id;
    public int user_id;

    protected void Page_Load(object sender, EventArgs e)
    {
        post_id = Convert.ToInt32(Request.QueryString["id"]);
        user_id = Convert.ToInt32(Session["user_id"]);

        if (Page.IsPostBack) return;

        Entities entities = new Entities();
        var post = entities.Posts.FirstOrDefault(p => p.id == post_id);

        if (post == null)
        {
            Response.Redirect("/index.aspx");
            return;
        }

        var user = entities.Users.FirstOrDefault(u => u.id == user_id);

    
        if(user == null)
        {
            comment_div.Visible = false;
        }
        else if (post.user_id != user.id)
        {
            editar_button.Visible = false;
            delete_button.Visible = false;
        }

        upvotes_button.Text = "+ " + post.votos.Count(v => v.positivo);
        downvotes_button.Text = "- " + post.votos.Count(v => !v.positivo);

        title_literal.Text = post.title;
        author_literal.Text = post.user.username;
        html_literal.Text = post.html;

        comment_repeater.DataSource = post.comentarios;
        comment_repeater.DataBind();
    }

    protected void OnComentarClick(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;
        if (post_id == 0) return;
        if (user_id == 0) return;

        Entities entities = new Entities();
        Markdown markdown = new Markdown();

        var comment = new Comentario();
        comment.html = markdown.Transform(comment_box.Text);
        comment.post_id = post_id;
        comment.user_id = user_id;

        entities.Comentarios.Add(comment);
        entities.SaveChanges();

        comment_box.Text = "";

        comment_repeater.DataSource = entities.Comentarios.Where(c => c.post_id == post_id).ToList();
        comment_repeater.DataBind();
    }

    
    protected void OnEditarClick(object sender, EventArgs e)
    {
        Response.Redirect("/editar_post.aspx?id=" + post_id);
    }

    protected void OnDeleteClick(object sender, EventArgs e)
    {
        if (post_id == 0) return;
        if (user_id == 0) return;

        Entities entities = new Entities();

        var post = entities.Posts.First(p => p.id == post_id);
        if (post.user.id != user_id) return;

        foreach (var voto in post.votos.ToList())
        {
            entities.Votos.Remove(voto);
        }

        foreach (var comment in post.comentarios.ToList())
        {
            entities.Comentarios.Remove(comment);
        }

        entities.Posts.Remove(post);
        entities.SaveChanges();

        Response.Redirect("/index.aspx");
    }

    protected void OnDeleteCommentClick(object sender, EventArgs e)
    {
        var button_message = ((Button)sender).CommandArgument.Split('-');

        var user_id = Convert.ToInt32(Session["user_id"]);

        if (Convert.ToInt32(button_message[1]) != user_id) return;

        Entities entities = new Entities();

        var comment_id = Convert.ToInt32(button_message[0]);
        var comment = entities.Comentarios.FirstOrDefault(c => c.id == comment_id);

        entities.Comentarios.Remove(comment);
        entities.SaveChanges();
    }

    protected void OnUpvoteClick(object sender, EventArgs e)
    {
        if (post_id == 0) return;
        if (user_id == 0) return;

        Entities entities = new Entities();

        var voto = entities.Votos.FirstOrDefault(v => v.post_id == post_id && v.user_id == user_id);

        if (voto == null)
        {
            var upvote = new Voto();
            upvote.positivo = true;
            upvote.post_id = post_id;
            upvote.user_id = user_id;

            entities.Votos.Add(upvote);
            entities.SaveChanges();
        }
        else if (!voto.positivo)
        {
            voto.positivo = true;
            entities.SaveChanges();
        }

        upvotes_button.Text = "+ " + entities.Votos.Count(v => v.post_id == post_id && v.positivo);
        downvotes_button.Text = "- " +  entities.Votos.Count(v => v.post_id == post_id && !v.positivo);
    }

    protected void OnDownvoteClick(object sender, EventArgs e)
    {
        if (post_id == 0) return;
        if (user_id == 0) return;

        Entities entities = new Entities();

        var voto = entities.Votos.FirstOrDefault(v => v.post_id == post_id && v.user_id == user_id);

        if (voto == null)
        {
            var downvote = new Voto();
            downvote.positivo = false;
            downvote.post_id = post_id;
            downvote.user_id = user_id;

            entities.Votos.Add(downvote);
            entities.SaveChanges();
        }
        else if (voto.positivo)
        {
            voto.positivo = false;
            entities.SaveChanges();
        }

        upvotes_button.Text = "+ " + entities.Votos.Count(v => v.post_id == post_id && v.positivo);
        downvotes_button.Text = "- " + entities.Votos.Count(v => v.post_id == post_id && !v.positivo);
    }
}