package com.ics.common.utils.device;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public class WisDomDeviceUtils {

    private static final String appId = "10001415";
    private static final String appKey = "af08e3de-1670-4f80-9844-152cc7bb5e8b";


    /**
     * 获取设备唯一标识
     *
     * @return
     */
    public static String getDeviceId() {

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("pageNo",0);
        jsonObject.put("pageSize",10);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("appId",appId);
        heads.put("appKey",appKey );

        String result = HttpRequest.post("https://iot.ikonke.com:4432/2.0/developer/ccus")
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(30*1000).execute().body();


        JSONObject jsonObject1 = JSON.parseObject(result);
        JSONArray data = jsonObject1.getJSONArray("data");

        System.out.println(data.getJSONObject(0).getString("deviceId"));
        System.out.println(data.getJSONObject(0).getString("id"));
        System.out.println(data.getJSONObject(0).getString("productId"));

        return "123456";
    }



    public static JSONObject getDeviceStatus(String deviceId,String type){

        JSONObject on =new JSONObject();
        on.put("id",deviceId);
        on.put("type",type);


        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("appId",appId);
        heads.put("appKey",appKey );

        String result = HttpRequest.post("https://iot.ikonke.com:4432/1.0/app/ccu/ef85e561-512d-4e9c-befe-ee4b0d9c2414/deviceStatus")
                .headerMap(heads, false)
                .body(on.toString()).timeout(30*1000).execute().body();


        JSONObject jsonObject1 = JSON.parseObject(result);
        JSONObject o = (JSONObject) jsonObject1.get("status");
        JSONObject jsonObject = JSON.parseObject(String.valueOf(o));

        System.out.println(jsonObject);
        return jsonObject;

    }



    /**
     * 设备通用操作 (开关等控制器)
     */
    public static Boolean deviceControl(String deviceId,boolean isOn){
        JSONObject jsonObject = new JSONObject();

        JSONObject on =new JSONObject();
        on.put("on",isOn);
        jsonObject.put("actionArg",on);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("appId",appId);
        heads.put("appKey",appKey );

        String result = HttpRequest.post("https://iot.ikonke.com:4432/2.0/ccu/CCU_288340/device/"+deviceId+"/opt?realType=KONKE_ZIGBEE_CHOPIN_LIGHT_PROJECT&action=SwitchOpt")
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(30*1000).execute().body();

        JSONObject jsonObject1 = JSON.parseObject(result);
        Boolean success = Boolean.valueOf(jsonObject1.getString("success"));
        log.info("灯控接口返回结果：{}",jsonObject1);
        log.info(DateUtil.date()+" -------灯控接口结果：{}",success);

        return success;
    }


    /**
     *调光灯接口
     */
    public static Boolean dimmingControl(String deviceId,int bri,int colourTemperature){
        JSONObject jsonObject = new JSONObject();

        JSONObject on =new JSONObject();
        on.put("bri",bri);
        on.put("colour_temperature",colourTemperature);
        jsonObject.put("actionArg",on);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("appId",appId);
        heads.put("appKey",appKey );

        String result = HttpRequest.post("https://iot.ikonke.com:4432/2.0/ccu/CCU_288340/device/"+deviceId+"/opt?realType=KONKE_ZIGBEE_Z3S_GALAXY_DIMMER_LIGHT&action=SetDimmableLightMode")
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(30*1000).execute().body();

        JSONObject jsonObject1 = JSON.parseObject(result);
        Boolean success = Boolean.valueOf(jsonObject1.getString("success"));
        log.info("调光灯接口返回结果：{}",jsonObject1);

        log.info(DateUtil.date()+" -------调光灯接口结果：{}",success);

        return success;
    }

    //调光灯，亮度调节
//    public static Boolean dimmingLightControl(String deviceId,String mode,boolean on){
//        JSONObject jsonObject = new JSONObject();
//
//        JSONObject on1 =new JSONObject();
//        on1.put("mode",mode);
//        on1.put("on",on);
//        jsonObject.put("actionArg",on1);
//        Map<String, String > heads = new HashMap<>();
//        heads.put("Content-Type", "application/json;charset=UTF-8");
//        heads.put("appId",appId);
//        heads.put("appKey",appKey );
//
//        String result = HttpRequest.post("https://iot.ikonke.com:4432/2.0/ccu/CCU_288340/device/"+deviceId+"/opt?realType=KONKE_ZIGBEE_Z3S_GALAXY_DIMMER_LIGHT&action=SetKLightMode")
//                .headerMap(heads, false)
//                .body(jsonObject.toString()).timeout(30*1000).execute().body();
//
//        JSONObject jsonObject1 = JSON.parseObject(result);
//        Boolean success = Boolean.valueOf(jsonObject1.getString("success"));
//        return success;
//    }


    /**
     *  情景面板接口
     */
    public static Boolean shortControl(String deviceId,int buttonId){
        JSONObject jsonObject = new JSONObject();

        JSONObject on =new JSONObject();
        on.put("buttonId",buttonId);
        jsonObject.put("actionArg",on);

        Map<String, String > heads = new HashMap    <>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("appId",appId);
        heads.put("appKey",appKey );

        String result = HttpRequest.post("https://iot.ikonke.com:4432/2.0/ccu/CCU_288340/device/"+deviceId+"/opt?realType=KONKE_ZIGBEE_CHOPIN_SHORTCUTPANEL_PROJECT&action=ActiveShortcutPanel")
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(30*1000).execute().body();

        JSONObject jsonObject1 = JSON.parseObject(result);
        Boolean success = Boolean.valueOf(jsonObject1.getString("success"));
        log.info("情景面板接口返回结果：{}",jsonObject1);

        log.info(DateUtil.date()+" -------情景面板接口结果：{}",success);







        return success;
    }

    /**
     * 窗帘接口
     */
    public static Boolean curtainControl(String deviceId,String opt){
        JSONObject jsonObject = new JSONObject();

        JSONObject on =new JSONObject();
        on.put("opt",opt);
        jsonObject.put("actionArg",on);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("appId",appId);
        heads.put("appKey",appKey );

        String result = HttpRequest.post("https://iot.ikonke.com:4432/2.0/ccu/CCU_288340/device/"+deviceId+"/opt?realType=KONKE_ZIGBEE_CHOPIN_CURTAIN_CUSTOM&action=MotorOpt")
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(30*1000).execute().body();

        JSONObject jsonObject1 = JSON.parseObject(result);
        Boolean success = Boolean.valueOf(jsonObject1.getString("success"));
        log.info("窗帘控制接口返回结果：{}",jsonObject1);

        log.info(DateUtil.date()+" -------调用窗帘控制结果：{}",success);
        return success;
    }


    /**
     * 插座接口
     * @param deviceId
     * @param status
     * @return
     */
    public static Boolean socketControl(String deviceId, Boolean status) {
        JSONObject jsonObject = new JSONObject();

        JSONObject on =new JSONObject();
        on.put("on",status);
        jsonObject.put("actionArg",on);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("appId",appId);
        heads.put("appKey",appKey );

        String result = HttpRequest.post("https://iot.ikonke.com:4432/2.0/ccu/CCU_288340/device/"+deviceId+"/opt?realType=KONKE_ZIGBEE_CHOPIN_10A_SOCKET&action=SwitchOpt")
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(30*1000).execute().body();

        JSONObject jsonObject1 = JSON.parseObject(result);
        Boolean success = Boolean.valueOf(jsonObject1.getString("success"));

        log.info("插座接口返回结果：{}",jsonObject1);
        log.info(DateUtil.date()+" -------插座接口结果：{}",success);

        return success;
    }

    public static Boolean dimmingOpenControl(String deviceId, Boolean status) {
        JSONObject jsonObject = new JSONObject();

        JSONObject on =new JSONObject();
        on.put("on",status);
        jsonObject.put("actionArg",on);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("appId",appId);
        heads.put("appKey",appKey );

        String result = HttpRequest.post("https://iot.ikonke.com:4432/2.0/ccu/CCU_288340/device/"+deviceId+"/opt?realType=KONKE_ZIGBEE_Z3S_GALAXY_DIMMER_LIGHT&action=SwitchOpt")
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(30*1000).execute().body();

        JSONObject jsonObject1 = JSON.parseObject(result);
        Boolean success = Boolean.valueOf(jsonObject1.getString("success"));

        log.info("调光灯接口返回结果：{}",jsonObject1);
        log.info(DateUtil.date()+" -------调光灯接口结果：{}",success);

        return success;
    }



    /**
     * 卷帘开启
     */
    public static Boolean openCover(String deviceId, String status) {

        JSONObject on =new JSONObject();
        on.put("device_id",deviceId);

        Map<String, String > heads = new HashMap<>();
        heads.put("Authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI1ZTg3OGUyMmQ3NDc0ZTM4YTFhMmY5ODQzZDcxM2FhOCIsImlhdCI6MTcyMjYwMjU1NCwiZXhwIjoyMDM3OTYyNTU0fQ.Wjo9F0FSbFiVySwFzyHmKwt5ncneis-oPO6sPSffUJg");
        heads.put("Content-Type", "application/json;charset=UTF-8");
        HttpRequest.post("http://222.184.49.22:8123/api/services/cover/"+status)
                .headerMap(heads,false)
                .body(on.toString()).timeout(30*1000).executeAsync();

        return false;
    }

    public static String openControlLock(String ip) {
        String url = "http://222.67.48.244:1234/api/Op/OpDoor?ipaddress="+ip;
        log.info("开门控制器的url:{}", "222.67.48.244");
        String msg = HttpUtil.get(url);
        log.info("开门控制器的结果为结果:{}", msg);
        return msg;
    }

    public static void main(String[] args) {
        openControlLock("192.168.1.34");
    }



}
