package net.northking.cctp.upperComputer.webSocket.function;

import com.alibaba.fastjson.JSONObject;
import net.northking.cctp.upperComputer.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class IosFunction {

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

    private final int port;

    public IosFunction(int port) {
        this.port = port;
    }


    /**
     * 获取屏幕宽高
     *
     * @return
     */
    public JSONObject getScreenSize() {
        String screenUrl = String.format(FunctionConstants.SCREEN_SIZE, port);
        logger.debug("请求ios获取屏幕宽高的地址：{}", screenUrl);
        JSONObject screenSize = doWdaGet(screenUrl);
        return screenSize;
    }

    /**
     * 获取屏幕方向
     *
     * @return
     */
    public JSONObject getScreenRotation() {
        String screenUrl = String.format(FunctionConstants.SCREEN_ROTATION, port);
        logger.debug("请求ios获取屏幕方向的地址：{}", screenUrl);
        JSONObject screenRotation = doWdaGet(screenUrl);
        return screenRotation;
    }

    /**
     * 旋转屏幕
     *
     * @param param
     * @return
     */
    public JSONObject rotationScreen(Map<String, Object> param) {
        String screenUrl = String.format(FunctionConstants.SCREEN_ROTATION, port);
        JSONObject rotationResult = doWdaPost(screenUrl, param);
        return rotationResult;
    }

    /**
     * 点击屏幕
     *
     * @param x
     * @param y
     * @return
     */
    public JSONObject click(Float x, Float y) {
        String url = String.format(FunctionConstants.SCREEN_CLICK, port);
        logger.debug("请求点击屏幕的地址：{}", url);
        Map<String, Object> param = new HashMap<>();
        param.put("x", x);
        param.put("y", y);
        JSONObject clickResult = doWdaPost(url, param);
        return clickResult;
    }

    /**
     * 长按屏幕
     *
     * @param startX
     * @param startY
     * @param endX
     * @param endY
     * @return
     */
    public JSONObject longPress(Float startX, Float startY, Float endX, Float endY) {
        String url = String.format(FunctionConstants.SCREEN_SWIPE, port);
        logger.debug("请求长按屏幕的地址：{}", url);
        Map<String, Object> param = new HashMap<>();
        param.put("fromX", startX);
        param.put("fromY", startY);
        param.put("toX", endX);
        param.put("toY", endY);
        param.put("duration", 1.5f);
        JSONObject result = doWdaPost(url, param);
        return result;
    }

    /**
     * 滑动屏幕
     *
     * @param startX
     * @param startY
     * @param endX
     * @param endY
     * @param time
     * @return
     */
    public JSONObject swipe(Float startX, Float startY, Float endX, Float endY, Long time) {
        String swipeUrl = String.format(FunctionConstants.SCREEN_SWIPE, port);
        logger.debug("请求滑动屏幕的地址：{}", swipeUrl);
        Map<String, Object> param = new HashMap<>();
        param.put("fromX", startX);
        param.put("fromY", startY);
        param.put("toX", endX);
        param.put("toY", endY);
        if (time > 300) {
            time = 300l;
        }
        param.put("duration", time / 1000f);
        JSONObject swipeResult = doWdaPost(swipeUrl, param);
        return swipeResult;
    }

    /**
     * 点击home键
     *
     * @return
     */
    public JSONObject pressHome() {
        String homeUrl = String.format(FunctionConstants.HOME_CLICK, port);
        logger.debug("请求点击home键的地址：{}", homeUrl);
        JSONObject clickResult = doWdaPost(homeUrl, null);
        return clickResult;
    }

    /**
     * 输入文本
     *
     * @param text
     * @return
     */
    public JSONObject inputText(String text) {
        String screenUrl = String.format(FunctionConstants.INPUT_TEXT, port);
        logger.debug("请求输入文本的地址：{}", screenUrl);
        Map<String, Object> param = new HashMap<>();
        param.put("value", text);
        JSONObject clickResult = doWdaPost(screenUrl, param);
        return clickResult;
    }


    /**
     * 删除一个字符
     */
    public JSONObject deleteOneKey() {
        String screenUrl = String.format(FunctionConstants.DELETE_ONE_CHAR, port);
        logger.debug("请求删除一个字符的地址：{}", screenUrl);
        JSONObject deleteResult = doWdaPost(screenUrl, null);
        return deleteResult;
    }

    private JSONObject doWdaGet(String url) {
        JSONObject result = null;
        try {
            JSONObject value = HttpUtils.doGet(new URI(url), JSONObject.class);
            result = value.getJSONObject("value");
        } catch (Exception e) {
            logger.error("请求wda出错", e);
        }
        return result;
    }

    private JSONObject doWdaPost(String url, Map<String, Object> param) {
        JSONObject result = null;
        try {
            HttpEntity<Map<String, Object>> entity = new HttpEntity<>(param);
            JSONObject value = HttpUtils.doPost(new URI(url), entity, JSONObject.class);
            result = value.getJSONObject("value");
        } catch (Exception e) {
            logger.error("wda请求出错", e);
        }
        return result;
    }

}
