// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.developer.devcenter;

import com.azure.core.util.Configuration;
import com.azure.core.util.BinaryData;
import com.azure.core.util.polling.SyncPoller;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.identity.DefaultAzureCredentialBuilder;

public final class ReadmeSamples {
    public void readmeSamples() {
        // BEGIN: com.azure.developer.devcenter.readme.devboxes
        // BEGIN: com.azure.developer.devcenter.readme.createDevCenterClient
        String endpoint = Configuration.getGlobalConfiguration().get("DEVCENTER_ENDPOINT");

        // Build our clients
        DevCenterClient devCenterClient =
                        new DevCenterClientBuilder()
                                .endpoint(endpoint)
                                .credential(new DefaultAzureCredentialBuilder().build())
                                .buildClient();
        // END: com.azure.developer.devcenter.readme.createDevCenterClient
        
        // BEGIN: com.azure.developer.devcenter.readme.createDevBoxClient
        DevBoxesClient devBoxClient =
                        new DevBoxesClientBuilder()
                                .endpoint(endpoint)
                                .credential(new DefaultAzureCredentialBuilder().build())
                                .buildClient();
        // END: com.azure.developer.devcenter.readme.createDevBoxClient
        
        // BEGIN: com.azure.developer.devcenter.readme.getProjectsAndPools
        // Find available Projects and Pools
        PagedIterable<BinaryData> projectListResponse = devCenterClient.listProjects(null);
        for (BinaryData p: projectListResponse) {
            System.out.println(p);
        }

        PagedIterable<BinaryData> poolListResponse = devBoxClient.listPools("myProject", null);
        for (BinaryData p: poolListResponse) {
            System.out.println(p);
        }
        // END: com.azure.developer.devcenter.readme.getProjectsAndPools
        
        // BEGIN: com.azure.developer.devcenter.readme.createAndConnectToDevBox
        // Provision a Dev Box
        BinaryData devBoxBody = BinaryData.fromString("{\"poolName\":\"MyPool\"}");
        SyncPoller<BinaryData, BinaryData> devBoxCreateResponse =
                devBoxClient.beginCreateDevBox("myProject", "me", "MyDevBox", devBoxBody, null);
        devBoxCreateResponse.waitForCompletion();

        
        Response<BinaryData> remoteConnectionResponse =
                        devBoxClient.getRemoteConnectionWithResponse("myProject", "me", "MyDevBox", null);
        System.out.println(remoteConnectionResponse.getValue());
        // END: com.azure.developer.devcenter.readme.createAndConnectToDevBox
        
        // BEGIN: com.azure.developer.devcenter.readme.deleteDevBox
        // Tear down the Dev Box when we're finished:
        SyncPoller<BinaryData, Void> devBoxDeleteResponse =
                        devBoxClient.beginDeleteDevBox("myProject", "me", "MyDevBox", null);
        devBoxDeleteResponse.waitForCompletion();        
        // END: com.azure.developer.devcenter.readme.deleteDevBox
        // END: com.azure.developer.devcenter.readme.devboxes

        // BEGIN: com.azure.developer.devcenter.readme.environments
        // BEGIN: com.azure.developer.devcenter.readme.createEnvironmentsClient
        EnvironmentsClient environmentsClient =
                        new EnvironmentsClientBuilder()
                                .endpoint(endpoint)
                                .credential(new DefaultAzureCredentialBuilder().build())
                                .buildClient();
        // END: com.azure.developer.devcenter.readme.createEnvironmentsClient
        
        // BEGIN: com.azure.developer.devcenter.readme.getCatalogItemsAndEnvironmentTypes
        // Fetch available catalog items and environment types
        PagedIterable<BinaryData> catalogItemListResponse = environmentsClient.listCatalogItems("myProject", null);
        for (BinaryData p: catalogItemListResponse) {
            System.out.println(p);
        }

        PagedIterable<BinaryData> environmentTypesListResponse = environmentsClient.listEnvironmentTypes("myProject", null);
        for (BinaryData p: environmentTypesListResponse) {
            System.out.println(p);
        }
        // END: com.azure.developer.devcenter.readme.getCatalogItemsAndEnvironmentTypes
        
        // BEGIN: com.azure.developer.devcenter.readme.createEnvironment
        // Create an environment
        BinaryData environmentBody = BinaryData.fromString("{\"catalogItemName\":\"MyCatalogItem\", \"environmentType\":\"MyEnvironmentType\"}");
        SyncPoller<BinaryData, BinaryData> environmentCreateResponse =
                environmentsClient.beginCreateOrUpdateEnvironment("myProject", "me", "TestEnvironment", environmentBody, null);
        environmentCreateResponse.waitForCompletion();
        // END: com.azure.developer.devcenter.readme.createEnvironment

        // BEGIN: com.azure.developer.devcenter.readme.deleteEnvironment
        // Delete the environment when we're finished:
        SyncPoller<BinaryData, Void> environmentDeleteResponse =
                        environmentsClient.beginDeleteEnvironment("myProject", "me", "TestEnvironment", null);
        environmentDeleteResponse.waitForCompletion();
        // END: com.azure.developer.devcenter.readme.deleteEnvironment
        // END: com.azure.developer.devcenter.readme.environments
    }
}
