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 AdminCreatePost : System.Web.UI.Page
    {
        DatabaseRepository repo;
        tbl_posts post;
        string sess;

        protected void Page_Load(object sender, EventArgs e)
        {
            // inja safheye ijade post e jadid hastesh

            repo = new DatabaseRepository();

            // hamoontor ke goftim aval check mikonim bebinim karbar login karde ya na
            if (Session["username"] == null || Session["username"].ToString() == "")
                Response.Redirect("Login.aspx");

            sess = Session["username"].ToString();
            // chon ba username e kasi ke login karde kar darim va bayad sabt beshe to database
            // vase rahatie kar too ye moteqayer qararesh midim
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            // age ro dokmeye create click shod

            // yekam bala tar beri mibini ma ye instance az class e tbl_posts sakhtim 
            // vali inja behesh meqdar dadim, mitoonestim oonja meqdar bedim behesh vali
            // behtare ke inja bedim

            post = new tbl_posts();


            // aval bia ax ro upload kon to ye pooshe be esme tmp (chon ke bayad ax resize beshe aval to ye ja be tore
            // movaqat uploadesh mikonim badan pakesh mikonim)

            //Server.MapPath("/") address e server ro barmigardoone
            
            fileUpload1.PostedFile.SaveAs(Server.MapPath("/") + "/tmp/" + fileUpload1.FileName);

            // esme ax ha ro Hash code e axi ke upload kardim dar nazar gereftim , in baes mishe axa esmeshoon yeksan nabashe
            // esme ax = hash code e file + pasvande file
            string fileName = ImageProcessor.getFileHash(Server.MapPath("/") + "/tmp/" + fileUpload1.FileName) + Path.GetExtension(fileUpload1.FileName);

            // bad ke didi moshkeli nist , ye noskhe az ax ro too poosheye photos copy kon
            File.Copy(Server.MapPath("/") + "tmp\\" + fileUpload1.FileName, Server.MapPath("/") + "photos\\" + fileName);


            // inja miad thumbnail (tasvire band angoshti 200x200)
            // vase in thumbnail misazim ke axa hajmeshoon kamtar mishe va koochiktar mishan vase 
            // jahayi mesle manage Posts va gallery ke koli ax bayad baz beshe 
            // thumbnail ro misazim va to folder e thumbs save mikonim

            ImageProcessor.Resize(File.OpenRead(Server.MapPath("/") + "photos\\" + fileName), 200, 200, Server.MapPath("/") + "thumbs\\" + fileName);

            // bebin , axe slide ro khode karbar mitoone upload kone , age khode karbar upload kone ke hich
            // dar qeyre in soorat , bayad axe asli i ke upload karde ro resize konim baraye estefad be onvane slide

            // momkene karbar axe slide ro khodesh upload karde bashe pas check mikonim
            if (fileUpload2.FileName != "" && fileUpload2.FileName != null) // age khodesh upload karde bood
            {
                fileUpload2.PostedFile.SaveAs(Server.MapPath("/") + "/tmp/" + fileUpload2.FileName); // too poosheye tmp savesh kon

                // bad bia resizesh kon be 900x450 va to folder e slides savesh kon
                ImageProcessor.Resize(File.OpenRead(Server.MapPath("/") + "tmp\\" + fileUpload1.FileName), 900, 450, Server.MapPath("/") + "slides\\" + fileName);

                // bad bia file e ke to tmp save kardi ro pak kon
                File.Delete(Server.MapPath("/") + "tmp//" + fileName);
            }
            else // age tasvire slide ro khodesh upload nakarde bood
                ImageProcessor.Resize(File.OpenRead(Server.MapPath("/") + "photos\\" + fileName), 900, 450, Server.MapPath("/") + "slides\\" + fileName);
            // bia tasvire asli ro resize kon be 900x450 bad to slides copy kon
            
            // moshakhaste dige ro ham be karte meli midim :

            post.datetime = DateTime.Now;


            post.description = txtDescription.Text;

            post.id = sess + DateAndTime.getID(); // to documentation goftam ID e ax mishe username e admin + tarikh va zamane feli

            post.img = fileName;
            

            // 2 ta RadioButton darim baraye inke moshakhas konim Slide fa ale ya na
            if(rbtnSlideTrue.Checked) //  age slide fa al bood
                post.slide = true;
            else
                post.slide = false;

            // hamchenin 2 ta Radio button darim baraye ..............
            if (rbtnCommentsTrue.Checked)
                post.comment = true;
            else
                post.comment = false;

            post.title = txtTitle.Text;
            post.username = sess;
            post.visits = 0;


            // bad bia kart e meli i ke ijad kardimo bede be method e ijade post ke sabtesh kone
            repo.postsCreate(post);
        }
    }
}