package com.backendsys.modules.sdk.baidu.yunapp.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.backendsys.exception.CustException;
import com.backendsys.modules.sdk.baidu.yunapp.entity.AppOperateParams;
import com.backendsys.modules.sdk.baidu.yunapp.entity.ExecuteScriptParams;
import com.backendsys.modules.sdk.baidu.yunapp.entity.ScreenshotParams;
import com.backendsys.modules.sdk.baidu.yunapp.entity.YuappSign;
import com.backendsys.modules.sdk.baidu.yunapp.service.YunappService;
import com.backendsys.modules.sdk.baidu.yunapp.utils.YunappUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 百度智能云 - ARM云手机
 * https://yunapp.baidu.com/console/bgs/doc/?d=bgs-phone&p=PHONE_API_use
 */
@Service
public class YunappServiceImpl implements YunappService {

    @Autowired
    private YunappUtil yunappUtil;

    /**
     * 实例执行脚本
     * https://yunapp.baidu.com/console/bgs/doc/?d=bgs-phone&p=PHONE_API_base_instance_execute_script
     */
    @Override
    public JSONArray executeScript(ExecuteScriptParams executeScriptParams) {
        // 签名
        YuappSign signEntity = yunappUtil.getSign();

        // 接口参数
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("padCodes", executeScriptParams.getPad_codes());
        params.put("scriptContent", executeScriptParams.getScript_content());
        params.put("createTime", signEntity.getNonce());
        params.put("msg", yunappUtil.getMsg(JSONUtil.toJsonStr(params), signEntity.getNonce()));

        String url = "https://platform.armvm.com/command/pad/execute-script.html" + signEntity.toParams();
        System.out.println("url = " + url);
        System.out.println("params = " + params);

        // 发起请求
        try(HttpResponse response = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(params))
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .execute()
        ) {
            System.out.println("response = " + response.body());
            return yunappUtil.getResponseJSONArray(response.body());
        } catch (Exception e) {
            throw new CustException(e.getMessage());
        }
    }

    /**
     * 实例执行脚本
     * https://cloud.baidu.com/doc/ARMCM/s/2kei7tyr3#%E4%BB%BB%E5%8A%A1%E7%BB%93%E6%9E%9C%E6%9F%A5%E8%AF%A2
     */
    @Override
    public JSONArray executeScriptInfo(List<Long> task_ids) {
        // 签名
        YuappSign signEntity = yunappUtil.getSign();

        // 接口参数
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("taskIds", task_ids);
        params.put("createTime", signEntity.getNonce());
        params.put("msg", yunappUtil.getMsg(JSONUtil.toJsonStr(params), signEntity.getNonce()));

        String url = "https://platform.armvm.com/command/pad/execute-task-info.html" + signEntity.toParams();
        System.out.println("url = " + url);
        System.out.println("params = " + params);

        // 发起请求
        try(HttpResponse response = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(params))
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .execute()
        ) {
            System.out.println("response = " + response.body());
            return yunappUtil.getResponseJSONArray(response.body());
        } catch (Exception e) {
            throw new CustException(e.getMessage());
        }
    }

    /**
     * 实例截图
     * https://yunapp.baidu.com/console/bgs/doc/?d=bgs-phone&p=PHONE_API_base_screenshot
     */
    @Override
    public JSONArray screenshot(ScreenshotParams screenshotParams) {
        // 签名
        YuappSign signEntity = yunappUtil.getSign();

        // 接口参数
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("padCodes", screenshotParams.getPad_codes());
        params.put("quality", screenshotParams.getQuality());
        params.put("createTime", signEntity.getNonce());
        params.put("msg", yunappUtil.getMsg(JSONUtil.toJsonStr(params), signEntity.getNonce()));

        String url = "https://platform.armvm.com/command/pad/screenshot.html" + signEntity.toParams();
        System.out.println("url = " + url);
        System.out.println("params = " + params);
        System.out.println("paramsStr = " + JSONUtil.toJsonStr(params));

        // 发起请求
        try(HttpResponse response = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(params))
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .execute()
        ) {
            System.out.println("response = " + response.body());
            return yunappUtil.getResponseJSONArray(response.body());
        } catch (Exception e) {
            throw new CustException(e.getMessage());
        }
    }

    /**
     * 实例截图结果查询
     * https://yunapp.baidu.com/console/bgs/doc/?d=bgs-phone&p=PHONE_API_base_screenshot_info
     */
    @Override
    public JSONArray screenshotInfo(List<Long> task_ids) {
        // 签名
        YuappSign signEntity = yunappUtil.getSign();

        // 接口参数
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("taskIds", task_ids);
        params.put("createTime", signEntity.getNonce());
        params.put("msg", yunappUtil.getMsg(JSONUtil.toJsonStr(params), signEntity.getNonce()));

        String url = "https://platform.armvm.com/command/pad/screenshot-info.html" + signEntity.toParams();
        System.out.println("url = " + url);
        System.out.println("params = " + params);

        // 发起请求
        try(HttpResponse response = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(params))
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .execute()
        ) {
            System.out.println("response = " + response.body());
            return yunappUtil.getResponseJSONArray(response.body());
        } catch (Exception e) {
            throw new CustException(e.getMessage());
        }
    }


    /**
     * 应用启停
     * https://yunapp.baidu.com/console/bgs/doc/?d=bgs-phone&p=PHONE_API_base_app_start_and_stop
     */
    @Override
    public JSONArray appOperate(AppOperateParams appOperateParams) {
        // 签名
        YuappSign signEntity = yunappUtil.getSign();

        // 接口参数
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("padCodes", appOperateParams.getPad_codes());
        params.put("operateType", appOperateParams.getOperate_type());
        params.put("packageName", appOperateParams.getPackage_name());
        params.put("createTime", signEntity.getNonce());
        params.put("msg", yunappUtil.getMsg(JSONUtil.toJsonStr(params), signEntity.getNonce()));

        String url = "https://platform.armvm.com/command/apps/app-operate.html" + signEntity.toParams();
        System.out.println("url = " + url);
        System.out.println("params = " + params);
        System.out.println("paramsStr = " + JSONUtil.toJsonStr(params));

        // 发起请求
        try(HttpResponse response = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(params))
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .execute()
        ) {
            System.out.println("response = " + response.body());
            return yunappUtil.getResponseJSONArray(response.body());
        } catch (Exception e) {
            throw new CustException(e.getMessage());
        }
    }

}
