package net.northking.cctp.element.mobile.keywords;

import net.northking.cctp.element.core.DeviceDriver;
import net.northking.cctp.element.core.annotation.Argument;
import net.northking.cctp.element.core.annotation.Keyword;
import net.northking.cctp.element.core.annotation.Keywords;
import net.northking.cctp.element.core.annotation.Return;
import net.northking.cctp.element.core.exception.EnvironmentException;
import net.northking.cctp.element.core.exception.ExecuteException;
import net.northking.cctp.element.core.exception.ParamMistakeException;
import net.northking.cctp.element.core.type.DataType;
import net.northking.cctp.element.core.type.ParamScope;
import net.northking.cctp.element.mobile.thread.PressKeyBackThread;
import net.northking.cctp.element.mobile.thread.PressKeyMenuThread;
import org.openqa.selenium.NoSuchSessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

@Keywords
public class AndroidTools {

    private Logger logger = LoggerFactory.getLogger(AndroidTools.class);

    private final ExecutorService executorService = Executors.newCachedThreadPool();


    /**
     * <p>点击返回键</p>
     *
     * @param deviceDriver 设备连接驱动
     * @return
     */
    @Keyword(alias = "点击返回健", category = "0", attributes = "4", commonlyUse = true)
    @Return(name = "result", comment = "点击返回健", type = DataType.BOOLEAN)
    public boolean pressKeyBack(@Argument(name = "__deviceDriver", scope = ParamScope.CONTEXT, comment = "设备连接", type = DataType.OBJECT) DeviceDriver deviceDriver) {
        PressKeyBackThread pressKeyBackThread = new PressKeyBackThread(deviceDriver);
        Future<Boolean> future = executorService.submit(pressKeyBackThread);
        Boolean aBoolean = null;
        deviceDriver.setFuture(future);
        try {
            aBoolean = future.get(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logger.warn("该线程被中断了。。。。。。");
            throw new ExecuteException("执行中断");
        } catch (ExecutionException e) {
            logger.warn("在执行的时候报错了。。。。");
            Throwable cause = e.getCause();
            if (cause instanceof ExecuteException) {
                ExecuteException ee = (ExecuteException) cause;
                throw ee;
            } else if (cause instanceof ParamMistakeException) {
                ParamMistakeException pe = (ParamMistakeException) cause;
                throw pe;
            } else if (cause instanceof NoSuchSessionException) {
                NoSuchSessionException ne = (NoSuchSessionException) cause;
                throw ne;
            } else if (cause instanceof EnvironmentException) {
                EnvironmentException ee = (EnvironmentException) cause;
                throw ee;
            }else{
                logger.error("出现了其他的问题。。。。");
                throw new ExecuteException(cause.getMessage());
            }
        } catch (TimeoutException e) {
            logger.warn("处理超时了。。。。。。");
            throw new ExecuteException("超时未反应");
        } catch (CancellationException e) {
            logger.warn("该操作被取消了");
            throw new ExecuteException("用户取消，执行失败");
        }
        return aBoolean;
        /*try {
            AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
            FunctionKeyUtil.pressKeyBack(driver);
        } catch (Exception e) {
            throw new ExecuteException("点击返回健失败");
        }
        return true;*/
    }

    /**
     * <p>点击功能菜单键</p>
     *
     * @param deviceDriver 设备连接驱动
     * @return
     */
    @Keyword(alias = "点击多任务键", category = "0", attributes = "4", commonlyUse = true)
    @Return(name = "result", comment = "点击多任务键", type = DataType.BOOLEAN)
    public boolean pressKeyMenu(@Argument(name = "__deviceDriver", scope = ParamScope.CONTEXT, comment = "设备连接", type = DataType.OBJECT) DeviceDriver deviceDriver) {
        PressKeyMenuThread pressKeyMenuThread = new PressKeyMenuThread(deviceDriver);
        Future<Boolean> future = executorService.submit(pressKeyMenuThread);
        Boolean aBoolean = null;
        deviceDriver.setFuture(future);
        try {
            aBoolean = future.get(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logger.warn("该线程被中断了。。。。。。");
            throw new ExecuteException("执行中断");
        } catch (ExecutionException e) {
            logger.warn("在执行的时候报错了。。。。");
            Throwable cause = e.getCause();
            if (cause instanceof ExecuteException) {
                ExecuteException ee = (ExecuteException) cause;
                throw ee;
            } else if (cause instanceof ParamMistakeException) {
                ParamMistakeException pe = (ParamMistakeException) cause;
                throw pe;
            } else if (cause instanceof NoSuchSessionException) {
                NoSuchSessionException ne = (NoSuchSessionException) cause;
                throw ne;
            } else if (cause instanceof EnvironmentException) {
                EnvironmentException ee = (EnvironmentException) cause;
                throw ee;
            }else{
                logger.error("出现了其他的问题。。。。");
                throw new ExecuteException(cause.getMessage());
            }
        } catch (TimeoutException e) {
            logger.warn("处理超时了。。。。。。");
            throw new ExecuteException("超时未反应");
        } catch (CancellationException e) {
            logger.warn("该操作被取消了");
            throw new ExecuteException("用户取消，执行失败");
        }
        return aBoolean;
        /*try {
            AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
            FunctionKeyUtil.pressKeyMenu(driver);
        } catch (Exception e) {
            throw new ExecuteException("点击多任务键失败");
        }
        return true;*/
    }
}
