// Copyright 2015-2016 Eivind Vegsundvåg
//
// 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 ninja.eivind.hotsreplayuploader.window;

import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import ninja.eivind.hotsreplayuploader.di.JavaFXController;
import ninja.eivind.hotsreplayuploader.models.Account;
import ninja.eivind.hotsreplayuploader.models.LeaderboardRanking;
import ninja.eivind.hotsreplayuploader.models.ReplayFile;
import ninja.eivind.hotsreplayuploader.models.stringconverters.AccountConverter;
import ninja.eivind.hotsreplayuploader.models.stringconverters.HeroConverter;
import ninja.eivind.hotsreplayuploader.models.stringconverters.StatusBinder;
import ninja.eivind.hotsreplayuploader.providers.hotslogs.HotSLogsHero;
import ninja.eivind.hotsreplayuploader.providers.hotslogs.HotsLogsProvider;
import ninja.eivind.hotsreplayuploader.scene.control.CustomListCellFactory;
import ninja.eivind.hotsreplayuploader.services.AccountService;
import ninja.eivind.hotsreplayuploader.services.HeroService;
import ninja.eivind.hotsreplayuploader.services.UploaderService;
import ninja.eivind.hotsreplayuploader.services.platform.PlatformService;
import ninja.eivind.hotsreplayuploader.utils.FXUtils;
import ninja.eivind.hotsreplayuploader.utils.ReplayFileComparator;
import ninja.eivind.hotsreplayuploader.utils.SimpleHttpClient;
import ninja.eivind.hotsreplayuploader.versions.GitHubRelease;
import ninja.eivind.hotsreplayuploader.versions.ReleaseManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * Window controller for the application. Contains references to all the components exposed to the user. Sets up events
 * and services to handle almost everything that happens in this application.
 */
public class HomeController implements JavaFXController {

    private static final Logger LOG = LoggerFactory.getLogger(HomeController.class);

    @FXML
    private VBox updatePane;
    @FXML
    private Label newVersionLabel;
    @FXML
    private Hyperlink updateLink;

    @FXML
    private ImageView logo;

    @Autowired
    private PlatformService platformService;
    @Autowired
    private ReleaseManager releaseManager;


    @Override
    public void afterPropertiesSet() {
        logo.setOnMouseClicked(event -> doOpenHotsLogs());

        checkNewVersion();
    }

    private void checkNewVersion() {
        final Task<Optional<GitHubRelease>> task = new Task<Optional<GitHubRelease>>() {
            @Override
            protected Optional<GitHubRelease> call() throws Exception {
                return releaseManager.getNewerVersionIfAny();
            }
        };
        task.setOnSucceeded(event -> task.getValue().
                ifPresent(this::displayUpdateMessage));
        new Thread(task).start();
    }

    private void displayUpdateMessage(final GitHubRelease newerVersionIfAny) {
        newVersionLabel.setText(newerVersionIfAny.getTagName());
        updateLink.setOnMouseClicked(value -> safeBrowse(newerVersionIfAny.getHtmlUrl()));
        updatePane.setVisible(true);
    }

    private void safeBrowse(final String url) {
        try {
            platformService.browse(SimpleHttpClient.encode(url));
        } catch (IOException e) {
            LOG.error("Could not open " + url + " in browser.", e);
        }
    }

    private void doOpenHotsLogs() {
        safeBrowse("https://www.hotslogs.com/Default");
    }

}

@@ -658,1387 +658,324 @@
 x.co
-llections.ListChangeListener;%0Aimport javafx.collections.ObservableList;%0Aimport javafx.concurrent.Task;%0Aimport javafx.fxml.FXML;%0Aimport javafx.scene.control.*;%0Aimport javafx.scene.image.ImageView;%0Aimport javafx.scene.input.KeyCode;%0Aimport javafx.scene.layout.VBox;%0Aimport javafx.scene.paint.Paint;%0Aimport ninja.eivind.hotsreplayuploader.di.JavaFXController;%0Aimport ninja.eivind.hotsreplayuploader.models.Account;%0Aimport ninja.eivind.hotsreplayuploader.models.LeaderboardRanking;%0Aimport ninja.eivind.hotsreplayuploader.models.ReplayFile;%0Aimport ninja.eivind.hotsreplayuploader.models.stringconverters.AccountConverter;%0Aimport ninja.eivind.hotsreplayuploader.models.stringconverters.HeroConverter;%0Aimport ninja.eivind.hotsreplayuploader.models.stringconverters.StatusBinder;%0Aimport ninja.eivind.hotsreplayuploader.providers.hotslogs.HotSLogsHero;%0Aimport ninja.eivind.hotsreplayuploader.providers.hotslogs.HotsLogsProvider;%0Aimport ninja.eivind.hotsreplayuploader.scene.control.CustomListCellFactory;%0Aimport ninja.eivind.hotsreplayuploader.services.AccountService;%0Aimport ninja.eivind.hotsreplayuploader.services.HeroService;%0Aimport ninja.eivind.hotsreplayuploader.services.UploaderService;%0Aimport ninja.eivind.hotsreplayuploader.services.platform.PlatformService;%0Aimport ninja.eivind.hotsreplayuploader.utils.FXUtils;%0Aimport ninja.eivind.hotsreplayuploader.utils.ReplayFileComparator
+ncurrent.Task;%0Aimport javafx.fxml.FXML;%0Aimport javafx.scene.control.Hyperlink;%0Aimport javafx.scene.control.Label;%0Aimport javafx.scene.image.ImageView;%0Aimport javafx.scene.layout.VBox;%0Aimport ninja.eivind.hotsreplayuploader.di.JavaFXController;%0Aimport ninja.eivind.hotsreplayuploader.services.platform.PlatformService
 ;%0Aim
@@ -1315,31 +1315,8 @@
 on;%0A
-import java.util.List;%0A
 impo

clean up