#!/usr/bin/python3

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

conn = sqlite3.connect(DBPath)
conn.text_factory = str
c = conn.cursor()

device = DeviceName

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

else:
    print("1.Save Tx Band")
    print("2.Save RX Band")
    print("3.Read Band")
    choice = int(input("select cgi Function===>"))
    if choice == 1:
        request_Type = 'save_tx_band'
    elif choice == 2:
        request_Type = 'save_rx_band'
    elif choice == 3:
        request_Type = 'read_mode_map'
    else:
        print("Enter Valid choice")
    print("Reques_Type="+str(request_Type))



def save_tx_band():
    cmd = "save_tx_band"
    cmd_id = get_ccm_command_id()
    if(GUI == true):
        value = form.getvalue("value")
        id = form.getvalue("id")
    else:
        value = form_getvalue("value")
        id = form_getvalue("id")


    if (CCM == true):
        message = "set,element=mode_map,args=6,uuid={0},action={1},value={2},id={3},device={4}"\
            .format(cmd_id, cmd, value, id, device)
        send_ccm_msg(message)
        response = get_ccm_response(cmd_id)
        print(response)
        delete_ccm_response(cmd_id)

    else:
        query = c.execute("update mode_map set tx_band = ? where id = ?", [value, id])
        conn.commit()
        print((json.JSONEncoder().encode(
            {"status": "success", "description": "Successfully Updated"})))

    c.close()
    conn.close()

def save_rx_band():
    cmd = "save_rx_band"
    cmd_id = get_ccm_command_id()
    if(GUI == true):
        value = form.getvalue("value")
        id = form.getvalue("id")
    else:
        value = form_getvalue("value")
        id = form_getvalue("id")


    if (CCM == true):
        message = "set,element=mode_map,args=6,uuid={0},action={1},value={2},id={3},device={4}"\
            .format(cmd_id, cmd, value, id, device)
        send_ccm_msg(message)
        response = get_ccm_response(cmd_id)
        print(response)
        delete_ccm_response(cmd_id)

    else:
        query = c.execute("update mode_map set rx_band = ? where id =?", [value,id])
        conn.commit()
        print((json.JSONEncoder().encode(
            {"status": "success", "description": "Successfully Updated"})))

    c.close()
    conn.close()

def set_enable_comb():
    cmd = "enable_comb"
    cmd_id = get_ccm_command_id()
    if(GUI == true):
        value = form.getvalue("value")
        id = form.getvalue("id")
    else:
        value = form_getvalue("value")
        id = form_getvalue("id")


    if (CCM == true):
        message = "set,element=mode_map,args=6,uuid={0},action={1},value={2},id={3},device={4}"\
            .format(cmd_id, cmd, value, id, device)
        send_ccm_msg(message)
        response = get_ccm_response(cmd_id)
        print(response)
        delete_ccm_response(cmd_id)

    else:
        query = c.execute("update mode_map set enable_comb = ? where id = ?", [value, id])
        conn.commit()
        print((json.JSONEncoder().encode(
            {"status": "success", "description": "Successfully Updated"})))

    c.close()
    conn.close()

def read_mode_map():

    query = c.execute("SELECT id,tx_band,rx_band,enable_comb,warmup_time FROM mode_map")
    rows = query.fetchone()
    
    data = { "id" : rows[0], "tx_band" : rows[1], "rx_band" : rows[2], "enable_comb" : rows[3], "warmup_time" : rows[4]}
        
    print((json.JSONEncoder().encode(data)))

    c.close()
    conn.close()




print("Content-type:text/html\r\n\r\n")
if(request_Type == 'save_tx_band'):
    save_tx_band()
elif(request_Type == 'save_rx_band'):
    save_rx_band()
elif(request_Type == 'set_enable_comb'):
    set_enable_comb()
else:
    read_mode_map()
