// Package gateway 提供设备相关的 7 个 API 方法封装
package gateway

import (
	"context"

	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/bytedance/sonic"
)

// GetDeviceInfo 获取设备信息
// 通过卡号或设备 ID 查询设备的在线状态、信号强度、WiFi 信息等
func (c *Client) GetDeviceInfo(ctx context.Context, req *DeviceInfoReq) (*DeviceInfoResp, error) {
	if req.CardNo == "" && req.DeviceID == "" {
		return nil, errors.New(errors.CodeInvalidParam, "cardNo 和 deviceId 至少需要一个")
	}

	params := make(map[string]interface{})
	if req.CardNo != "" {
		params["cardNo"] = req.CardNo
	}
	if req.DeviceID != "" {
		params["deviceId"] = req.DeviceID
	}

	businessData := map[string]interface{}{
		"params": params,
	}

	resp, err := c.doRequest(ctx, "/device/info", businessData)
	if err != nil {
		return nil, err
	}

	var result DeviceInfoResp
	if err := sonic.Unmarshal(resp, &result); err != nil {
		return nil, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析设备信息响应失败")
	}

	return &result, nil
}

// GetSlotInfo 获取设备卡槽信息
// 查询设备的所有卡槽及其中的卡信息
func (c *Client) GetSlotInfo(ctx context.Context, req *DeviceInfoReq) (*SlotInfoResp, error) {
	if req.CardNo == "" && req.DeviceID == "" {
		return nil, errors.New(errors.CodeInvalidParam, "cardNo 和 deviceId 至少需要一个")
	}

	params := make(map[string]interface{})
	if req.CardNo != "" {
		params["cardNo"] = req.CardNo
	}
	if req.DeviceID != "" {
		params["deviceId"] = req.DeviceID
	}

	businessData := map[string]interface{}{
		"params": params,
	}

	resp, err := c.doRequest(ctx, "/device/slot-info", businessData)
	if err != nil {
		return nil, err
	}

	var result SlotInfoResp
	if err := sonic.Unmarshal(resp, &result); err != nil {
		return nil, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析卡槽信息响应失败")
	}

	return &result, nil
}

// SetSpeedLimit 设置设备限速
// 设置设备的上行和下行速率限制
func (c *Client) SetSpeedLimit(ctx context.Context, req *SpeedLimitReq) error {
	params := map[string]interface{}{
		"deviceId":      req.DeviceID,
		"uploadSpeed":   req.UploadSpeed,
		"downloadSpeed": req.DownloadSpeed,
	}
	if req.Extend != "" {
		params["extend"] = req.Extend
	}

	businessData := map[string]interface{}{
		"params": params,
	}

	_, err := c.doRequest(ctx, "/device/speed-limit", businessData)
	return err
}

// SetWiFi 设置设备 WiFi
// 设置设备的 WiFi 名称、密码和启用状态
func (c *Client) SetWiFi(ctx context.Context, req *WiFiReq) error {
	params := map[string]interface{}{
		"deviceId": req.DeviceID,
		"ssid":     req.SSID,
		"password": req.Password,
		"enabled":  req.Enabled,
	}
	if req.Extend != "" {
		params["extend"] = req.Extend
	}

	businessData := map[string]interface{}{
		"params": params,
	}

	_, err := c.doRequest(ctx, "/device/wifi", businessData)
	return err
}

// SwitchCard 设备切换卡
// 切换设备当前使用的卡到指定的目标卡
func (c *Client) SwitchCard(ctx context.Context, req *SwitchCardReq) error {
	params := map[string]interface{}{
		"deviceId":    req.DeviceID,
		"targetIccid": req.TargetICCID,
	}
	if req.Extend != "" {
		params["extend"] = req.Extend
	}

	businessData := map[string]interface{}{
		"params": params,
	}

	_, err := c.doRequest(ctx, "/device/switch-card", businessData)
	return err
}

// ResetDevice 设备恢复出厂设置
// 将设备恢复到出厂设置状态
func (c *Client) ResetDevice(ctx context.Context, req *DeviceOperationReq) error {
	params := map[string]interface{}{
		"deviceId": req.DeviceID,
	}
	if req.Extend != "" {
		params["extend"] = req.Extend
	}

	businessData := map[string]interface{}{
		"params": params,
	}

	_, err := c.doRequest(ctx, "/device/reset", businessData)
	return err
}

// RebootDevice 设备重启
// 远程重启设备
func (c *Client) RebootDevice(ctx context.Context, req *DeviceOperationReq) error {
	params := map[string]interface{}{
		"deviceId": req.DeviceID,
	}
	if req.Extend != "" {
		params["extend"] = req.Extend
	}

	businessData := map[string]interface{}{
		"params": params,
	}

	_, err := c.doRequest(ctx, "/device/reboot", businessData)
	return err
}
