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

namespace TheGiftExchange {
    public partial class forgotpassword : System.Web.UI.Page {
        string email = null;
        string token = null;

        protected void Page_Load(object sender, EventArgs e) {
            email = Request["email"];
            token = Request["token"];

            if (email != null && token != null) {
                if (functions.verifyTokenInformation(email, token)) {
                    pnlEmailPassword.Visible = false;
                    pnlPasswordReset.Visible = true;
                } else {
                    pnlEmailPassword.Visible = true;
                    pnlPasswordReset.Visible = false;
                }
            } else {
                pnlEmailPassword.Visible = true;
                pnlPasswordReset.Visible = false;
            }
        }

        protected void btnSend_Click(object sender, EventArgs e) {
            if (functions.insertToken(txtEmail.Text, functions.generateToken())) {
                lblWarning2.Text = "Email with reset instructions has been sent.";
            } else {
                lblWarning2.Text = "An error occurred. Please contact website administrator for assistance.";
            }
        }

        protected void btnReset_Click(object sender, EventArgs e) {
            if (txtPassword.Text == txtConfPass.Text) {
                if (functions.resetForgottenPassword(email, token, txtPassword.Text)) {
                    Response.Redirect("index.aspx");
                } else { 
                    lblWarning.Text = "Could not reset password. Please contact website administrator for assistance.";
                }
            } else {
                lblWarning.Text = "Both passwords must match.";
            }
        }
    }
}