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

namespace CarGallery
{
    public partial class AdminSettings : System.Web.UI.Page
    {
        DatabaseRepository repo;
        string sess;

        protected void Page_Load(object sender, EventArgs e)
        {

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

            sess = Session["username"].ToString();


            repo = new DatabaseRepository();

            if(!IsPostBack)
                txtName.Text = repo.getName(sess);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtNewPass.Text != "") // age textBox e NewPassword khali nabood yani karbar mikhad password ro avaz kone pas :
            {
                if (repo.isUserExist(sess, txtCurrentPass.Text)) // age password e feli ke vared karde sahih bood
                {
                    if (txtNewPass.Text.Length >= 5) // va age passwordi ke vared karde 5 character ya bishtar bood
                    {
                        if (txtNewPass.Text == txtNewPassConfirm.Text) // va hamchenin age password e jadid , ba confirm esh yeki bood
                        {
                            // bad bayad password ro taqir bedi. vali ma ye field e name ham darim oonam taqir mikone

                            if (txtName.Text != "") // age name khali nabood
                            {
                                repo.updateName(sess, txtName.Text); // name ro update kon

                                repo.updatePassword(sess, txtNewPass.Text); // password ro ham update kon

                                lblMsg.ForeColor = System.Drawing.Color.Green; // ye label to formemoon hast range matnesho sabz kon

                                lblMsg.Text = "Changes are saved."; // ino toosh benevis
                            }
                            else // age name khali bood
                            {
                                lblMsg.ForeColor = System.Drawing.Color.Red; // label ro qermez kon

                                lblMsg.Text = "Name is required !"; // ino neshoon bede
                            }

                        }
                        else // age new password va Confirm Password yeki nabood :
                        {
                            lblMsg.ForeColor = System.Drawing.Color.Red;

                            lblMsg.Text = "New password and Confirm Password are not mach !";
                        }

                    }
                    else // age password kamtar az 5 ta character bood :
                    {
                        lblMsg.ForeColor = System.Drawing.Color.Red;


                        lblMsg.Text = "Password must be more than 5 characters !";
                    }
                }
                else // age password e feli dorost nabood :
                {
                    lblMsg.ForeColor = System.Drawing.Color.Red;

                    lblMsg.Text = "Current Password is Wrong !";
                }
            }
            else if (txtName.Text != "") // age field e new password khali bood yani karbar nemikhad password ro change kone
                // pas faqat name ro change kon albate age name khali nabood
            {
                repo.updateName(sess, txtName.Text);

                lblMsg.Text = "Changes are saved.";
                lblMsg.ForeColor = System.Drawing.Color.Green;
            }
            else // age name khali bood error bede
            {
                lblMsg.Text = "Name is required !";

                lblMsg.ForeColor = System.Drawing.Color.Red;

                
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminSettings.aspx");
        }
    }
}