/*
 *  Copyright 2022 Red Hat
 *
 *  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
 *
 *      https://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 org.jboss.hal.client.bootstrap.tasks;

import javax.inject.Inject;

import org.jboss.hal.config.Build;
import org.jboss.hal.config.Environment;
import org.jboss.hal.config.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.web.bindery.event.shared.EventBus;

import elemental2.dom.XMLHttpRequest;

import static org.jboss.hal.dmr.dispatch.Dispatcher.HttpMethod.GET;

public class CheckForUpdate implements InitializedTask {

    private static final String METADATA_URL = "https://raw.githubusercontent.com/hal/console/main/version.txt";
    private static final Logger logger = LoggerFactory.getLogger(CheckForUpdate.class);

    private final Environment environment;
    private final EventBus eventBus;

    @Inject
    public CheckForUpdate(Environment environment, EventBus eventBus) {
        this.environment = environment;
        this.eventBus = eventBus;
    }

    @Override
    public void run() {
        // only check for community updates
        if (environment.getHalBuild() == Build.PRODUCT) {
            logger.debug("Version update check skipped for EAP");
        } else {
            logger.debug("Check for update");
            XMLHttpRequest xhr = new XMLHttpRequest();
            xhr.onload = event -> {
                String versionText = xhr.responseText;
                try {
                    logger.debug("Online version {}", versionText);
                    Version version = Version.parseVersion(versionText);
                    eventBus.fireEvent(new VersionUpdateEvent(version));
                } catch (Throwable t) {
                    logger.warn("Cannot parse version from {}: {}", versionText, t.getMessage());
                }
            };
            xhr.addEventListener("error", event -> logger.warn("Cannot read version from {}: Status {} {}",
                    METADATA_URL, xhr.status, xhr.statusText));
            xhr.open(GET.name(), METADATA_URL, true);
            xhr.send();
        }
    }
}

@@ -653,29 +653,182 @@
 ort 
-javax.inject.Inject;%0A
+com.google.gwt.core.client.JsonUtils;%0Aimport com.google.gwt.json.client.JSONObject;%0Aimport com.google.web.bindery.event.shared.EventBus;%0Aimport elemental2.dom.XMLHttpRequest;
 %0Aimp
@@ -1006,90 +1006,26 @@
 ort 
-com.google.web.bindery.event.shared.EventBus;%0A%0Aimport elemental2.dom.XMLHttpReques
+javax.inject.Injec
 t;%0A%0A
@@ -1211,65 +1211,60 @@
 s://
-raw
+api
 .github
-usercontent.com/hal/console/main/version.tx
+.com/repos/hal/console/releases/lates
 t%22;%0A
+%0A
     
@@ -1594,16 +1594,401 @@
 %0A    %7D%0A%0A
+    private String getVersionFromResponse(String responseText) %7B%0A        JSONObject data = new JSONObject(JsonUtils.safeEval(responseText));%0A        String version = data.get(%22tag_name%22).toString();%0A        version = version.substring(1, version.length() - 1);%0A        if (version.startsWith(%22v%22)) %7B%0A            version = version.substring(1);%0A        %7D%0A        return version;%0A    %7D%0A%0A
     @Ove
@@ -2362,27 +2362,27 @@
  String 
-versionText
+xhrResponse
  = xhr.r
@@ -2394,16 +2394,90 @@
 seText;%0A
+                String versionText = getVersionFromResponse(xhrResponse);%0A
         
@@ -3141,10 +3141,9 @@
 %7D%0A    %7D%0A
+
 %7D
-%0A

Enhance check for update feature