from flask import Flask
from flasgger import Swagger

app = Flask(__name__)

swagger_config = {
    "headers": [],
    "openapi": "3.0.2",
    "swagger_ui": True,
    "specs_route": "/soardocs/",
    "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": {
                "type": "object",
                "required": ["message_type"],
                "properties": {
                    "message_type": {
                        "type": "string",
                    },
                    "source": {
                        "type": "string",
                        "description": "The sender.",
                        "example": "autonomy-engine",
                    },
                    "destination": {
                        "type": "string",
                        "description": "Publisher topic.",
                        "example": "soar.noc.autosub.ah1.status",
                    },
                    "delivery_type": {
                        "type": "string",
                        "description": "Published or broadcast",
                        "enum": ["broadcast", "publish"],
                        "example": "2.0.0",
                    },
                    "message_ID": {
                        "type": "string",
                        "description": "An identifier for the type of "
                        + "message received.",
                        "example": "PlatformStatus",
                    },
                },
                "discriminator": {
                    "propertyName": "message_type",
                },
            },
            "Coordinate": {
                "allOf": [{"$ref": "#/components/schemas/Message"}],
                "type": "object",
                "properties": {
                    "latitude": {
                        "type": "integer",
                        "description": "Latitude in decimal degrees.",
                        "example": 54.234,
                    },
                    "longitude": {
                        "type": "integer",
                        "description": "Longitude in decimal degrees.",
                        "example": -1.432,
                    },
                    "depth": {
                        "type": "integer",
                        "description": "Target depth",
                        "default": 0,
                        "example": 50,
                    },
                    "projection": {
                        "type": "integer",
                        "description": "EPSG Projection Code",
                        "example": 4326,
                        "default": 4326,
                    },
                },
            },
            "PlatformStatus": {
                "allOf": [
                    {"$ref": "#/components/schemas/Message"},
                ],
                "type": "object",
                "properties": {
                    "partner_ID": {
                        "type": "string",
                        "description": "An identifier for the partner "
                        + "owning/operating the platform.",
                        "example": "noc",
                    },
                    "platform_ID": {
                        "type": "string",
                        "description": "An identifier for the platform.",
                        "example": "noc_ah1",
                    },
                    "state": {
                        "type": "string",
                        "description": "Status of platform.",
                        "example": "idle",
                    },
                },
            },
        },
    },
    "paths": {
        "/messages": {
            "get": {
                "description": "Returns all messages from the system.",
                "responses": {
                    "200": {
                        "description": "A list of messages.",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "oneOf": [
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/Coordinate"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/PlatformStatus"
                                        },
                                    ],
                                    "discriminator": {
                                        "propertyName": "message_type",
                                    },
                                }
                            }
                        },
                    }
                },
            }
        },
        "/platformstatus": {
            "get": {
                "description": "Returns platform status message",
                "responses": {
                    "200": {
                        "description": "Platform status message.",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "allOf": [
                                        {
                                            "$ref": "#/components/schemas"
                                            + "/PlatformStatus"
                                        },
                                    ],
                                    "discriminator": {
                                        "propertyName": "message_type",
                                    },
                                }
                            }
                        },
                    }
                },
            }
        },
    },
    "produces": ["application/json"],
    "consumes": ["application/json"],
}

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)