package net.northking.cctp.upperComputer.automation.handler;

import com.alibaba.fastjson.JSONObject;
import net.northking.cctp.upperComputer.automation.constants.AutomationRequestCmd;
import net.northking.cctp.upperComputer.automation.entity.CmdAutomationRequest;
import net.northking.cctp.upperComputer.automation.entity.CmdAutomationResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.Session;
import java.io.IOException;


public abstract class AbstractAutomationHandler implements AutomationMessageHandler{

    private final Logger logger = LoggerFactory.getLogger(AbstractAutomationHandler.class);

    protected String serial;

    protected Session engineSession;

    @Override
    public void automationHandle(CmdAutomationRequest request, Session engineSession) {
        String cmd = request.getCmd();
        logger.debug("收到设备【{}】自动化指令:【{}】",serial,cmd);
        if (AutomationRequestCmd.OPEN_MOBILE_NOTIFICATION_BAR.equals(cmd)) { //打开手机通知栏
            openMobileNotificationBar(request);
        } else if (AutomationRequestCmd.CLOSE_MOBILE_NOTIFICATION_BAR.equals(cmd)){//关闭手机通知栏
            closeMobileNotificationBar(request);
        } else if (AutomationRequestCmd.INIT_APP.equals(cmd)) {
            initApplication(request);
        } else if (AutomationRequestCmd.SEARCH_ELEMENT_BY_NODE.equals(cmd)) {
            searchNodeByNodeTree(request);
        } else if (AutomationRequestCmd.SEARCH_ELEMENT_BY_POINT.equals(cmd)) {
            searchNodeByPoint(request);
        } else if (AutomationRequestCmd.SEARCH_ELEMENT_BY_OCR.equals(cmd)) {
            searchNodeByOcr(request);
        } else if (AutomationRequestCmd.SEARCH_ELEMENT_BY_IMAGE.equals(cmd)) {
            searchNodeByImage(request);
        } else if (AutomationRequestCmd.GET_ELEMENT_VALUE_BY_NODE.equals(cmd)) {
            getElementValueByNode(request);
        }else if (AutomationRequestCmd.GET_ELEMENT_VALUE_BY_OCR.equals(cmd)) {
            getElementValueByOcr(request);
        }else if (AutomationRequestCmd.GET_VERIFICATION_CODE_BY_NODE.equals(cmd)) {
            getVerificationCodeByNode(request);
        }else if (AutomationRequestCmd.GET_VERIFICATION_CODE_BY_OCR.equals(cmd)) {
            getVerificationCodeByOcr(request);
        }else if (AutomationRequestCmd.LONG_PRESS.equals(cmd)) {
            longPress(request);   //暂时没用
        }else if (AutomationRequestCmd.SWIPE.equals(cmd)) {
            swipe(request);
        }else if (AutomationRequestCmd.INPUT_TEXT.equals(cmd)) {
            inputText(request);
        }else if (AutomationRequestCmd.CLICK_POINT.equals(cmd)) {
            clickPoint(request);
        }else if (AutomationRequestCmd.HANDLE_ELEMENT_BY_NODE.equals(cmd)) {
            handleNodeByNodeTree(request);
        }else if (AutomationRequestCmd.HANDLE_ELEMENT_BY_POINT.equals(cmd)) {
            handleNodeByPoint(request);
        }else if (AutomationRequestCmd.HANDLE_ELEMENT_BY_OCR.equals(cmd)) {
            handleNodeByOcr(request);
        }else if (AutomationRequestCmd.HANDLE_ELEMENT_BY_IMAGE.equals(cmd)) {
            handleNodeByImage(request);
        } else if (AutomationRequestCmd.CLICK_TEXT_BY_OCR.equals(cmd)) {
            clickTextByOcr(request);
        } else if (AutomationRequestCmd.CLICK_TEXT_BY_OCR_NEW.equals(cmd)) {
            clickTextByOcrNew(request);
        } else if (AutomationRequestCmd.IF_TEXT.equals(cmd)) {
            ifText(request);
        } else if (AutomationRequestCmd.HANDLE_APP.equals(cmd)) {
            handleApp(request);
        } else if (AutomationRequestCmd.PRESS_HOME_KEY.equals(cmd)) {
            pressHomeKey(request);
        } else if (AutomationRequestCmd.INPUT_PASSWORD_BY_OCR.equals(cmd)) {
            inputPasswordByOcr(request);
        } else if (AutomationRequestCmd.GET_ELEMENT_MONEY_TEXT.equals(cmd)) {
            getElementMoneyText(request);
        } else if (AutomationRequestCmd.SCREEN_SHOT.equals(cmd)) {
            screenShot(request);
        } else if (AutomationRequestCmd.GET_ELEMENT_VALUE_BY_PATH_OCR.equals(cmd)) {
            getElementValueByPathOcr(request);
        } else if (AutomationRequestCmd.PRESS_AND_SWIPE.equals(cmd)) {
            pressAndSwipe(request);
        }
    }

    protected void sendResultToEngine(CmdAutomationResponse response){
        String result = JSONObject.toJSONString(response);
        logger.debug("步骤token:【{}】执行完成,返回结果:{}",response.getStepToken(),result);
        if (null != engineSession && engineSession.isOpen()) {
            synchronized (engineSession){
                try {
                    engineSession.getBasicRemote().sendText(result);
                } catch (IOException e) {
                    logger.warn("步骤token:【{}】执行完成,返回结果时连接出现异常", response.getStepToken(),e);
                }
            }
        } else {
            logger.warn("步骤token:【{}】执行完成,返回结果时引擎连接已经关闭", response.getStepToken());
        }

    }
}
