{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "groupLocation": {
            "defaultValue": "",
            "type": "string",
            "metadata": {
                "description": "Specifies the location of the Resource Group."
            }
        },
        "groupName": {
            "type": "string",
            "metadata": {
                "description": "Specifies the name of the Resource Group."
            }
        },
        "appId": {
            "type": "string",
            "metadata": {
                "description": "Active Directory App ID, set as MicrosoftAppId in the Web App's Application Settings."
            }
        },
        "appSecret": {
            "type": "string",
            "metadata": {
                "description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings."
            }
        },
        "botId": {
            "type": "string",
            "metadata": {
                "description": "The globally unique and immutable bot ID. Also used to configure the displayName of the bot, which is mutable."
            }
        },
        "botSku": {
            "defaultValue": "F0",
            "type": "string",
            "metadata": {
                "description": "The pricing tier of the Bot Service Registration. Acceptable values are F0 and S1."
            }
        },
        "newAppServicePlanName": {
            "defaultValue": "",
            "type": "string",
            "metadata": {
                "description": "The name of the App Service Plan."
            }
        },
        "newAppServicePlanSku": {
            "type": "object",
            "defaultValue": {
                "name": "P1v2",
                "tier": "PremiumV2",
                "size": "P1v2",
                "family": "Pv2",
                "capacity": 1
            },
            "metadata": {
                "description": "The SKU of the App Service Plan. Defaults to Standard values."
            }
        },
        "newAppServicePlanLocation": {
            "defaultValue": "",
            "type": "string",
            "metadata": {
                "description": "The location of the App Service Plan. Defaults to \"westus\"."
            }
        },
        "newWebAppName": {
            "type": "string",
            "defaultValue": "",
            "metadata": {
                "description": "The globally unique name of the Web App. Defaults to the value passed in for \"botId\"."
            }
        }
    },
    "variables": {
        "resourcesLocation": "[deployment().location]",
        "effectiveGroupLocation": "[if(empty(parameters('groupLocation')), variables('resourcesLocation'), parameters('groupLocation'))]",
        "effectivePlanLocation": "[if(empty(parameters('newAppServicePlanLocation')), variables('resourcesLocation'), parameters('newAppServicePlanLocation'))]",
        "appServicePlanName": "[if(empty(parameters('newAppServicePlanName')), concat(parameters('botId'), 'ServicePlan'), parameters('newAppServicePlanName'))]",
        "webAppName": "[if(empty(parameters('newWebAppName')), parameters('botId'), parameters('newWebAppName'))]",
        "siteHost": "[concat(variables('webAppName'), '.azurewebsites.net')]",
        "botEndpoint": "[concat('https://', variables('siteHost'), '/api/messages')]"
    },
    "resources": [
        {
            "name": "[parameters('groupName')]",
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[variables('effectiveGroupLocation')]",
            "properties": {
            }
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2018-05-01",
            "name": "storageDeployment",
            "resourceGroup": "[parameters('groupName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "comments": "Create a new App Service Plan",
                            "type": "Microsoft.Web/serverfarms",
                            "name": "[variables('appServicePlanName')]",
                            "apiVersion": "2018-02-01",
                            "location": "[variables('effectivePlanLocation')]",
                            "sku": "[parameters('newAppServicePlanSku')]",
                            "kind": "linux",
                            "properties": {
                                "name": "[variables('appServicePlanName')]",
                                "reserved":true
                            }
                        },
                        {
                            "comments": "Create a Web App using the new App Service Plan",
                            "type": "Microsoft.Web/sites",
                            "apiVersion": "2015-08-01",
                            "location": "[variables('resourcesLocation')]",
                            "kind": "app",
                            "dependsOn": [
                                "[resourceId('Microsoft.Web/serverfarms/', variables('appServicePlanName'))]"
                            ],
                            "name": "[variables('webAppName')]",
                            "properties": {
                                "name": "[variables('webAppName')]",
                                "serverFarmId": "[variables('appServicePlanName')]",
                                "siteConfig": {
                                    "appSettings": [
                                        {
                                            "name": "JAVA_OPTS",
                                            "value": "-Dserver.port=80"
                                        },
                                        {
                                            "name": "MicrosoftAppId",
                                            "value": "[parameters('appId')]"
                                        },
                                        {
                                            "name": "MicrosoftAppPassword",
                                            "value": "[parameters('appSecret')]"
                                        }
                                    ],
                                    "cors": {
                                        "allowedOrigins": [
                                            "https://botservice.hosting.portal.azure.net",
                                            "https://hosting.onecloud.azure-test.net/"
                                        ]
                                    }
                                }
                            }
                        },
                        {
                            "apiVersion": "2017-12-01",
                            "type": "Microsoft.BotService/botServices",
                            "name": "[parameters('botId')]",
                            "location": "global",
                            "kind": "bot",
                            "sku": {
                                "name": "[parameters('botSku')]"
                            },
                            "properties": {
                                "name": "[parameters('botId')]",
                                "displayName": "[parameters('botId')]",
                                "endpoint": "[variables('botEndpoint')]",
                                "msaAppId": "[parameters('appId')]",
                                "developerAppInsightsApplicationId": null,
                                "developerAppInsightKey": null,
                                "publishingCredentials": null,
                                "storageResourceId": null
                            },
                            "dependsOn": [
                                "[resourceId('Microsoft.Web/sites/', variables('webAppName'))]"
                            ]
                        }
                    ],
                    "outputs": {}
                }
            }
        }
    ]
}