from tkinter import *
import sqlite3

root = Tk()
root.title("Challenge Response Scheme")
conn = sqlite3.connect("BB.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS bb3(id integer primary key autoincrement , email TEXT, password TEXT,credits integer , security_q TEXT, security_ans TEXT, sqltime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)")
conn.commit()
conn.close()


def forgot():
   def check():
       t0 = Toplevel()
       conn = sqlite3.connect("BB.db")
       c = conn.cursor()
       c.execute("SELECT * FROM bb3 WHERE email=?;", (e1.get(),))
       row=c.fetchone()
       print(row)
       if row==None:
           l=Label(t0, text="Invalid email, please start again").pack()
       else:
           c.execute("UPDATE bb3 SET password=? WHERE security_q=? AND security_ans=?;", (ee3.get(), ee1.get(), ee2.get(), ))
           l=Label(t0, text="Password Change Sucessfull").pack()
       conn.commit()
       r = c.fetchall()
       conn.close()


   t = Toplevel()
   l1= Label(t, text="Enter Security Question")
   l2=Label(t, text="Enter your answer")
   l3=Label(t, text="Enter new password")
   ee1=Entry(t, width=50)
   ee2=Entry(t, width=50)
   ee3=Entry(t, width=50)
   l1.grid(row=1, column=0)
   ee1.grid(row=1, column=2)
   l2.grid(row=2, column=0)
   ee2.grid(row=2, column=2)
   l3.grid(row=3, column=0)
   ee3.grid(row=3, column=2)
   b=Button(t, text="Continue", command=check)
   b.grid(row=4, column=2)


def save():
   tt = Toplevel()
   b = Button(tt, text="Data Saved!")
   conn = sqlite3.connect('BB.db')
   c = conn.cursor()
   c.execute('INSERT INTO bb3(email,password,credits, security_q, security_ans) VALUES (?,?,?,?,?)', (e11.get(), e22.get(), 50, e33.get(), e44.get()))
   conn.commit()
   b.grid(row=2, column=2)

myLabel1 = Label(root, text="Challenge Response Scheme")
name = Label(root, text="Email")
pas = Label(root, text="Password")
e1 = Entry(root, width=50)
e2 = Entry(root, width=50)
b = Button(root, text="Login")
myLabel1.grid(row=0, column=2)
name.grid(row=1, column=0)
pas.grid(row=2, column=0)
b.grid(row=3, column=2)
e1.grid(row=1, column=2)
e2.grid(row=2, column=2)
ll=Label(root, text="New User? Register Below")
ll.grid(row=4, column=2)
l1 = Label(root, text="Email")
l2 = Label(root, text="Password")
l3= Label(root, text="Security Question")
l4= Label(root, text="Answer")
b0 = Button(root, text="Save", command=save)
e11 = Entry(root, width=50)
e22 = Entry(root, width=50)
e33= Entry(root, width=50)
e44= Entry(root, width=50)
l1.grid(row=5, column=0)
l2.grid(row=6, column=0)
l3.grid(row=7, column=0)
l4.grid(row=8, column=0)
e11.grid(row=5, column=2)
e22.grid(row=6, column=2)
e33.grid(row=7, column=2)
e44.grid(row=8, column=2)
b0.grid(row=9, column=2)
b1= Button(root, text="Forgot Password?", command=forgot)
b1.grid(row= 12, column= 2)

root.mainloop()