import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiuguo.iot.third.service.TuyaDeviceConnector;
import com.qiuguo.iot.user.api.IotBoxUserApiApplication;
import java.util.Arrays;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * XXX
 *
 * @author weiyachao
 * @since 2023/9/5 16:03
 */
@SpringBootTest(classes = IotBoxUserApiApplication.class)
@Slf4j
public class UserTest {
    //spring.profiles.active=dev
    public String deviceId = "6c29b49edea00eb541rg3n";
    
    public String spaceId = "163208675";
    
    @Autowired
    private TuyaDeviceConnector tuyaDeviceConnector;
    
    @Test
    public void 获取设备状态() {
    
    }
    
    @Test
    public void 查询所有分类() {
        String key = "sd";
        JSONArray aa = tuyaDeviceConnector.categories();
        for (Object o : aa) {
            JSONObject jsonObject = (JSONObject) o;
            if (Objects.equals(jsonObject.getString("code"), key)) {
                System.out.println(jsonObject.getString("name"));
                break;
            }
        }
    }
    
    @Test
    public void 转移设备() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("space_id", spaceId);
        Boolean aBoolean = tuyaDeviceConnector.transferDevice(deviceId, jsonObject);
        System.out.println(aBoolean);
    }
    
    @Test
    public void 获取设备支持的指令集() {
        Object functions = tuyaDeviceConnector.getFunctions(deviceId);
        System.out.println(functions);
    }
    
    @Test
    public void 修改空间() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "testname");
        Boolean jsonObject1 = tuyaDeviceConnector.updateSpace(spaceId, jsonObject);
        System.out.println(jsonObject1);
    }
    
    @Test
    public void 查询空间() {
        //40001900
        JSONObject spaceInfo = tuyaDeviceConnector.getSpaceInfo("163208675");
        System.out.println(spaceInfo);
    }
    
    @Test
    public void 删除空间() {
        //40001900
        Boolean jsonObject = tuyaDeviceConnector.deleteSpace("163258893");
        System.out.println(jsonObject);
    }
    
    @Test
    public void 创建空间() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "space1_2");
        jsonObject.put("description", "space1_2的空间描述");
        Long jsonObject1 = tuyaDeviceConnector.creatSpace(jsonObject);
        System.out.println(jsonObject1);
    }
    
    @Test
    public void 控制设备动作() {
        JSONObject commands = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "power");
        jsonObject.put("value", true);
        JSONObject js2 = new JSONObject();
        js2.put("code", "power");
        js2.put("value", true);
        JSONObject js3 = new JSONObject();
        js3.put("code", "bright_value_v2");
        js3.put("value", 10);
        commands.put("commands", Arrays.asList( js2));
        
        Object controlDevice = tuyaDeviceConnector.controlDevice(deviceId,commands);
        System.out.println(controlDevice);
        
    }
    
    @Test
    public void 查询空间下设备列表() {
        JSONArray devicesBySpaceIds = tuyaDeviceConnector.getDevicesBySpaceIds("163257138", 20);
        System.out.println(devicesBySpaceIds);
    }
    
    @Test
    public void 查询设备信息() {
        JSONArray byid = tuyaDeviceConnector.getByid(deviceId);
        System.out.println(byid);
    }
    
}
