/*
 * 文 件 名:  SquareLightCtlTest
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/9/15
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.test;

import com.alibaba.fastjson2.JSONObject;
import com.huashe.common.domain.JsonEntity;
import com.ruoyi.ems.EmsDevAdpApplication;
import com.ruoyi.ems.config.SquareLightCtlConfig;
import com.ruoyi.ems.core.SquareLightCtlTemplate;
import com.ruoyi.ems.handle.SquareLightCtlHandler;
import com.ruoyi.ems.model.CallData;
import com.ruoyi.ems.model.slightctl.AppLoginRes;
import com.ruoyi.ems.model.slightctl.DeviceInfo;
import com.ruoyi.ems.model.slightctl.SlcDevice;
import com.ruoyi.ems.model.slightctl.SlcProject;
import com.ruoyi.ems.model.slightctl.SlcProjectSubset;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 广场灯控测试
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/9/15]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EmsDevAdpApplication.class)
public class SquareLightCtlTest {
    @Autowired
    private SquareLightCtlConfig config;

    @Test
    public void testAppLogin() {
        SquareLightCtlTemplate template = new SquareLightCtlTemplate(config.getUrl());
        AppLoginRes res = template.appLogin(config);
        log.info("login res: {}", res.accessToken);
        Assert.assertNotNull(res);
    }

    @Test
    public void testGetProjectList() {
        SquareLightCtlTemplate template = new SquareLightCtlTemplate(config.getUrl());
        CallData<SlcProject> callData = template.getProjectList();
        log.info("ResPayload: {}", callData.getResPayload());
        Assert.assertNotNull(callData.getResPayload());
    }

    @Test
    public void testGetProjectSubsetList() {
        SquareLightCtlTemplate template = new SquareLightCtlTemplate(config.getUrl());
        CallData<SlcProjectSubset> callData = template.getProjectSubsetList("1948750398922747906");
        log.info("ProjectSubsetList: {}", callData.getCallPayload());
        Assert.assertNotNull(callData.getResPayload());
    }

    @Test
    public void testGetDeviceList() {
        SquareLightCtlTemplate template = new SquareLightCtlTemplate(config.getUrl());
        CallData<SlcDevice> callData = template.getDeviceList("1948750398922747906", "1948750399027605506");
        log.info("DeviceList: {}", callData.getCallPayload());
        Assert.assertNotNull(callData.getCallPayload());
    }

    @Test
    public void testGetDeviceInfo() {
        SquareLightCtlTemplate template = new SquareLightCtlTemplate(config.getUrl());
        CallData<DeviceInfo> callData = template.getDeviceInfo("1948750398922747906", "1954717047404425217");
        log.info("DeviceInfo: {}", JSONObject.toJSONString(callData.getData()));
        Assert.assertNotNull(callData.getData());
    }

    @Test
    public void testLightControl() {
        SquareLightCtlTemplate template = new SquareLightCtlTemplate(config.getUrl());
        JsonEntity.ObjBuilder lampListObj = JsonEntity.objBuilder().putKv("lampNo", "1").putKv("lampOnOff", 1)
            .putKv("brightness", 0);

        JsonEntity.ObjBuilder json = JsonEntity.objBuilder().addKeyArray("deviceIds", "1954717047404425217")
            .addKeyArray("lampList", lampListObj.build().getJsonObj());

        CallData<Void> callData = template.lightControl("1948750398922747906", json.build().getJsonObj());
        log.info("lightControl: {}", callData.getResPayload());
        Assert.assertNotNull(callData.getResPayload());
    }

    @Test
    public void testControlBrightness() {
        SquareLightCtlTemplate template = new SquareLightCtlTemplate(config.getUrl());

        JsonEntity.ObjBuilder lampListObj = JsonEntity.objBuilder().putKv("lampNo", "1").putKv("brightness", 25);
        JsonEntity.ObjBuilder json = JsonEntity.objBuilder().addKeyArray("deviceIds", "1954717047404425217")
            .addKeyArray("lampList", lampListObj.build().getJsonObj());

        CallData<Void> callData = template.controlBrightness("1948750398922747906", json.build().getJsonObj());
        log.info("controlBrightness: {}", callData.getResPayload());
        Assert.assertNotNull(callData.getResPayload());
    }

    @Autowired
    private SquareLightCtlHandler handler;

    @Test
    public void testSyncDeviceList() {
        handler.execGetDeviceList();
    }
}