package com.yuchen.saas.park.service.service.impl;

import com.yuchen.saas.park.api.vo.responsevo.ResponseVo;
import com.yuchen.saas.park.service.iot.IotClientService;
import com.yuchen.saas.park.service.service.LampControlService;
import com.yuchen.saas.park.service.util.iot.client.ResourceUtils;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

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

/**
 * 灯光控制
 */
@Service
public class LampControlServiceImpl implements LampControlService {
    @Resource
    IotClientService iotClientService;
    @Override
    public ResponseVo openOrClose(Map<String, Object> body) {
        ResponseVo responseVo = new ResponseVo();
        if(Func.isEmpty(body)) {
            responseVo.setCode("1");
            responseVo.setData("请求参数为空！");
            return responseVo;
        }

        // 获取单灯列表
        List<Map<String, Object>> lampList = new ArrayList<Map<String, Object>>();
        lampList = (List<Map<String, Object>>)body.get("lampList");
        // 获取产品key
        String productKey = (String)body.get("productKey");
        if(Func.isNotEmpty(lampList) && lampList.size() > 0) {
            for(Map<String, Object> lamp : lampList) {
                Map<String, Object> openLamp = new HashMap<String, Object>();
                openLamp.put("identify", "1".equals(String.valueOf(lamp.get("switchState")))
                        ? ResourceUtils.ACTION_OPEN_LIGHT:ResourceUtils.ACTION_CLOSE_LIGHT);
                openLamp.put("productKey", productKey);
                openLamp.put("deviceName", lamp.get("deviceName"));

                Map<String, Object> params = new HashMap<String, Object>();
                params.put("delay", lamp.get("delay"));

                openLamp.put("params", params);
                System.out.println("openLamp:" + openLamp);
                responseVo = iotClientService.serviceCall(openLamp);
            }
        }
        return responseVo;
    }
}
