/*
 * Copyright 2015-2017 Canoo Engineering AG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.canoo.platform.samples.distribution.client;

import com.canoo.platform.client.PlatformClient;
import com.canoo.platform.remoting.client.ClientContext;
import com.canoo.platform.remoting.client.ClientContextFactory;
import com.canoo.platform.remoting.client.javafx.FxToolkit;
import com.canoo.platform.samples.distribution.client.view.ToDoView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URISyntaxException;
import java.net.URL;

public class DistributionClient extends Application {

    private void connect(final Stage primaryStage, final URL endpoint) {
        final ClientContextFactory clientContextFactory = PlatformClient.getService(ClientContextFactory.class);
        try{


        final ClientContext clientContext = clientContextFactory.create(PlatformClient.getClientConfiguration(), endpoint.toURI());
        clientContext.connect().handle((v, e) -> {
            if (e != null) {
                e.printStackTrace();
                System.exit(-1);
            }
            Platform.runLater(() -> {
                try {
                    ToDoView viewController = new ToDoView(clientContext);
                    Scene scene = new Scene(viewController.getParent());
                    scene.getStylesheets().add(DistributionClient.class.getResource("style.css").toExternalForm());
                    primaryStage.setScene(scene);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    System.exit(-1);
                }
            });
            return null;
        });
        }catch(URISyntaxException ex){
            ex.printStackTrace();
        }
    }

    @Override
    public void start(final Stage primaryStage) throws Exception {
        FxToolkit.init();

        final Button server1Button = new Button("Server instance 1");
        final URL server1Url = new URL("http://localhost:8082/distribution-app/dolphin");
        server1Button.setOnAction(e -> connect(primaryStage, server1Url));

        final Button server2Button = new Button("Server instance 2");
        final URL server2Url = new URL("http://localhost:8083/distribution-app/dolphin");
        server2Button.setOnAction(e -> connect(primaryStage, server2Url));

        final VBox selectBox = new VBox(server1Button, server2Button);
        selectBox.setPadding(new Insets(24));
        selectBox.setSpacing(24);

        Scene scene = new Scene(selectBox);
        scene.getStylesheets().add(DistributionClient.class.getResource("style.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setOnCloseRequest(e -> System.exit(0));
        primaryStage.show();
    }


    public static void main(String[] args) {
        Application.launch(args);
    }
}

@@ -1212,44 +1212,8 @@
 .URI
-SyntaxException;%0Aimport java.net.URL
 ;%0A%0Ap
@@ -1325,17 +1325,17 @@
 final UR
-L
+I
  endpoin
@@ -1456,21 +1456,8 @@
 s);%0A
-        try%7B%0A
 %0A%0A  
@@ -1579,16 +1579,8 @@
 oint
-.toURI()
 );%0A 
@@ -2311,91 +2311,8 @@
 %7D);%0A
-        %7Dcatch(URISyntaxException ex)%7B%0A            ex.printStackTrace();%0A        %7D%0A
     
@@ -2504,25 +2504,25 @@
     final UR
-L
+I
  server1Url 
@@ -2521,33 +2521,33 @@
 ver1Url = new UR
-L
+I
 (%22http://localho
@@ -2744,17 +2744,17 @@
 final UR
-L
+I
  server2
@@ -2765,17 +2765,17 @@
 = new UR
-L
+I
 (%22http:/

Fix review comment