using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CarGallery
{
    public partial class ViewSinglePost : System.Web.UI.Page
    {
        DatabaseRepository repo;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            repo = new DatabaseRepository();

            // hamoontor ke to class e DatabaseRepository yejash tozih dadam mishe vase har safhe tedadi parameter e voroodi 
            // dar nazar begirim , masalan vase in safhe ma post_id ro dar nazar gereftim
            // be in soorat to URL estefade mishe : addresseSite/ViewSinglePost.aspx?post_id=yeChizi
            // niaz be tarif kardan nadare , faqat kafie az tariqe request.params["esmi ke doos dari"] oon meqdar ro beegiri
            
            
            // oomadim goftim age meqdare parameter khali nabood

            if (Request.Params["post_id"] != "" && Request.Params["post_id"] != null)
            {
                DataTable dt = null;
                // ye data Table besaz

                try // say kon post e taki i ke ID sh barabare ba meqdare parameter e post_id ro beriz to datatable i ke sakhtim
                    // age error dad yani post vojood nadare ya be ebarate behtar , post_id eshtebas
                {
                    dt = repo.postGetSingle(Request.Params["post_id"].ToString());

                    // postGetSingle ye method e ke tooye DatabaseRepository tarifesh kardim va ye datatable barmigardoone ba in sotoon ha :
                    // name,title,description,img,visits,commentsCount,DateTime,comment,slide
                    
                }
                catch
                {// age error dad bargard be safheye Default
                    Response.Redirect("Default.aspx");
                }

                // age error nadad ham edameye dastan ro mire jolo

                // textBox ha va object haye digeyi ke to in safhe tarif karde boodim ro meqdar dahi mikonim
                // vase gereftane ye meqdare taki az DataTable be in soorat amal mikonim

                // esmeTable.Rows[shomareSatr][ShomareSotoon].ToString();
                
                lblPostCreator.Text = "Posted By : " + dt.Rows[0][0].ToString();
                // hamoontor ke yeja goftam , shomare khoone ha az 0 shoroo mishe ! pas vaqti migim satre 0 , sotoone 0 , yani az satre 0 sotoone name
                // (name,title,description,img,visits,commentsCount,DateTime,comment,slide)

                lblTitle.Text = dt.Rows[0][1].ToString();
                // vaqti migim satre 0 sotoone 1 , yani az satre 0 sotoone Title

                lbldescription.Text = "<br>" + dt.Rows[0][2].ToString().Replace("\r\n", "<br/>");

                image.ImageUrl = "./photos/" + dt.Rows[0][3].ToString();

                lblvisits.Text = "Visits : " + (int.Parse(dt.Rows[0][4].ToString()) + 1).ToString();
                
                lblDateTime.Text = "Date : " + dt.Rows[0][6].ToString();

                // age sotoon e 8 , sotooni hast ke fa al boodan ya naboodan e comment ha ro moshakhas mikone
                // age fa al nabood yani false bood
                // pas bayad biaym kole object hayi ke vase comment gozashtan hast ro qeyre fa al konim

                if (dt.Rows[0][8].ToString().ToLower() == "false")
                {
                    txtEmail.Enabled = false;
                    txtMessage.Enabled = false;
                    txtName.Enabled = false;

                    txtEmail.Text = "Comments are disabled for this post";
                    txtMessage.Text = "Comments are disabled for this post";
                    txtName.Text = "Comments are disabled for this post";

                    btnSubmit.Enabled = false;
                }


                // hamchenin inja ham gridview1 , objecti hast ke comment ha ro neshoon mide
                // datasourcesh ro migim comment hayi ke post_id shoon barabar ba chizi ke to parameter hast bargardoon

                gridView1.DataSource = repo.commentsGet(Request.Params["post_id"].ToString());

                gridView1.DataBind(); // va Bind kon


            } // age parameter khali ya null bood , boro be safhe Default
            else
                Response.Redirect("Default.aspx");                

            if(!IsPostBack) // ispostback , vaqti safhe ro baz mikoni ya refresh mikoni / ispostback false mishe
                // vaqti ye dokme ro to safhe roosh click mikoni , ispostback true mishe
                // gahi lazeme tashkhis bedi safhe baz shode , ya inke dokme feshorde shode ! az in tariq mifahmi
                // inja gofte age mostaqim varede safhe shode bood bia in kara ro anjam bede
            {
                // be tedade moshahedat e post e post_id ezafe kon

                repo.postVisitPlus(Request.Params["post_id"]);
            }
        }

        protected void lbtnViewFull_Click(object sender, EventArgs e)
        {
            // ye dokme darim to safhe roosh click koni ax ro be soorate kamel baz mikone
            Response.Redirect(image.ImageUrl);
        }

        protected void gridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            // ye event e too gridview e comment ha , too oonja in event tarif shode
            // in ezafas ! vali be khatere kamboode vaqt nemitonam baresh daram :| niaz be edit dare

            gridView1.EditIndex = e.NewEditIndex;
        }

        protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // inam ye event e va zamani be voqoo mipeyvande ke oon shomare ha ke oon payin hast bekhay beri safhe haye baadie comment ha
            // vaqti ye shomare ro click koni in event ejra mishe

            gridView1.PageIndex = e.NewPageIndex;
            gridView1.DataSource = repo.commentsGet(Request.Params["post_id"].ToString());
            gridView1.DataBind();
        }


        //  vase ijad e comment vaqti karbar roo submit click kard ke post bezare
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtName.Text != "" && txtMessage.Text != "") // age moshakhasatesh khali nabood
            {
                tbl_comments comment = new tbl_comments(); // ye instance az class e comment ha besaz (hamoon karte melie)
                // bad bia be qesmat haye mokhtalefesh meqdar bede 

                comment.confirmed = "";
                comment.datetime = DateTime.Now;
                comment.description = txtMessage.Text;
                comment.email = txtEmail.Text;

                var remoteIpAddress = Request.UserHostAddress; // inja miad IP e kasi ke comment gozashte ro ham migirie !

                comment.ip = remoteIpAddress.ToString();
                comment.id = comment.ip.Replace(".","") + DateAndTime.getID(); //ID e comment hast : ip e taraf + tarikhZaman
                // masalan 19216811201602252214

                comment.name = txtName.Text;
                comment.post_id = Request.Params["post_id"].ToString();

                // va .....

                // hala miad karte meli ro mide be method ta create kone
                repo.commentCreate(comment);

                // message neshoon mide to ye label
                lblMessage.Text = "your message has been sent. it will be shown after confirmation";

                txtEmail.Text = "";
                txtMessage.Text = "";
                txtName.Text = "";
            }
            else // age moshakhasati ke lazeme khali bood error neshoon mide
                lblMessage.Text = "Your name and Message are Important !";
        }
    }
}