#!/usr/bin/python3

import json
import sqlite3
from db import *
from globalSettings import *
from global_func import *
import cgi
import cgitb
cgitb.enable()


if (GUI == true):
    form = cgi.FieldStorage()
    request_type = form.getvalue('requestType')

conn = sqlite3.connect(DBPath)

def restore_module():
    conn.text_factory = str
    c = conn.cursor()
    # Restore TEC
    c.execute("update TEC set set_temp = ?, set_on_off_state=?",[25,0])
    conn.commit()
    
    wave = 0
    #Restore Wavelength
    c.execute("update mode_map set set_wavelength = ?,set_power_state =?",[wave,0])
    conn.commit()
    
    c.close()
    conn.close()
    jsonSchema = {"status" : 200, "response" : "Default Value Reset Successfully"}
    print(json.JSONEncoder().encode(jsonSchema))

def restore_demo():
    jsonSchema = {'status' : 200, 'response' : "Default Value Reset Successfully!"}
    print(json.JSONEncoder().encode(jsonSchema))

def restore_vdac():
    jsonSchema = {'status' : 200, 'response' : "Default Value Reset Successfully!"}
    print(json.JSONEncoder().encode(jsonSchema))

def restore_idac():
    jsonSchema = {'status' : 200, 'response' : "Default Value Reset Successfully!"}
    print(json.JSONEncoder().encode(jsonSchema))

def restore_calibration():
    conn.text_factory = str
    c = conn.cursor()
    query = c.execute("update calibration set ring_stop_voltage_1 = ?,ring_increment_1 = ?,ring_stop_voltage_2 = ?,ring_increment_2 = ?,phase_stop_voltage = ?,phase_increment = ?,gain_start_current = ?,gain_stop_current = ?,start_temperature = ?,stop_temperature = ?,gain_inc = ?,temp_inc = ?,soa_stop = ?,soa_inc = ?",[10,0.1,10,0.1,10,2,100,100,25,25,10,1,100,1])
    conn.commit()
    c.close()
    conn.close()
    jsonSchema = {'status' : 200, 'response' : "Default Value Reset Successfully!"}
    print(json.JSONEncoder().encode(jsonSchema))

print("Content-type:/text/json\r\n\r\n")


if(request_type == 'restore_module'):
    restore_module()
elif(request_type == 'restore_demo'):
    restore_demo()
elif(request_type == 'restore_vdac'):
    restore_vdac()
elif(request_type == 'restore_idac'):
    restore_idac()
elif(request_type == 'restore_calibration'):
    restore_calibration()
else:
    print("Invalid Request Type")    


