/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		unphp <unphp@qq.com>
 * @date		2018-12-18
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */
package cooper

import (
	"errors"
	"fmt"
	"net/url"
)

//NewUidGet ...
func (that *CooperClient) NewDevice() *DeviceRequest {
	return &DeviceRequest{
		Client: that,
	}
}

type DeviceReturn struct {
	Uid []int64
}

//UidRequest 唯一id生成器
type DeviceRequest struct {
	Client     *CooperClient
	DevType    int64  //设备类型
	DevId      string //设备ID
	DevStatus  int64  //设备状态 在线 离线
	DevEnable  int64  //设备状态 有效 无效
	DevCmd     string //设备指令
	Id         int64
	apiAddress string
	DevIds     string //多个设备ID
}

//AcCompanyListResponse ...
type DeviceResponse struct {
	AppId         int64  `json:"app_id"`     //
	DevType       string `json:"dev_type"`   //
	DevId         string `json:"dev_id"`   //
	DevStatus     string `json:"dev_status"`        //
	DevEnable     string `json:"dev_enable"`      //
	DevCmd        int64  `json:"dev_cmd"`    //
}

func (that *DeviceRequest) Add() *DeviceRequest {
	that.apiAddress = "/device/add"
	return that
}

func (that *DeviceRequest) Edit() *DeviceRequest {
	that.apiAddress = "/device/edit"
	return that
}

func (that *DeviceRequest) Get() *DeviceRequest {
	that.apiAddress = "/device/get"
	return that
}

func (that *DeviceRequest) ListByDevIds() *DeviceRequest {
	that.apiAddress = "/device/listByDevIds"
	return that
}

func (that *DeviceRequest) ListByAppId() *DeviceRequest {
	that.apiAddress = "/device/listByAppId"
	return that
}

func (that *DeviceRequest) SetApiAddress(apiAddress string) *DeviceRequest {
	that.apiAddress = apiAddress
	return that
}

func (that *DeviceRequest) SetDevType(devType int64) *DeviceRequest {
	that.DevType = devType
	return that
}

func (that *DeviceRequest) SetDevId(devId string) *DeviceRequest {
	that.DevId = devId
	return that
}

func (that *DeviceRequest) SetDevStatus(devStatus int64) *DeviceRequest {
	that.DevStatus = devStatus
	return that
}

func (that *DeviceRequest) SetDevEnable(devEnable int64) *DeviceRequest {
	that.DevEnable = devEnable
	return that
}

func (that *DeviceRequest) SetDevCmd(devCmd string) *DeviceRequest {
	that.DevCmd = devCmd
	return that
}

func (that *DeviceRequest) SetId(id int64) *DeviceRequest {
	that.Id = id
	return that
}

func (that *DeviceRequest) SetDevIds(devIds string) *DeviceRequest {
	that.DevIds = devIds
	return that
}

//Params ...
func (that *DeviceRequest) Params() (url.Values,error) {
	var m = make(url.Values)
	m.Set("dev_type", ToString(that.DevType))
	m.Set("dev_id", ToString(that.DevId))
	m.Set("dev_status", ToString(that.DevStatus))
	m.Set("dev_enable", ToString(that.DevEnable))
	m.Set("dev_cmd", ToString(that.DevCmd))
	m.Set("id", ToString(that.Id))
	return m,nil
}

//Do ...
func (that *DeviceRequest) ListByAppIdDo() ([]map[string]interface{}, error) {
	//
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}

	_info := _data.([]interface{})
	//返回结果
	var _rs  []map[string]interface{}
	for _, _v := range _info {
		_vData, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确2")
		}
		_rs = append(_rs, _vData)
	}

	return _rs, nil

}

func (that *DeviceRequest) Do() (map[string]interface{}, error) {
	//
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return make(map[string]interface{}), _err
	}

	_res, _ok := _data.(map[string]interface{})
	if !_ok {
		return make(map[string]interface{}), _err
	}
	fmt.Println(_res)
	return _res, nil

}
