from flask import Flask
from flasgger import Swagger

# from . import properties
from formats.message_wrapper import message_wrapper_schema
from formats.mission_plan import mission_plan_schema
from formats.observation import observation_schema
from formats.planning_configuration import planning_configuration_schema
from formats.platform_status import platform_status_message_schema
from formats.acknowledgement import acknowledgement_schema

app = Flask(__name__)

swagger_config = {
    "headers": [],
    "openapi": "3.0.2",
    "swagger_ui": True,
    "specs_route": "/",
    "info": {
        "title": "Backbone Message Formats",
        "version": "0.1",
        "description": "SoAR message schemas (i.e. formats)",
    },
    "specs": [
        {
            "endpoint": "swagger",
            "route": "/swagger.json",
            "rule_filter": lambda rule: True,
            "model_filter": lambda tag: True,
        }
    ],
    "components": {
        "schemas": {
            "Message": message_wrapper_schema,
            "MissionPlan": mission_plan_schema,
            "Observation": observation_schema,
            "PlanningConfiguration": planning_configuration_schema,
            "PlatformStatus": platform_status_message_schema,
            "Acknowledgement": acknowledgement_schema,
        },
    },
    "paths": {
        "/all_messages": {
            "get": {
                "description": "Returns all messages from the system.",
                "responses": {
                    "200": {
                        "description": "A list of messages.",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "oneOf": [
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/Acknowledgement"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/PlatformStatus"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/MissionPlan"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/Observation"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/PlanningConfiguration"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/PlatformStatus"
                                        },
                                    ],
                                    "discriminator": {
                                        "propertyName": "message_type",
                                    },
                                }
                            }
                        },
                    }
                },
            }
        },
    },
    "produces": ["application/json"],
    "consumes": ["application/json"],
}
message_types = [
    # "Message",
    "Acknowledgement",
    "MissionPlan",
    "Observation",
    "PlanningConfiguration",
    "PlatformStatus",
]
for item in message_types:
    swagger_config["paths"]["/" + str(item.lower())] = {
        "get": {
            "description": "Returns message for " + item,
            "responses": {
                "200": {
                    "description": item + " message.",
                    "content": {
                        "application/json": {
                            "schema": {
                                "allOf": [
                                    {
                                        "$ref": "#/components/schemas"
                                        + "/" + item,
                                    },
                                ],
                                "discriminator": {
                                    "propertyName": "message_type",
                                },
                            }
                        }
                    },
                }
            },
        }
    }

swag = Swagger(app, config=swagger_config, merge=True)

# app.add_url_rule(
#     '/coordinates',
#     view_func=Coordinates.as_view('coordinates'),
#     methods=['GET']
# )

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