#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on: 16/01/2023
@author: Andoni Aranguren Ubierna
@updates: Sergio Campos 02-03/2023
"""

import pickle
import pysmile
import sys
import json
print(sys.path)
from src import constants, pysmile_license
from werkzeug.middleware.proxy_fix import ProxyFix


import mysql.connector
from flask import Flask, request, render_template

from src import configuration, recommender, database, planner, modal_choice

app = Flask(__name__)

model = pickle.load(open('model.pkcls', 'rb'))
"""
net = pysmile.Network()
"""

#CORS
from flask_cors import CORS
cors = CORS(app, resources={r"/*": {"origins": "*"}})

cnx = None


@app.route("/")
def index():
    return render_template("index.html")



# MODAL CHOICE: -------------------------------------------------------------------------------------------------------
@app.get("/modal_choice/getConfigurationFile")
def modal_choice_getconfigurationfile():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return modal_choice.modal_choice_getconfigurationfile(cnx, request)




@app.get("/modal_choice/estimation")
def modal_choice_estimation():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return modal_choice.modal_choice_estimation(cnx, request) 

@app.get("/modal_choice/getProbabilites")
def modal_choice_getprobabilities():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return modal_choice.modal_choice_getprobabilities(cnx, request)

@app.get("/modal_choice/setProbabilites")
def modal_choice_setprobabilities():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return modal_choice.modal_choice_setprobabilities(cnx, request)


@app.get("/modal_choice/getDependencies")
def modal_choice_getdependencies():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return modal_choice.modal_choice_getdependencies(cnx, request)


@app.get("/modal_choice/getValues")
def modal_choice_getvalues():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return modal_choice.modal_choice_getvalues(cnx, request)





#Dexi configuration: -------------------------------------------------------------------------------------------------
@app.get("/recommender/configuration/getMatrix")
def configuration_getmatrix():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return configuration.getmatrix(cnx, request) 


@app.get("/recommender/configuration/setMatrix")
def configuration_setmatrix():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return configuration.setmatrix(cnx, request)


@app.get("/recommender/configuration/getSliders")
def configuration_getsliders():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return configuration.getsliders(cnx, request)


@app.get("/recommender/configuration/setSliders")
def configuration_setsliders():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return configuration.setsliders(cnx, request)



@app.get("/recommender/configuration/getEvaluationFile")
def configuration_getevaluationfile():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return configuration.getevaluationfile(cnx, request)


@app.get("/recommender/configuration/getConfigurationFile")
def configuration_getconfigurationfile():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return configuration.getconfigurationfile(cnx, request)



# RECOMMENDERS: -------------------------------------------------------------------------------------------------------
@app.get("/recommender/popularity")
def recommender_popularity():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return recommender.recommender_popularity(cnx, request)


@app.get("/recommender/geographic/action_id")
def recommender_geographic_id():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return recommender.recommender_geographic_id(cnx, request)


@app.get("/recommender/geographic/lat_lon")
def recommender_geographic_lat_lon():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return recommender.recommender_geographic_lat_lon(cnx, request)


@app.get("/recommender/preferences")
def recommender_preferences():
    """
    We ask for kpis based on the affinity to the kpi provided.
    :return: json
    """
    return recommender.recommender_preferences(cnx, request)


@app.get("/recommender/kpis/kpi_id")
def recommender_kpis_kpi_id():
    """
    Provided a action it returns a set of kpis by their affinity regarding the provided action
    :return: json
    """
    return recommender.recommender_kpis_kpi_id(cnx, request)


@app.get("/recommender/kpis/action_id")
def recommender_kpis_action_id():
    """
    Provided a action it returns a set of kpis by their affinity regarding the provided action
    :return: json
    """
    return recommender.recommender_kpis_action_id(cnx, request)


@app.get("/recommender/bayesian")
def recommender_bayesian_action_id():
    """
    We ask for actions based on popularity of the actions
    :return: json
    """
    return recommender.recommender_bayesian_action_id(cnx, request)


# DATABASE SAVE: -------------------------------------------------------------------------------------------------------
@app.post("/database/save/plan")
def database_save_plan():
    """
    It stores the plan_details that have been selected.
    The json provided should have the same format that /planner/plan_detail outputs filled with each plan_detail
    :return: json with the plan_detail
    """
    if request.json:
        return database.database_save_plan(cnx, request)
    else:
        return constants.ERROR_JSON_NEEDED


# plan_detail PLANNER: -------------------------------------------------------------------------------------------------------
@app.get("/planner/plan_detail")
def planner_plan_detail():
    """
    It calculates the best plan_detail traversing streets and reordering the intermediate nodes provided.
    :return: json with the plan_detail
    """
    return planner.planner_plan_detail(cnx, request)

@app.get("/database/store/kpi")
def database_store_kpi():
    """
    The json provided should have the same format that json with the kpi
    """
    #if request.json:
    if request:
        return database.database_store_kpi(cnx, request)
    else:
        return constants.ERROR_JSON_NEEDED

@app.get("/database/store/kpi_relative")
def database_store_kpi_relative():
    """
    The json provided should have the same format that json with the kpi
    """
    #if request.json:
    if request:
        return database.database_store_kpi(cnx, request, relative=True)
    else:
        return constants.ERROR_JSON_NEEDED

@app.get("/database/store/kpi_area")
def database_store_kpi_area():
    """
    The json provided should have the same format that json with the kpi
    """
    #if request.json:
    if request:
        return database.database_store_kpi(cnx, request, area=True)
    else:
        return constants.ERROR_JSON_NEEDED

@app.get("/database/store/kpi_relative_area")
def database_store_kpi_relative_area():
    """
    The json provided should have the same format that json with the kpi
    """
    #if request.json:
    if request:
        return database.database_store_kpi_relative_area(cnx, request)
    else:
        return constants.ERROR_JSON_NEEDED

# MAIN: ----------------------------------------------------------------------------------------------------------------
if __name__ == "__main__":
    """model = pickle.load(open('model.pkcls', 'rb'))
    net = pysmile.Network()
    net.read_file("URBANITE_ModalChoice.xdsl");
    """
    #cnx = mysql.connector.connect(**constants.DDBB_CONFIG)
    user = 'root'
    passwd = 'admin'
    ip = 'mysql'
    bd = 'urbanite_recommender'
    cnx = "mysql+pymysql://%s:%s@%s/%s" % (user, passwd, ip, bd)
    # app.run(debug=True, host='0.0.0.0')
    # app.wsgi_app = ProxyFix( app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)