package controllers

import (
	"lc/common/mqtt"
	"lc/common/protocol"
)

// 卡莱特屏幕新接口
const (
	LED_STATUS_PREFIX string = "led_stat_"  //LED实时状态
	POWER             string = "power"      //1电源状态
	VSN               string = "vsn"        //2音量
	TIME              string = "time"       //3亮度
	VOLUME            string = "volume"     //4播放节目名称
	BRIGHTNESS        string = "brightness" //5最近连接时间
)

type reqData struct {
	Codes []string    `json:"codes"` //多张sn
	Param interface{} `json:"param"` //请求参数
}

type CltledController struct {
	BaseController
}

// Act @Title 卡莱物信息屏
// @Description 卡莱物信息屏全部接口
// @Param   body controllers.reqData true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/act [post]
func (o *CltledController) Act() {
	act := o.Ctx.Input.Param(":actName")
	tenant := o.Ctx.Input.Param(":tenant")
	reqId := o.Ctx.Input.Param(":requestId")
	actList := map[string]interface{}{
		protocol.TP_LED_QUERY_INFO:        "查询屏基础信息",
		protocol.TP_LED_QUERY_PGMS:        "查询节目信息",
		protocol.TP_LED_SET_PGMS:          "发布节目",
		protocol.TP_LED_SWITCH:            "切换节目",
		protocol.TP_LED_DELETE:            "删除一个或多个节目",
		protocol.TP_LED_CLEAN:             "清除所有节目",
		protocol.TP_LED_SNAPSHOT:          "截屏",
		protocol.TP_LED_QUERY_SCHEDULE:    "获取排程",
		protocol.TP_LED_SET_SCHEDULE:      "配置排程",
		protocol.TP_LED_QUERY_POWERSTATUS: "状态",
		protocol.TP_LED_SET_CMD:           "下发远程指令",
		protocol.TP_LED_QUERY_BRIGHTCOLOR: "查询亮度色温",
		protocol.TP_LED_SET_BRIGHTNESS:    "亮度调节",
		protocol.TP_LED_QUERY_VOLUME:      "查询音量",
		protocol.TP_LED_SET_VOLUME:        "调节音量 ",
		protocol.TP_LED_QUERY_RESOLUTION:  "查询屏幕参数",
	}
	_, ok := actList[act]
	if !ok || tenant == "" {
		o.Response(Failure, "actName非法", nil)
		return
	}

	jsonStr := reqData{}
	body := o.Ctx.Input.RequestBody
	err := json.Unmarshal(body, &jsonStr)
	if err != nil {
		o.Response(Failure, "失败"+err.Error(), nil)
		return
	}
	for _, sn := range jsonStr.Codes {
		topic := getTopic(tenant, sn, act, reqId)
		err = GetMqttHandler().PublishJSON(topic, jsonStr.Param, mqtt.AtMostOnce)
	}

	if err != nil {
		o.Response(Failure, "失败"+err.Error(), nil)
		return
	}
	o.Response(Success, "成功", jsonStr)
}

func getTopic(tenant, sn, downORup, reqid string) string {
	// 生成新的 UUID
	//id := uuid.New()
	return tenant + "/" + protocol.DT_CLT_LED + "/" + sn + "/down/" + downORup + "/" + reqid
}
