package cooper

import (
	"encoding/json"
	"errors"
	"net/url"
)

//单条查询灯路配置
func (that *CooperClient) NewUsrK3LightLineGetRequest() *UsrK3LightLineGetRequest {
	return &UsrK3LightLineGetRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/usrK3/lightLineGet",
		},
	}
}

//UsrK3LightLineGetRequest ...
type UsrK3LightLineGetRequest struct {
	Request
}

//LineId
func (that *UsrK3LightLineGetRequest) SetLineId(value int64) *UsrK3LightLineGetRequest {
	that.initParams()
	that.params.Set("line_id", ToString(value))
	return that
}

//Params ...
func (that *UsrK3LightLineGetRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.publicParamsSlice != nil {
		for _, _v := range that.publicParamsSlice {
			if _params.Get(_v) == "" {
				return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
			}
		}
	}
	return _params, nil
}

type UsrK3LightLineGetResponse struct {
	LineId             int64  `json:"line_id"`                                  //线路id
	LineName           string `json:"line_name"`                                    //线路名称
	DeviceId           int64  `json:"device_id"`                                   //串口设备id
	DeviceType         int64  `json:"device_type"`                                    //串口设备类型
	TurnOnOrder        string `json:"turn_on_order"`                    //开灯指令(用户编辑的部分指令)
	TurnOnOrderAction  string `json:"turn_on_order_action"`                   //开灯指令完整的指令
	TurnOffOrder       string `json:"turn_off_order"`                   //关灯指令(用户编辑的部分指令)
	TurnOffOrderAction string `json:"turn_off_order_action"`                  //关灯指令完整的指令
	Identifier         string `json:"identifier"`                                    //识别符
	IsEnable           int64  `json:"is_enable"`                //开启状态（1 开启 0 关闭）
	UTime      		   int64  `json:"u_time"`                        //
	CTime              int64  `json:"c_time"`                        //
	CTimeStr   string `json:"c_time_str"`                        //
	UTimeStr   string `json:"u_time_str"`                        //
}

//Do ...
func (that *UsrK3LightLineGetRequest) Do() (*UsrK3LightLineGetResponse, error) {
	//返回data
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_str, _err := json.Marshal(_data)
	if _err != nil {
		return nil, errors.New(that.apiAddress + "Marshal failed!" + _err.Error())
	}
	//返回结果
	_rs := new(UsrK3LightLineGetResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + "Unmarshal failed!" + _err.Error())
	}
	return _rs, nil
}
