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

namespace CarGallery
{
    public partial class AdminDeletePost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null || Session["username"].ToString() == "")
                Response.Redirect("Login.aspx");

            if(Session["post_id"] == null || Session["post_id"].ToString() == "")
                Response.Redirect("AdminManagePosts.aspx");

            
            lblQuestion.Text = "Are you sure you want to Delete post : " + Session["post_id"].ToString() + " ?";
        }

        protected void btnNo_Click(object sender, EventArgs e)
        {
            Session["post_id"] = null;
            Response.Redirect("AdminManagePosts.aspx");
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            // dar morede Delete Post ham sohbat kardam. 
            // faqat oon qabli ke ina ro dasht , vase modiriate dasteyie post hast.
            // ino vase inke ro errori ke mide yekam control dashte bashim intori neveshtam

            DatabaseRepository repo = new DatabaseRepository();
            string imgName = repo.postGetSingle(Session["post_id"].ToString()).Rows[0][3].ToString();

            bool slide = false, photo = false, thumb = false;

            try
            {
                File.Move(Server.MapPath("/") + "photos\\" + imgName, Server.MapPath("/") +  "photos\\archived\\" + imgName);
                photo = true;

            }
            catch { };

            try
            {
                File.Move(Server.MapPath("/") + "slides\\" + imgName, Server.MapPath("/") + "slides\\archived\\" + imgName);
                slide = true;

            }
            catch { };


            try
            {
                File.Move(Server.MapPath("/") + "thumbs\\" + imgName, Server.MapPath("/") + "thumbs\\archived\\" + imgName);
                thumb = true;

            }
            catch { };

            if (photo && slide && thumb)
            {
                File.Delete(Server.MapPath("/") + "photos\\archived\\" + imgName);
                File.Delete(Server.MapPath("/") + "slides\\archived\\" + imgName);
                File.Delete(Server.MapPath("/") + "thumbs\\archived\\" + imgName);

                repo.postDelete(Session["post_id"].ToString());
                Response.Redirect("AdminManagePosts.aspx");
            }
            else
            {
                lblMessage.Text = "ERROR ! One or more file(s) are locked ! Please try again later.";

                try
                {
                    File.Move("photos\\archived\\" + imgName, "photos\\" + imgName);
                }
                catch { };

                try
                {
                    File.Move("slides\\archived\\" + imgName, "slides\\" + imgName);
                }
                catch { };

                try
                {
                    File.Move("thumbs\\archived\\" + imgName, "thumbs\\]" + imgName);
                }
                catch { };
            }
        }
    }
}