package net.northking.cctp.se.util;

import com.alibaba.fastjson.JSON;
import net.northking.cctp.element.core.exception.ExecuteException;
import net.northking.cctp.se.device.bean.DebuggerDeviceInfo;
import net.northking.cctp.se.device.bean.DeviceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;

import java.util.Map;

public class AndroidUtil extends PhoneUtil{

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


    private static final String GET_PACKAGE_VERSION_URL = "http://%s/engine/getPackageVersion";
    private static final String ALL_SCREEN_SHOT_URL = "http://%s/engine/uploadShotAllScreen";
    private static final String START_RECORD_URL = "http://%s/engine/startRecord";
    private static final String END_RECORD_URL = "http://%s/engine/endRecord";
    private static final String RELEASE_ADB_FORWARD_PORT = "http://%s/engine/releaseAdbForwardPort";


    public static boolean releaseAdbForwardPort(String remoteAddress,String deviceId){
        try {
            DebuggerDeviceInfo info = new DebuggerDeviceInfo();
            info.setDeviceId(deviceId);
            HttpEntity<DebuggerDeviceInfo> entity = new HttpEntity<>(info);
            return HttpUtils.doPost(String.format(RELEASE_ADB_FORWARD_PORT, remoteAddress),entity, Boolean.class);
        } catch (Exception e) {
            logger.error("清除数据失败，原因：{}",e);
            return false;
        }
    }


    /**
     * 获取手机应用版本号
     * @param deviceId
     * @param appPackage
     * @return
     */
    public static String getOldPackageCode(String remoteAddress,String deviceId,String appPackage,String platform){
        DebuggerDeviceInfo info = new DebuggerDeviceInfo();
        info.setDeviceId(deviceId);
        info.setAppPackage(appPackage);
        info.setPlatform(platform);
        HttpEntity<DebuggerDeviceInfo> entity = new HttpEntity<>(info);
        try {
            return HttpUtils.doPost(String.format(GET_PACKAGE_VERSION_URL, remoteAddress),entity, String.class);
        } catch (Exception e) {
            logger.error("获取应用版本号失败，原因：{}",e);
            return "";
        }
    }


    public static String snapshotAllScreen(String remoteAddress, DeviceInfo deviceInfo, String tenantId) {
        DebuggerDeviceInfo info = new DebuggerDeviceInfo();
        info.setPlatform(deviceInfo.getPlatform());
        info.setDeviceId(deviceInfo.getDeviceId());
        info.setResolution(deviceInfo.getResolution());
        info.setTenantId(tenantId); //租户id
        logger.info("参数：{}", JSON.toJSONString(info));
        HttpEntity<DebuggerDeviceInfo> entity = new HttpEntity<>(info);
        String result = null;
        try {
            result = HttpUtils.doPost(String.format(ALL_SCREEN_SHOT_URL, remoteAddress), entity, String.class);
        } catch (Exception e) {
            logger.error("截图失败",e);
        }
        return result;
    }

}
