using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
public partial class showarticle : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private bool LikeVoted(string userIP, int articleId)
    {
        bool flag;
        using (DataBaseContext db = new DataBaseContext())
        {
            db.likes.Where(current => current.IpAddress == userIP && current.articleID == articleId).Load();
            if (db.likes.Local.Count() != 0)
                flag = true;
            else
                flag = false;
        }
        return flag;
    }
    private bool disLikeVoted(string userIP, int articleId)
    {
        bool flag;
        using (DataBaseContext db = new DataBaseContext())
        {
            db.dislikes.Where(current => current.IpAddress == userIP && current.articleID == articleId).Load();
            if (db.dislikes.Local.Count() != 0)
                flag = true;
            else
                flag = false;
        }
        return flag;
    }
    protected void btnLike_Click(object sender, EventArgs e)
    {
        using (DataBaseContext db = new DataBaseContext())
        {
            Button m = (Button)sender;
            int i = Int32.Parse(m.CommandArgument);
            Button btnLike = (Button)GridView1.Rows[i].FindControl("btnLike");
            int id = int.Parse(Request.QueryString["id"]);                
            like obj = new like();
            obj.articleID = id;
            db.likes.Add(obj);
            db.SaveChanges();
            string Msg = "رای شما با موفقیت ثبت شد";
            ScriptManager.RegisterStartupScript(Page, typeof(Page),
            "Alert", "<script>alert('" + Msg + "');</script>", false);
            int articleId = int.Parse(Request.QueryString["id"]);
            string userIp = HttpContext.Current.Request.UserHostAddress;
            if (LikeVoted(userIp, articleId))
                btnLike.Enabled = false;
        }
    }
    protected void btnDisLike_Click(object sender, EventArgs e)
    {
        using (DataBaseContext db = new DataBaseContext())
        {
            Button m = (Button)sender;
            int i = Int32.Parse(m.CommandArgument);
            Button btnDisLike = (Button)GridView1.Rows[i].FindControl("btnDisLike");
            int id = int.Parse(Request.QueryString["id"]);
            dislike obj = new dislike();
            obj.articleID = id;
            db.dislikes.Add(obj);
            db.SaveChanges();
            string Msg = "رای شما با موفقیت ثبت شد";
            ScriptManager.RegisterStartupScript(Page, typeof(Page),
            "Alert", "<script>alert('" + Msg + "');</script>", false);
            int articleId = int.Parse(Request.QueryString["id"]);
            string userIp = HttpContext.Current.Request.UserHostAddress;
            if (disLikeVoted(userIp, articleId))
                btnDisLike.Enabled = false;
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;
        if (row.DataItem == null)
            return;
        Button btnLike = (Button)row.FindControl("btnLike");
        Button btnDisLike = (Button)row.FindControl("btnDisLike");
        int articleId = int.Parse(Request.QueryString["id"]);
        string userIp = HttpContext.Current.Request.UserHostAddress;
        if (LikeVoted(userIp, articleId))
            btnLike.Enabled = false;
        if (disLikeVoted(userIp, articleId))
            btnDisLike.Enabled = false;

      
    }
}