{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "siteName": {
            "type": "string",
            "defaultValue": "GameServerWatcher",
            "metadata": {
                "description": "The name / subdomain of the web app that you wish to create. (alphanumeric only!)"
            }
        },
        "siteLocation": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "The location to use for creating the web app and hosting plan. It must be one of the Azure locations that support web apps."
            }
        },
        "secret": {
            "type": "string",
            "metadata": {
                "description": "Admin secret"
            }
        },
        "refreshTimeMinutes": {
            "type": "int",
            "defaultValue": 2,
            "minValue": 1,
            "maxValue": 60,
            "metadata": {
                "description": "Game server info refresh interval in minutes"
            }
        },
        "discordBotToken": {
            "type": "string",
            "defaultValue": "-",
            "metadata": {
                "description": "Discord bot token"
            }
        },
        "telegramBotToken": {
            "type": "string",
            "defaultValue": "-",
            "metadata": {
                "description": "Telegram bot token"
            }
        },
        "slackBotToken": {
            "type": "string",
            "defaultValue": "-",
            "metadata": {
                "description": "Slack bot token"
            }
        },
        "slackAppToken": {
            "type": "string",
            "defaultValue": "-",
            "metadata": {
                "description": "Slack app token"
            }
        },
        "steamWebApiKey": {
            "type": "string",
            "defaultValue": "-",
            "metadata": {
                "description": "Steam web API key"
            }
        },
        "repoUrl": {
            "type": "string",
            "defaultValue": "https://github.com/a-sync/game-server-watcher.git",
            "metadata": {
                "description": "Source repo URL."
            }
        },
        "branch": {
            "type": "string",
            "defaultValue": "master",
            "metadata": {
                "description": "Source repo branch."
            }
        }
    },
    "variables": {
        "sku": "Basic",
        "skuCode": "B1",
        "linuxFxVersion": "NODE|16-lts",
        "hostingPlanName": "[format('AppServicePlan-{0}', parameters('siteName'))]"
    },
    "resources": [
        {
            "apiVersion": "2021-02-01",
            "name": "[variables('hostingPlanName')]",
            "type": "Microsoft.Web/serverfarms",
            "location": "[parameters('siteLocation')]",
            "sku": {
                "name": "[variables('skuCode')]",
                "tier": "[variables('sku')]"
            },
            "kind": "linux",
            "properties": {
                "name": "[variables('hostingPlanName')]",
                "reserved": true
            }
        },
        {
            "apiVersion": "2021-02-01",
            "name": "[parameters('siteName')]",
            "type": "Microsoft.Web/sites",
            "location": "[parameters('siteLocation')]",
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
            ],
            "kind": "app,linux",
            "properties": {
                "serverFarmId": "[variables('hostingPlanName')]",
                "siteConfig": {
                    "linuxFxVersion": "[variables('linuxFxVersion')]",
                    "AlwaysOn": true
                }
            },
            "resources": [
                {
                    "apiVersion": "2021-02-01",
                    "name": "web",
                    "type": "config",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/Sites', parameters('siteName'))]"
                    ],
                    "properties": {
                        "siteProperties": {
                            "webSocketsEnabled": true
                        }
                    }
                },
                {
                    "apiVersion": "2021-02-01",
                    "name": "appsettings",
                    "type": "config",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/Sites', parameters('siteName'))]"
                    ],
                    "properties": {
                        "SECRET": "[parameters('secret')]",
                        "REFRESH_TIME_MINUTES": "[parameters('refreshTimeMinutes')]",
                        "DISCORD_BOT_TOKEN": "[if(equals('-', parameters('discordBotToken')), '', parameters('discordBotToken'))]",
                        "TELEGRAM_BOT_TOKEN": "[if(equals('-', parameters('telegramBotToken')), '', parameters('telegramBotToken'))]",
                        "SLACK_BOT_TOKEN": "[if(equals('-', parameters('slackBotToken')), '', parameters('slackBotToken'))]",
                        "SLACK_APP_TOKEN": "[if(equals('-', parameters('slackAppToken')), '', parameters('slackAppToken'))]",
                        "STEAM_WEB_API_KEY": "[if(equals('-', parameters('steamWebApiKey')), '', parameters('steamWebApiKey'))]"
                    }
                },
                {
                    "condition": "[contains(parameters('repoUrl'), 'http')]",
                    "apiVersion": "2021-02-01",
                    "name": "web",
                    "type": "sourcecontrols",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/Sites', parameters('siteName'))]",
                        "[concat('Microsoft.Web/Sites/', parameters('siteName'), '/config/web')]"
                    ],
                    "properties": {
                        "RepoUrl": "[parameters('repoUrl')]",
                        "branch": "[parameters('branch')]",
                        "IsManualIntegration": true
                    }
                }
            ]
        }
    ],
    "outputs": {
        "siteUri": {
            "type": "string",
            "value": "[concat('https://',reference(resourceId('Microsoft.Web/sites', parameters('siteName'))).hostNames[0])]"
        }
    }
}
