package main

import (
	"github.com/go-martini/martini"
	"github.com/martini-contrib/render"
	"net/http"
	"sparrow/pkg/rpcs"
	"sparrow/pkg/server"
	"strconv"
)

// GetDeviceCommands 查询设备指令列表（通过设备类型id）
func GetDeviceCommands(req *http.Request, r render.Render) {
	deviceTypeId := req.URL.Query().Get("device_type_id")
	if deviceTypeId == "" {
		r.JSON(http.StatusOK, renderError(ErrWrongQueryFormat, errBadRequestString))
		return
	}
	pi, _ := strconv.Atoi(req.URL.Query().Get("pi"))
	ps, _ := strconv.Atoi(req.URL.Query().Get("ps"))
	if pi <= 0 {
		pi = 1
	}
	if ps <= 0 {
		ps = 20
	}
	name := req.URL.Query().Get("name")

	args := &rpcs.ArgsDeviceCommandQuery{
		DeviceTypeId: deviceTypeId,
		Pi:           pi,
		Ps:           ps,
		Name:         name,
	}
	reply := &rpcs.ReplyDeviceCommandList{}
	err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.GetDeviceCommands", args, reply)
	if err != nil {
		server.Log.Errorf("GetDeviceCommands error: %v", err)
		r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
		return
	}
	r.JSON(http.StatusOK, done(map[string]interface{}{
		"list":  reply.List,
		"total": reply.Total,
	}))
}

// GetDeviceStatusList 查询设备状态列表（通过设备类型id）
func GetDeviceStatusList(req *http.Request, r render.Render) {
	deviceTypeId := req.URL.Query().Get("device_type_id")
	if deviceTypeId == "" {
		r.JSON(http.StatusOK, renderError(ErrWrongQueryFormat, errBadRequestString))
		return
	}
	pi, _ := strconv.Atoi(req.URL.Query().Get("pi"))
	ps, _ := strconv.Atoi(req.URL.Query().Get("ps"))
	if pi <= 0 {
		pi = 1
	}
	if ps <= 0 {
		ps = 20
	}
	name := req.URL.Query().Get("name")

	args := &rpcs.ArgsDeviceStatusQuery{
		DeviceTypeId: deviceTypeId,
		Pi:           pi,
		Ps:           ps,
		Name:         name,
	}
	reply := &rpcs.ReplyDeviceStatusList{}
	err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.GetDeviceStatusList", args, reply)
	if err != nil {
		server.Log.Errorf("GetDeviceStatusList error: %v", err)
		r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
		return
	}
	r.JSON(http.StatusOK, done(map[string]interface{}{
		"list":  reply.List,
		"total": reply.Total,
	}))
}

// GetSceneHis 分页查询场景执行历史
func GetSceneHis(req *http.Request, r render.Render) {
	pi, _ := strconv.Atoi(req.URL.Query().Get("pi"))
	ps, _ := strconv.Atoi(req.URL.Query().Get("ps"))
	if pi <= 0 {
		pi = 1
	}
	if ps <= 0 {
		ps = 20
	}
	sceneId := req.URL.Query().Get("scene_id")

	args := &rpcs.ArgsSceneHisQuery{
		SceneId: sceneId,
		Pi:      pi,
		Ps:      ps,
	}
	reply := &rpcs.ReplySceneHisList{}
	err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.GetSceneHis", args, reply)
	if err != nil {
		server.Log.Errorf("GetSceneHis error: %v", err)
		r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
		return
	}
	r.JSON(http.StatusOK, done(map[string]interface{}{
		"list":  reply.List,
		"total": reply.Total,
	}))
}

// GetSceneHisBySceneId 按场景ID分页查询执行历史
func GetSceneHisBySceneId(params martini.Params, req *http.Request, r render.Render) {
	sceneId := params["scene_id"]
	if sceneId == "" {
		r.JSON(http.StatusOK, renderError(ErrWrongQueryFormat, errBadRequestString))
		return
	}

	pi, _ := strconv.Atoi(req.URL.Query().Get("pi"))
	ps, _ := strconv.Atoi(req.URL.Query().Get("ps"))
	if pi <= 0 {
		pi = 1
	}
	if ps <= 0 {
		ps = 20
	}

	args := &rpcs.ArgsSceneHisQuery{
		SceneId: sceneId,
		Pi:      pi,
		Ps:      ps,
	}
	reply := &rpcs.ReplySceneHisList{}
	err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.GetSceneHisBySceneId", args, reply)
	if err != nil {
		server.Log.Errorf("GetSceneHisBySceneId error: %v", err)
		r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
		return
	}
	r.JSON(http.StatusOK, done(map[string]interface{}{
		"list":  reply.List,
		"total": reply.Total,
	}))
}
