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 Login : System.Web.UI.Page
    {
        DatabaseRepository repo;
        protected void Page_Load(object sender, EventArgs e)
        {
            repo = new DatabaseRepository();

            var sess = Session["username"];

            // ma ye mafhoomi darim be esme session , ke to system e karbar ye etela ati ro save mikone baraye ye modate kootahi
            // az oon etela at mitoonim baraye etebar sanji estefade konim bebinim karbar login karde ya na masalan

            // miaym migim age session i be esme username to system e karbar bood va meqdar dasht
            // yani karbar login karde pas nabayad safhe login ro neshoon bede , bargard be safhe asli !
            
            if (sess != null && sess != "")
            {
                Response.Redirect("Default.aspx");
            }
        }


        protected void btnLogin_Click(object sender, EventArgs e)
        {
            // bia check kon bebin age karbar ro dokme login click kard etela atesh doroste ya na
            // qablan tozih dadam dar morede method e isUserExist
            
            if (repo.isUserExist(txtUsername.Text, txtPassword.Text)) 
            {// age dorost bood va karbari ba moshakhasate vared shode vojood dasht
                Session["username"] = txtUsername.Text; // ye session barash besaz va bargard be safhe asli
                Response.Redirect("Default.aspx");
            }
            else // age dorost nabood error bede
                lblMessage.Text = "Incorrect username or password";
        }
    }
}