// Path: src/main/java/ninja/eivind/hotsreplayuploader/concurrent/tasks/HeroListTask.java
// public class HeroListTask extends Task<List<HotSLogsHero>> {
//     public static final String API_ROUTE = "https://www.hotslogs.com/API/Data/Heroes";
//     private final SimpleHttpClient httpClient;
// 
//     public HeroListTask(SimpleHttpClient httpClient) {
//         this.httpClient = httpClient;
//     }
// 
//     @Override
//     protected List<HotSLogsHero> call() throws Exception {
//         final String result = httpClient.simpleRequest(API_ROUTE);
//         final HotSLogsHero[] heroes = new ObjectMapper().readValue(result, HotSLogsHero[].class);
//         final List<HotSLogsHero> heroList = Arrays.asList(heroes);
//         heroList.sort(Comparator.comparing(HotSLogsHero::getPrimaryName));
//         return heroList;
//     }
// }
// 
// Path: src/main/java/ninja/eivind/hotsreplayuploader/providers/hotslogs/HotSLogsHero.java
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class HotSLogsHero implements Hero {
// 
//     @JsonProperty("PrimaryName")
//     private String primaryName;
//     @JsonProperty("ImageURL")
//     private String imageURL;
// 
//     public HotSLogsHero() {
//     }
// 
//     public HotSLogsHero(final String primaryName, final String imageURL) {
//         this.primaryName = primaryName;
//         this.imageURL = imageURL;
//     }
// 
//     @Override
//     public String toString() {
//         return primaryName;
//     }
// 
//     @Override
//     public boolean equals(final Object o) {
//         if (this == o) return true;
//         if (o == null || getClass() != o.getClass()) return false;
// 
//         final HotSLogsHero hero = (HotSLogsHero) o;
// 
//         return !(primaryName != null ? !primaryName.equals(hero.primaryName) : hero.primaryName != null)
//                 && !(imageURL != null ? !imageURL.equals(hero.imageURL) : hero.imageURL != null);
// 
//     }
// 
//     @Override
//     public int hashCode() {
//         int result = primaryName != null ? primaryName.hashCode() : 0;
//         result = 31 * result + (imageURL != null ? imageURL.hashCode() : 0);
//         return result;
//     }
// 
//     @Override
//     public String getPrimaryName() {
//         return primaryName;
//     }
// 
//     @Override
//     public void setPrimaryName(final String primaryName) {
//         this.primaryName = primaryName;
//     }
// 
//     @Override
//     public String getImageURL() {
//         return imageURL;
//     }
// 
//     @Override
//     public void setImageURL(final String imageURL) {
//         this.imageURL = imageURL;
//     }
// 
// }
// 
// Path: src/main/java/ninja/eivind/hotsreplayuploader/utils/SimpleHttpClient.java
// public class SimpleHttpClient {
// 
//     public static final String SPACE = "%20";
//     private static final Logger LOG = LoggerFactory.getLogger(SimpleHttpClient.class);
// 
//     /**
//      * Encodes a given String url into a valid URI if possible
//      * Method should be updated as required to make sure any valid String url that can be pasted into an address bar
//      * will fit into a URI object
//      *
//      * @param url the url to encode
//      * @return a newly built URI if at all possible
//      */
//     public static URI encode(final String url) {
//         return URI.create(url.replaceAll(" ", SPACE));
//     }
// 
//     public String simpleRequest(final String target) throws IOException {
//         return simpleRequest(encode(target));
//     }
// 
//     public String simpleRequest(final URI uri) throws IOException {
//         return simpleRequest(uri.toURL());
//     }
// 
//     public String simpleRequest(final URL url) throws IOException {
//         LOG.info("Opening connection to " + url);
//         final InputStream inputStream = url.openStream();
//         return IOUtils.readInputStream(inputStream);
//     }
// }


import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.util.Duration;
import ninja.eivind.hotsreplayuploader.concurrent.tasks.HeroListTask;
import ninja.eivind.hotsreplayuploader.providers.hotslogs.HotSLogsHero;
import ninja.eivind.hotsreplayuploader.utils.SimpleHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
// Copyright 2015 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.services;



/**
 * {@link ScheduledService} for checking the current list of available {@link HotSLogsHero}es periodically.
 */
@Component
public class HeroService extends ScheduledService<List<HotSLogsHero>> {

    private static final Logger LOG = LoggerFactory.getLogger(HeroService.class);
    @Autowired
    private SimpleHttpClient httpClient;

    public HeroService() {
        LOG.info("Instantiating " + getClass().getSimpleName());
        setPeriod(Duration.hours(2));
        setBackoffStrategy(param -> param.getPeriod().multiply(1.25));
        LOG.info("Instantiated " + getClass().getSimpleName());
    }

    @Override
    protected Task<List<HotSLogsHero>> createTask() {
// Path: src/main/java/ninja/eivind/hotsreplayuploader/concurrent/tasks/HeroListTask.java
// public class HeroListTask extends Task<List<HotSLogsHero>> {
//     public static final String API_ROUTE = "https://www.hotslogs.com/API/Data/Heroes";
//     private final SimpleHttpClient httpClient;
// 
//     public HeroListTask(SimpleHttpClient httpClient) {
//         this.httpClient = httpClient;
//     }
// 
//     @Override
//     protected List<HotSLogsHero> call() throws Exception {
//         final String result = httpClient.simpleRequest(API_ROUTE);
//         final HotSLogsHero[] heroes = new ObjectMapper().readValue(result, HotSLogsHero[].class);
//         final List<HotSLogsHero> heroList = Arrays.asList(heroes);
//         heroList.sort(Comparator.comparing(HotSLogsHero::getPrimaryName));
//         return heroList;
//     }
// }
// 
// Path: src/main/java/ninja/eivind/hotsreplayuploader/providers/hotslogs/HotSLogsHero.java
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class HotSLogsHero implements Hero {
// 
//     @JsonProperty("PrimaryName")
//     private String primaryName;
//     @JsonProperty("ImageURL")
//     private String imageURL;
// 
//     public HotSLogsHero() {
//     }
// 
//     public HotSLogsHero(final String primaryName, final String imageURL) {
//         this.primaryName = primaryName;
//         this.imageURL = imageURL;
//     }
// 
//     @Override
//     public String toString() {
//         return primaryName;
//     }
// 
//     @Override
//     public boolean equals(final Object o) {
//         if (this == o) return true;
//         if (o == null || getClass() != o.getClass()) return false;
// 
//         final HotSLogsHero hero = (HotSLogsHero) o;
// 
//         return !(primaryName != null ? !primaryName.equals(hero.primaryName) : hero.primaryName != null)
//                 && !(imageURL != null ? !imageURL.equals(hero.imageURL) : hero.imageURL != null);
// 
//     }
// 
//     @Override
//     public int hashCode() {
//         int result = primaryName != null ? primaryName.hashCode() : 0;
//         result = 31 * result + (imageURL != null ? imageURL.hashCode() : 0);
//         return result;
//     }
// 
//     @Override
//     public String getPrimaryName() {
//         return primaryName;
//     }
// 
//     @Override
//     public void setPrimaryName(final String primaryName) {
//         this.primaryName = primaryName;
//     }
// 
//     @Override
//     public String getImageURL() {
//         return imageURL;
//     }
// 
//     @Override
//     public void setImageURL(final String imageURL) {
//         this.imageURL = imageURL;
//     }
// 
// }
// 
// Path: src/main/java/ninja/eivind/hotsreplayuploader/utils/SimpleHttpClient.java
// public class SimpleHttpClient {
// 
//     public static final String SPACE = "%20";
//     private static final Logger LOG = LoggerFactory.getLogger(SimpleHttpClient.class);
// 
//     /**
//      * Encodes a given String url into a valid URI if possible
//      * Method should be updated as required to make sure any valid String url that can be pasted into an address bar
//      * will fit into a URI object
//      *
//      * @param url the url to encode
//      * @return a newly built URI if at all possible
//      */
//     public static URI encode(final String url) {
//         return URI.create(url.replaceAll(" ", SPACE));
//     }
// 
//     public String simpleRequest(final String target) throws IOException {
//         return simpleRequest(encode(target));
//     }
// 
//     public String simpleRequest(final URI uri) throws IOException {
//         return simpleRequest(uri.toURL());
//     }
// 
//     public String simpleRequest(final URL url) throws IOException {
//         LOG.info("Opening connection to " + url);
//         final InputStream inputStream = url.openStream();
//         return IOUtils.readInputStream(inputStream);
//     }
// }


// Path: src/main/java/ninja/eivind/hotsreplayuploader/services/HeroService.java
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.util.Duration;
import ninja.eivind.hotsreplayuploader.concurrent.tasks.HeroListTask;
import ninja.eivind.hotsreplayuploader.providers.hotslogs.HotSLogsHero;
import ninja.eivind.hotsreplayuploader.utils.SimpleHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

// Copyright 2015 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.services;



/**
 * {@link ScheduledService} for checking the current list of available {@link HotSLogsHero}es periodically.
 */
@Component
public class HeroService extends ScheduledService<List<HotSLogsHero>> {

    private static final Logger LOG = LoggerFactory.getLogger(HeroService.class);
    @Autowired
    private SimpleHttpClient httpClient;

    public HeroService() {
        LOG.info("Instantiating " + getClass().getSimpleName());
        setPeriod(Duration.hours(2));
        setBackoffStrategy(param -> param.getPeriod().multiply(1.25));
        LOG.info("Instantiated " + getClass().getSimpleName());
    }

    @Override
    protected Task<List<HotSLogsHero>> createTask() {
        final HeroListTask heroListTask = new HeroListTask(httpClient);