package com.fjhx.utils;

import cn.hutool.extra.spring.SpringUtil;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.*;
import lombok.Data;
import org.springblade.core.log.exception.ServiceException;

import java.util.Collections;

public class IoTDAUtil {

    private static final IoTDAClient client = SpringUtil.getBean(IoTDAClient.class);

    /**
     * 创建资源空间
     *
     * @param appName 资源空间名称
     * @return 资源空间id
     */
    public static String addApplication(String appName) {
        AddApplicationRequest addApplicationRequest = new AddApplicationRequest();
        AddApplication addApplication = new AddApplication();
        addApplication.setAppName(appName);
        addApplicationRequest.setBody(addApplication);
        AddApplicationResponse addApplicationResponse = client.addApplication(addApplicationRequest);
        return addApplicationResponse.getAppId();
    }

    /**
     * 删除资源空间
     *
     * @param appId 资源空间Id
     */
    public static void deleteApplication(String appId) {
        try {
            DeleteApplicationRequest deleteApplicationRequest = new DeleteApplicationRequest();
            deleteApplicationRequest.setAppId(appId);
            client.deleteApplication(deleteApplicationRequest);
        } catch (Exception e) {
            throw new ServiceException("资源空间删除失败");
        }
    }

    /**
     * 创建产品
     *
     * @param appId            资源空间Id
     * @param name             产品名称
     * @param deviceType       设备类型
     * @param manufacturerName 厂商名称
     * @return 产品id
     */
    public static String createProduct(String appId, String name, String deviceType, String manufacturerName) {
        CreateProductRequest createProductRequest = new CreateProductRequest();
        AddProduct addProduct = new AddProduct();
        // 资源空间
        addProduct.setAppId(appId);
        // 产品名称
        addProduct.setName(name);
        addProduct.setDeviceType(deviceType);
        addProduct.setManufacturerName(manufacturerName);

        addProduct.setProtocolType("MQTT");
        addProduct.setDataFormat("json");

        // 添加产品属性
        ServiceCapability serviceCapability = new ServiceCapability();
        serviceCapability.setServiceId("Data");
        serviceCapability.setServiceType("data");
        // 添加命名空间
        ServiceProperty serviceProperty = new ServiceProperty();
        serviceProperty.setPropertyName("DeviceData");
        serviceProperty.setDataType("string");
        serviceProperty.setRequired(false);
        serviceProperty.setMaxLength(2000000);
        serviceProperty.setStep(0.0D);
        serviceProperty.setMethod("RW");
        serviceCapability.setProperties(Collections.singletonList(serviceProperty));
        addProduct.setServiceCapabilities(Collections.singletonList(serviceCapability));

        createProductRequest.setBody(addProduct);
        CreateProductResponse product = client.createProduct(createProductRequest);
        return product.getProductId();
    }

    /**
     * 修改产品
     *
     * @param productId        产品id
     * @param appId            资源空间Id
     * @param name             产品名称
     * @param deviceType       设备类型
     * @param manufacturerName 厂商名称
     */
    public static void updateProduct(String productId, String appId, String name, String deviceType, String manufacturerName) {
        UpdateProductRequest updateProductRequest = new UpdateProductRequest();
        updateProductRequest.setProductId(productId);
        UpdateProduct updateProduct = new UpdateProduct();
        updateProduct.setAppId(appId);
        updateProduct.setName(name);
        updateProduct.setDeviceType(deviceType);
        updateProduct.setManufacturerName(manufacturerName);
        updateProductRequest.setBody(updateProduct);
        client.updateProduct(updateProductRequest);
    }

    /**
     * 删除产品
     *
     * @param appId     资源空间Id
     * @param productId 产品id
     */
    public static void deleteProduct(String appId, String productId) {
        try {
            DeleteProductRequest deleteProductRequest = new DeleteProductRequest();
            deleteProductRequest.setAppId(appId);
            deleteProductRequest.setProductId(productId);
            client.deleteProduct(deleteProductRequest);
        } catch (Exception e) {
            throw new ServiceException("产品删除失败");
        }
    }

    /**
     * 添加设备
     *
     * @param nodeId     设备标识
     * @param deviceName 设备名称
     * @param appId      资源空间Id
     * @param productId  产品id
     * @return 设备secret
     */
    public static DeviceKey addDevice(String nodeId, String deviceName, String appId, String productId) {
        AddDeviceRequest addDeviceRequest = new AddDeviceRequest();
        AddDevice addDevice = new AddDevice();
        addDevice.setNodeId(nodeId);
        addDevice.setDeviceName(deviceName);
        addDevice.setAppId(appId);
        addDevice.setProductId(productId);

        addDeviceRequest.setBody(addDevice);
        AddDeviceResponse addDeviceResponse = client.addDevice(addDeviceRequest);

        DeviceKey deviceKey = new DeviceKey();
        deviceKey.setDeviceId(addDeviceResponse.getDeviceId());
        deviceKey.setSecret(addDeviceResponse.getAuthInfo().getSecret());

        return deviceKey;
    }

    @Data
    public static class DeviceKey {
        // 设备id
        private String deviceId;
        // 设备密匙
        private String secret;
    }

    /**
     * 删除设备
     *
     * @param deviceId 设备Id
     */
    public static void deleteDevice(String deviceId) {
        try {
            DeleteDeviceRequest deleteDeviceRequest = new DeleteDeviceRequest();
            deleteDeviceRequest.setDeviceId(deviceId);
            client.deleteDevice(deleteDeviceRequest);
        } catch (Exception e) {
            throw new ServiceException("产品删除失败");
        }
    }

}
