import sqlite3
from internet import check_internet_connection

def create_connection():
    connection = sqlite3.connect("memory.db")
    return connection
def get_questions_and_answer():
    con = create_connection()
    cur = con.cursor()
    cur.execute("SELECT * FROM questionsandanswer")
    return cur.fetchall()
def insert_question_and_answer(question, answer):
    con = create_connection()
    cur = con.cursor()
    query = "INSERT INTO questionsandanswer values('"+question+"', '"+answer+"')"
    cur.execute(query)
    con.commit()
    





def get_answer_from_memory(question):
    rows = get_questions_and_answer()
    answer = ""
    
    for row in rows:
        if row[0].lower() in question.lower():
            answer = row[1]
            break
    return answer

def get_name():
    con = create_connection()
    cur = con.cursor()
    query = "select value from memory where name = 'assistant_name'"
    cur.execute(query)
    return cur.fetchall()[0][0]
def update_name(new_name):
    con = create_connection()
    cur = con.cursor()
    query = "update memory set value = '"+new_name+"' where name = 'assistant_name'"
    cur.execute(query)
    con.commit()
def update_last_seen(last_seen_date):
    con = create_connection()
    cur = con.cursor()
    query = "update memory set value = '"+str(last_seen_date)+"' where name = 'last_seen_date'"
    cur.execute(query)
    con.commit()
def turn_on_speech():
    if (check_internet_connection):
        con = create_connection()
        cur = con.cursor()
        query = "update memory set value = 'on' where name = 'speech'"
        cur.execute(query)
        con.commit()
        return ("speech turn on")
    else:
        return ("please turn on the internet first")
def turn_off_speech():
    con = create_connection()
    cur = con.cursor()
    query = "update memory set value = 'off' where name = 'speech'"
    cur.execute(query)
    con.commit()
    return ("speech is off")
def speak_is_on():
    con = create_connection()
    cur = con.cursor()
    query = "select value from memory where name = 'speech'"
    cur.execute(query)
    ans = str(cur.fetchall()[0][0])
    if ans == "on":
        return True
    else:
        return False
def mic_is_on():
    con = create_connection()
    cur = con.cursor()
    query = "select value from memory where name = 'mic_module'"
    cur.execute(query)
    ans = str(cur.fetchall()[0][0])
    if ans == "on":
        return True
    else:
        return False
def turn_on_mic():
    if (check_internet_connection):
        con = create_connection()
        cur = con.cursor()
        query = "update memory set value = 'on' where name = 'mic'"
        cur.execute(query)
        con.commit()
        return ("mic turned on")
    else:
        return ("please turn on the internet first")    
def turn_off_mic():
    con = create_connection()
    cur = con.cursor()
    query = "update memory set value = 'off' where name = 'mic'"
    cur.execute(query)
    con.commit()
    return ("mic is turned off")
       

