import { ActionInfo } from "./common"

export const Permission = {
    Start: 1,
    Stop: 1 << 1,
    Browse: 1 << 2,
    Create: 1 << 3,
    Delete: 1 << 4,
    RunCommand: 1 << 5,
    Admin: 1 << 6,
}

const definitions = {
    ChangeUserRequest: {
        properties: {
            permissions: {
                items: {
                    "$ref": "#/definitions/Permission"
                },
                type: "array",
                title: "Permissions"
            }
        },
        type: "object",
        required: [
            "permissions"
        ],
        title: "ChangeUserRequest"
    },
    CreateUserRequest: {
        properties: {
            username: {
                type: "string",
                title: "Username"
            },
            password: {
                type: "string",
                title: "Password"
            }
        },
        type: "object",
        required: [
            "username",
            "password"
        ],
        title: "CreateUserRequest"
    },
    FileBrowserInfo: {
        properties: {
            id_: {
                type: "string",
                title: "Id "
            },
            owner_id: {
                type: "string",
                title: "Owner Id"
            },
            domain: {
                type: "string",
                title: "Domain"
            },
            connected_to: {
                "$ref": "#/definitions/ServerInfo"
            },
            url: {
                type: "string",
                title: "Url",
                readOnly: true
            }
        },
        type: "object",
        required: [
            "id_",
            "owner_id",
            "domain",
            "connected_to",
            "url"
        ],
        title: "FileBrowserInfo"
    },
    HTTPValidationError: {
        properties: {
            detail: {
                items: {
                    "$ref": "#/definitions/ValidationError"
                },
                type: "array",
                title: "Detail"
            }
        },
        type: "object",
        title: "HTTPValidationError"
    },
    ImageInfo: {
        properties: {
            name: {
                type: "string",
                title: "Name"
            },
            version: {
                type: "string",
                title: "Version"
            },
            ports: {
                items: {
                    "$ref": "#/definitions/Port-Output"
                },
                type: "array",
                uniqueItems: true,
                title: "Ports"
            },
            id_: {
                type: "string",
                title: "Id ",
                readOnly: true
            },
            display_name: {
                type: "string",
                title: "Display Name",
                readOnly: true
            }
        },
        type: "object",
        required: [
            "name",
            "version",
            "id_",
            "display_name"
        ],
        title: "ImageInfo"
    },
    PasswordRequestForm: {
        properties: {
            username: {
                type: "string",
                title: "Username"
            },
            password: {
                type: "string",
                title: "Password"
            },
            remember: {
                anyOf: [
                    {
                        type: "boolean"
                    },
                    {
                        type: "string"
                    }
                ],
                title: "Remember",
                default: false
            }
        },
        type: "object",
        required: [
            "username",
            "password"
        ],
        title: "PasswordRequestForm"
    },
    Permission: {
        type: "number",
        enum: [
            "create",
            "start",
            "browse",
            "cloud",
            "run-command",
            "delete",
            "stop",
            "admin"
        ],
        title: "Permission"
    },
    "Port-Input": {
        properties: {
            number: {
                type: "integer",
                exclusiveMaximum: 65535,
                exclusiveMinimum: 1,
                title: "Number"
            },
            protocol: {
                "$ref": "#/definitions/PortProtocol"
            }
        },
        type: "object",
        required: [
            "number",
            "protocol"
        ],
        title: "Port"
    },
    "Port-Output": {
        properties: {
            number: {
                type: "integer",
                exclusiveMaximum: 65535,
                exclusiveMinimum: 1,
                title: "Number"
            },
            protocol: {
                "$ref": "#/definitions/PortProtocol"
            },
            id_: {
                type: "string",
                title: "Id ",
                readOnly: true
            }
        },
        type: "object",
        required: [
            "number",
            "protocol",
            "id_"
        ],
        title: "Port"
    },
    PortMapping: {
        properties: {
            source_port: {
                "$ref": "#/definitions/Port-Input"
            },
            destination_port: {
                anyOf: [
                    {
                        "$ref": "#/definitions/Port-Input"
                    },
                    {
                        type: "null"
                    }
                ]
            }
        },
        type: "object",
        required: [
            "source_port"
        ],
        title: "PortMapping"
    },
    PortProtocol: {
        type: "string",
        enum: [
            "tcp",
            "udp"
        ],
        title: "PortProtocol"
    },
    ServerInfo: {
        properties: {
            id_: {
                type: "string",
                title: "Id "
            },
            user_id: {
                type: "string",
                title: "User Id"
            },
            image: {
                "$ref": "#/definitions/ImageInfo"
            },
            on: {
                type: "boolean",
                title: "On"
            },
            domain: {
                anyOf: [
                    {
                        type: "string"
                    },
                    {
                        type: "null"
                    }
                ],
                title: "Domain"
            },
            ports: {
                anyOf: [
                    {
                        items: {
                            "$ref": "#/definitions/Port-Output"
                        },
                        type: "array",
                        uniqueItems: true
                    },
                    {
                        type: "null"
                    }
                ],
                title: "Ports"
            },
            nickname: {
                anyOf: [
                    {
                        type: "string"
                    },
                    {
                        type: "null"
                    }
                ],
                title: "Nickname"
            }
        },
        type: "object",
        required: [
            "id_",
            "user_id",
            "image",
            "on"
        ],
        title: "ServerInfo"
    },
    SetServerNicknameRequest: {
        properties: {
            nickname: {
                type: "string",
                title: "Nickname"
            }
        },
        type: "object",
        required: [
            "nickname"
        ],
        title: "SetServerNicknameRequest"
    },
    Token: {
        properties: {
            access_token: {
                type: "string",
                title: "Access Token"
            },
            token_type: {
                type: "string",
                title: "Token Type"
            }
        },
        type: "object",
        required: [
            "access_token",
            "token_type"
        ],
        title: "Token"
    },
    UserView: {
        properties: {
            username: {
                type: "string",
                title: "Username"
            },
            email: {
                type: "string",
                title: "Email"
            },
            max_owned_servers: {
                type: "integer",
                title: "Max Owned Servers",
                default: 5
            },
            permissions: {
                items: {
                    "$ref": "#/definitions/Permission"
                },
                type: "array",
                title: "Permissions"
            }
        },
        type: "object",
        required: [
            "username",
            "email"
        ],
        title: "UserView"
    },
    ValidationError: {
        properties: {
            loc: {
                items: {
                    anyOf: [
                        {
                            type: "string"
                        },
                        {
                            type: "integer"
                        }
                    ]
                },
                type: "array",
                title: "Location"
            },
            msg: {
                type: "string",
                title: "Message"
            },
            type: {
                type: "string",
                title: "Error Type"
            }
        },
        type: "object",
        required: [
            "loc",
            "msg",
            "type"
        ],
        title: "ValidationError"
    }
}



export const SERVER_ACTIONS: ActionInfo[] = [
    {
        name: "Start",
        args: {
            properties: {
                ports: {
                    items: {
                        "$ref": "#/definitions/PortMapping"
                    },
                    type: "array",
                    title: "Ports",
                    default: []
                },
                command: {
                    anyOf: [
                        {
                            type: "string"
                        },
                        {
                            type: "null"
                        }
                    ],
                    title: "Command"
                }
            },
            type: "object",
            title: "Start Server",
            definitions: definitions,
        },
        requestType: "post",
        endpoint: "/servers/{server_id}/start",
        permissions: Permission.Start,
        response_action: "Ignore"
    },
    {
        name: "Stop",
        args: {
            title: "Stop",
            type: "object",
            required: [],
            properties: {},
            default: {}
        },
        requestType: "post",
        endpoint: "/servers/{server_id}/stop",
        permissions: Permission.Stop,
        response_action: "Ignore"
    },
    {
        name: "Delete Server",
        args: {
            title: "Delete Server",
            type: "object",
            required: [],
            properties: {},
            default: {}
        },
        requestType: "delete",
        endpoint: "/servers/{server_id}",
        permissions: Permission.Delete,
        response_action: "Ignore"
    },
    {
        name: "Run Command",
        args: {
            properties: {
                command: {
                    type: "string",
                    title: "Command"
                }
            },
            type: "object",
            required: [
                "command"
            ],
            title: "Run Command",
            definitions: definitions,
        },
        requestType: "post",
        endpoint: "/servers/{server_id}/run_command",
        permissions: Permission.RunCommand,
        response_action: "Ignore"
    },
    {
        name: "Browse",
        args: {
            title: "Browse",
            type: "object",
            required: [],
            properties: {},
            default: {}
        },
        requestType: "post",
        endpoint: "/servers/{server_id}/browse",
        permissions: Permission.Browse,
        response_action: "Browse"
    },
    {
        name: "Update Server",
        requestType: "patch",
        endpoint: "/servers/{server_id}",
        permissions: Permission.Admin,
        response_action: "Ignore",
        args: {
            title: "Update Server",
            type: "object",
            required: [],
            properties: {
                DefaultPorts: {
                    type: "array",
                    title: "Default Ports",
                    items: 
                        {
                            "$ref": "#/definitions/Port-Input"
                        }
                    
                },
                DefaultCommand: {
                    type: "string",
                    title: "Default Command"
                },
                Nickname: {
                    type: "string",
                    title: "Nickname"
                }
            },
            definitions: definitions,

        },
    },
    {
        name: "Add Permissions",
        requestType: "post",
        endpoint: "/servers/{server_id}/permissions",
        permissions: Permission.Admin,
        response_action: "Ignore",
        args: {
            properties: {
                Username: {
                    type: "string",
                    title: "Username",
                    fetch_display_path: "Username",
                    fetch_key_path: "Username",
                    fetch_url: "/users"
                },
                Permissions: {
                    type: "number",
                    title: "Permissions",
                    permissions: true,
                }
            },
            type: "object",
            required: [
                "Username"
            ],
            default: {
                Permissions: 0
            },
            title: "Set User Permissions",
            definitions: definitions,
        },
    }
]

export const CREATE_SERVER_ACTION: ActionInfo = {
    name: "Create Server",
    args: {
        properties: {
            ImageId: {
                type: "string",
                title: "Image Id",
                fetch_display_path: "DisplayName",
                fetch_key_path: "Id",
                fetch_url: "/images"
            },
            Nickname: {
                type: "string",
                title: "Server Nickname"
            },
            DefaultCommand: {
                type: "string",
                title: "Default Command"
            },
            DefaultPorts: {
                items: {
                    "$ref": "#/definitions/Port-Input"
                },
                type: "array",
                title: "Default Ports",
            }
        },
        type: "object",
        required: [
            "ImageId"
        ],
        title: "Create Server",

        definitions: definitions,
    },
    requestType: "post",
    endpoint: "/servers",
    permissions: Permission.Create,
    response_action: "Ignore"
}

export const INVITE_USER_ACTION: ActionInfo = {
    name: "Invite User",
    args: {
        properties: {
            email: {
                type: "string",
                title: "Email"
            },
            permissions: {
                type: "number",
                title: "Permissions",
                permissions: true,
            }
        },
        type: "object",
        required: [
            "email"
        ],
        title: "Invite User",
        definitions: definitions,
    },
    requestType: "post",
    endpoint: "/users",
    permissions: Permission.Admin,
    response_action: "Ignore"
}
export const USERS_ACTIONS: ActionInfo[] = [
    {
        name: "Delete User",
        args: {},
        requestType: "get",
        endpoint: "/users/{user_id}",
        permissions: Permission.Admin,
        response_action: "Ignore"
    },
    {
        name: "Change Permissions",
        args: {
            name: "Add Permissions",
            requestType: "post",
            endpoint: "/servers/{server_id}/permissions",
            permissions: Permission.Admin,
            response_action: "Ignore",
            args: {
                properties: {
                    Permissions: {
                        type: "number",
                        title: "Permissions",
                        permissions: true,
                    }
                },
                type: "object",
                required: [],
                default: {
                    Permissions: 0
                },
                title: "Set User Permissions",
                definitions: definitions,
            }
    },
        requestType: "patch",
        endpoint: "/users/{user_id}/permissions",
        permissions: Permission.Admin,
        response_action: "Ignore"
    },
]


export const BROWSER_ACTIONS: ActionInfo[] = [
    {
        name: "Delete Browser",
        requestType: "delete",
        endpoint: "/browsers/{browser_id}",
        args: {},
        permissions: Permission.Browse,
        response_action: "Ignore"
    }
]