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

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user_id"] != null)
        {
            Response.Redirect("/index.aspx");
            return;
        }
    }

    protected void onLoginClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Entities entities = new Entities();
            User user = (from u in entities.Users where u.username == username_box.Text select u).SingleOrDefault();

            if (user == null)
            {
                error_message.Text = "El usuario o contraseña no son validos";
                return;
            }

            if (!PasswordHash.Compare(password_box.Text, user.salt, user.password))
            {
                error_message.Text = "El usuario o contraseña no son validos";
                return;
            }

            Session["user_id"] = user.id;
            Response.Redirect("/index.aspx");
        }
    }

    protected void validateUsername(object sender, ServerValidateEventArgs e)
    {
        e.IsValid = true;
    }

    protected void validatePassword(object sender, ServerValidateEventArgs e)
    {
        e.IsValid = true;
    }
}