package net.northking.cctp.upperComputer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.upperComputer.service.DebuggerService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/element")
@Api(tags = "上位机获取元素控制器")
public class  ElementController {

    @Resource(name = "androidService")
    private DebuggerService androidService;

    @Resource(name = "iosService")
    private DebuggerService iosService;

    @ApiOperation(value = "获取UI树")
    @RequestMapping(value = "/getUiTree",method = RequestMethod.GET)
    public Map<String, Object> getUiTree(@RequestParam("deviceId")String deviceId,@RequestParam("type") String type){
        Map<String, Object> map = new HashMap<>();
        Object xml = null;
        if ("android".equals(type)) {
            xml = androidService.getUiTree(deviceId);
        } else if ("ios".equals(type)) {
            xml = iosService.getUiTree(deviceId);
        }
        if ("appium获取xml失败".equals(xml)) {
            map.put("code", "000001");
            map.put("data", "appium获取xml失败");
            map.put("success", false);
            map.put("message", "appium获取xml失败");
            map.put("timestamp", System.currentTimeMillis());
        } else {
            map.put("code", "000000");
            map.put("data", xml);
            map.put("success", true);
            map.put("message", "接口调用成功");
            map.put("timestamp", System.currentTimeMillis());
        }
        return map;
    }
}
