package net.northking.cctp.upperComputer.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.appium.java_client.AppiumDriver;
import net.northking.cctp.upperComputer.automation.entity.ScreenInfo;
import net.northking.cctp.upperComputer.deviceManager.HarmonyDeviceManager;
import net.northking.cctp.upperComputer.deviceManager.screen.HarmonyScreenResponseThread;
import net.northking.cctp.upperComputer.deviceManager.thread.HarmonyProvider;
import net.northking.cctp.upperComputer.driver.harmony.HarmonyDevice;
import net.northking.cctp.upperComputer.entity.DebuggerDeviceInfo;
import net.northking.cctp.upperComputer.utils.deviceHepler.harmony.HarmonyHandlerHelper;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author : yineng.huang
 * @date : 2024/10/31 16:54
 */
@Service("harmonyService")
public class HarmonyDebuggerServiceImpl extends AbstractDebuggerService {

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

    public HarmonyDebuggerServiceImpl() {
        this.deviceHandleHelper = new HarmonyHandlerHelper();
    }

    @Override
    public Object getUiTree(String deviceId) {
        return null;
    }

    @Override
    public AppiumDriver<WebElement> getDriver(DebuggerDeviceInfo info) {
        return null;
    }

    @Override
    public boolean initApp(DebuggerDeviceInfo info) {
        return false;
    }

    @Override
    public String getScreenShot(DebuggerDeviceInfo info) {
        ScreenInfo screenInfo = HarmonyDeviceManager.getInstance().getScreenInfo(info.getDeviceId());
        String path = getScreenShot(info, screenInfo.getWidth(), screenInfo.getHeight());
        return path;
    }

    @Override
    public boolean destroyDriver(String deviceId) {
        return false;
    }

    @Override
    public String getScreenShotToBase64(DebuggerDeviceInfo info) {
        return null;
    }

    @Override
    public String shotAllScreen(DebuggerDeviceInfo info) {
        return null;
    }

    @Override
    public String uploadShotAllScreen(DebuggerDeviceInfo info) {
        return null;
    }

    @Override
    public boolean startRecord(DebuggerDeviceInfo info) {
        logger.info("收到设备【{}】在任务【{}】开启录屏的请求............",info.getDeviceId(),info.getTaskId());
        HarmonyScreenResponseThread screenThread = HarmonyDeviceManager.getInstance().getScreenThread(info.getDeviceId());
        if (null == screenThread) {
            HarmonyProvider provider = HarmonyDeviceManager.getInstance().getCurrentDeviceProvider(info.getDeviceId());
            if (null == provider) {
                logger.error("当前设备【{}】不在线，无法开启录频",info.getDeviceId());
                return false;
            }
            HarmonyDevice currentDevice = HarmonyDeviceManager.getInstance().getCurrentDevice(info.getDeviceId());
            if (null == currentDevice) {
                logger.error("当前设备【{}】不在线，无法开启录频",info.getDeviceId());
                return false;
            }
            screenThread = new HarmonyScreenResponseThread(currentDevice);
            HarmonyDeviceManager.getInstance().saveScreenThread(info.getDeviceId(), screenThread);
            //开启手机端的屏幕
            boolean success = provider.startCaptureScreenImageStream(0.5f, screenThread);
            logger.info("开始拉取设备【{}】的屏幕,结果：{}",info.getDeviceId(),success);
            if (!success) {
                logger.error("拉取设备【{}】的屏幕失败",info.getDeviceId());
                return false;
            }
        }
        String taskId = info.getTaskId();
        String tenantId = info.getTenantId();
        String resolution = info.getResolution();
        String[] split = resolution.split("\\*");
        int width = Integer.parseInt(split[0]);
        int height = Integer.parseInt(split[1]);
        int isBigger = height - width;
        if (isBigger < 0) {  //高比宽小，换一下
            int temp = width;
            width = height;
            height = temp;
        }
        screenThread.startRecordScreen(tenantId, width, height,taskId);
        return true;
    }

    @Override
    public String endRecord(DebuggerDeviceInfo info) {
        String videoUrl = null;
        logger.info("收到设备【{}】在任务【{}】关闭录屏的请求............",info.getDeviceId(),info.getTaskId());
        HarmonyScreenResponseThread screenThread = HarmonyDeviceManager.getInstance().getScreenThread(info.getDeviceId());
        if (null != screenThread) {
            String result = screenThread.stopRecord(info);
            JSONObject jsonObject = JSON.parseObject(result, JSONObject.class);
            videoUrl = jsonObject.getString("videoUrl");
        } else {
            logger.info("设备【{}】在任务【{}】录屏不存在............",info.getDeviceId(),info.getTaskId());
        }
        logger.info("设备【{}】在任务【{}】录屏保存的地址：{}............",info.getDeviceId(),info.getTaskId(),videoUrl);
        return videoUrl;
    }

    @Override
    public String getCurrentMessage(String deviceId) {
        return null;
    }

}
