/*
 *  Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you 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 org.wso2.identity.integration.test.auth;

import java.io.File;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.wso2.carbon.automation.extensions.servers.utils.ServerLogReader;
import org.wso2.carbon.integration.common.utils.exceptions.AutomationUtilException;
import org.wso2.carbon.integration.common.utils.mgt.ServerConfigurationManager;

/**
 * Initiation Test for adaptive authentication.
 */
public class AdaptiveScriptInitializerTestCase extends AbstractAdaptiveAuthenticationTestCase {

    private ServerConfigurationManager serverConfigurationManager;

    private static String enableInfo = "Adaptive authentication successfully enabled.";
    private static String disableInfo = "Adaptive authentication successfully disabled.";

    @BeforeTest(alwaysRun = true)
    public void testInit() throws Exception {

        super.init();
        serverConfigurationManager = new ServerConfigurationManager(isServer);
        runAdaptiveAuthenticationDependencyScript(false);
    }

    private void runAdaptiveAuthenticationDependencyScript(boolean disable) {

        ServerLogReader inputStreamHandler;
        ServerLogReader errorStreamHandler;
        String targetFolder = System.getProperty("carbon.home");
        String scriptFolder =  getTestArtifactLocation() + File.separator;
        Process tempProcess = null;
        File scriptFile = new File(scriptFolder);
        Runtime runtime = Runtime.getRuntime();

        try {
            if (System.getProperty("os.name").toLowerCase().contains("windows")) {
                log.warn("Operating System is Windows, skipping execution");
            } else {
                log.info("Operating system is not windows. Executing shell script");
                if (disable) {
                    tempProcess = runtime.getRuntime().exec(
                            new String[] { "/bin/bash", "adaptive.sh", targetFolder, "DISABLE" }, null, scriptFile);
                } else {
                    tempProcess = runtime.getRuntime().exec(
                            new String[] { "/bin/bash", "adaptive.sh", targetFolder }, null, scriptFile);
                }
                errorStreamHandler = new ServerLogReader("errorStream",
                        tempProcess.getErrorStream());
                inputStreamHandler = new ServerLogReader("inputStream",
                        tempProcess.getInputStream());
                inputStreamHandler.start();
                errorStreamHandler.start();
                boolean runStatus = waitForMessage(inputStreamHandler, disable);
                log.info("Status Message : " + runStatus);
                restartServer();
            }
        } catch (Exception e) {
            log.error("Failed to execute adaptive authentication dependency script", e);
        } finally {
            if (tempProcess != null) {
                tempProcess.destroy();
            }
        }
    }

    private void restartServer() throws AutomationUtilException {

        serverConfigurationManager.restartGracefully();
    }

    private boolean waitForMessage(ServerLogReader inputStreamHandler, boolean disable) {
        long time = System.currentTimeMillis() + 60 * 1000;
        String message = enableInfo;
        if (disable) {
            message = disableInfo;
        }
        while (System.currentTimeMillis() < time) {
            if (inputStreamHandler.getOutput().contains(message)) {
                return true;
            }
        }
        return false;
    }

    @AfterTest(alwaysRun = true)
    public void resetUserstoreConfig() throws Exception {

        super.init();
        runAdaptiveAuthenticationDependencyScript(true);
        serverConfigurationManager.restoreToLastConfiguration(false);
    }
}

@@ -2278,12 +2278,12 @@
 log.
-warn
+info
 (%22Op
@@ -2311,29 +2311,1056 @@
 dows
-, skipping execution%22
+. Executing batch script%22);%0A                if (disable) %7B%0A                    // TODO https://github.com/wso2/product-is/issues/14301%0A                    restartServer();%0A                    tempProcess = runtime.exec(%0A                            new String%5B%5D %7B %22cmd%22, %22/c%22, %22adaptive.bat%22, targetFolder, %22DISABLE%22 %7D, null, scriptFile);%0A                %7D else %7B%0A                    tempProcess = runtime.exec(%0A                            new String%5B%5D %7B %22cmd%22, %22/c%22, %22adaptive.bat%22, targetFolder %7D, null, scriptFile);%0A                %7D%0A                errorStreamHandler = new ServerLogReader(%22errorStream%22,%0A                        tempProcess.getErrorStream());%0A                inputStreamHandler = new ServerLogReader(%22inputStream%22,%0A                        tempProcess.getInputStream());%0A                inputStreamHandler.start();%0A                errorStreamHandler.start();%0A                boolean runStatus = waitForMessage(inputStreamHandler, disable);%0A                log.info(%22Status Message : %22 + runStatus);%0A                restartServer(
 );%0A 

Add run adaptive auth script for windows