from flask import Flask, request

app = Flask(__name__)

# Bad response code
error_400 = "Bad parameters or missing parameters", 400
error_500 = "Internal server error", 500

# Mandatory for good response
response_header = {"content-type": "application/json"}
response_body = {
    "ErrorNumber": 0,
    "ErrorMessage": "",
    "Value": "Dummy",
}

# Optional for params and good response
optional = {
    "ClientID": 0,
    "ClientTransactionID": 0,
    "ServerTransactionID": 0,
}

path = "/api/v1/switch/<int:device_number>/"

def check(var):
    if tipo == int:
        try:
            return int(var)
        except (KeyError, ValueError) as e:
            return None

    elif tipo == float:
        try:
            return float(var)
        except (KeyError, ValueError) as e:
            return None

    elif tipo == bool:
        try:
            if var.lower() == "true":
                return True
            elif var.lower() == "false":
                return False
            else:
                return None
        except (KeyError, ValueError) as e:
            return None

    else:
        try:
            return str(var)
        except (KeyError, ValueError) as e:
            return None

def check_param(param, tipo):
    return check(request.args[param], tipo)

def check_data(data, tipo):
    return check(api.payload[data], tipo)


def dummy(device_number):
    return "Not Implemented"


def canwrite(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = True # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def maxswitch(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = 1 # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def getswitch(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = True # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def getswitchname(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = "switch name" # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def getminswitchvalue(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = "switch name" # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def getswitchdescription(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = "switch description" # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def minswitchvalue(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = 0.001 # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def maxswitchvalue(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = 0.999 # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def getswitchvalue(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = 0.123 # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def switchstep(device_number):

    params = [check_param('Id', int)]
    if any(p is None for p in params):
        return error_400

    value = 0.001 # Perform get operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def setswitch(device_number):

    data = [check_data('Id', int),
            check_data('State', int)]
    if any(d is None for d in data):
        return error_400

    value = data[1] # Perform set operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def setswitchname(device_number):

    data = [check_data('Id', int),
            check_data('Name', int)]
    if any(d is None for d in data):
        return error_400

    value = data[1] # Perform set operation here
    response_body["Value"] = value

    return response_body, 200, response_header


def setswitchvalue(device_number):

    data = [check_data('Id', int),
            check_data('Value', int)]
    if any(d is None for d in data):
        return error_400

    value = data[1] # Perform set operation here
    response_body["Value"] = value

    return response_body, 200, response_header


# Common GET
app.add_url_rule(path+"connected", view_func=dummy)
app.add_url_rule(path+"description", view_func=dummy)
app.add_url_rule(path+"driverinfo", view_func=dummy)
app.add_url_rule(path+"driverversion", view_func=dummy)
app.add_url_rule(path+"interfaceversion", view_func=dummy)
app.add_url_rule(path+"name", view_func=dummy)
app.add_url_rule(path+"supportedactions", view_func=dummy)

# Common PUT
app.add_url_rule(path+"action", view_func=dummy, methods=["PUT"])
app.add_url_rule(path+"commandblind", view_func=dummy, methods=["PUT"])
app.add_url_rule(path+"commandbool", view_func=dummy, methods=["PUT"])
app.add_url_rule(path+"commandstring", view_func=dummy, methods=["PUT"])
app.add_url_rule(path+"connected", view_func=dummy, methods=["PUT"])

# Switch GET
app.add_url_rule(path+"maxswitch", view_func=maxswitch)
app.add_url_rule(path+"canwrite", view_func=canwrite)
app.add_url_rule(path+"getswitch", view_func=getswitch)
app.add_url_rule(path+"getswitchdescription", view_func=getswitchdescription)
app.add_url_rule(path+"getswitchname", view_func=getswitchname)
app.add_url_rule(path+"getswitchvalue", view_func=getswitchvalue)
app.add_url_rule(path+"minswitchvalue", view_func=minswitchvalue)
app.add_url_rule(path+"maxswitchvalue", view_func=maxswitchvalue)
app.add_url_rule(path+"switchstep", view_func=switchstep)

# Switch PUT
app.add_url_rule(path+"setswitch", view_func=setswitch, methods=["PUT"])
app.add_url_rule(path+"setswitchname", view_func=setswitchname, methods=["PUT"])
app.add_url_rule(path+"setswitchvalue", view_func=setswitchvalue, methods=["PUT"])


if __name__ == "__main__":
    app.run(debug=True)
