package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"chain33-pai/pkg/app"
	"chain33-pai/pkg/e"
	"chain33-pai/service/pai_service"
	"chain33-pai/service/stat_service"
)

type ReqTicketInfo struct {
	Addr string `json:"addr" binding:"requried"`
	Page int32 `json:"page"`
	Pageszie int32 `json:"pageszie"`
	Detail bool `json:"detail"`
}

type ReqMineTime struct {
	Addr string `json:"addr" binding:"requried"`
	StartTime int64 `json:"start_time"`
	EndTime int64 `json:"end_time"`
}

func GetDevdetail(c *gin.Context) {
	appG := app.Gin{C: c}
	var pai pai_service.Pai
	ok := pai.GetConfig()
	if !ok {
		appG.Response(http.StatusOK, e.ERROR, nil)
	}
	appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
		"serial": pai.Serial,
	})
}


func GetDevstatus(c *gin.Context) {
	appG := app.Gin{C: c}
	var pai pai_service.Pai
	err := pai.GetDevstatus()
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
	}
	appG.Response(http.StatusOK, e.SUCCESS, pai)
}

func GetTicketInfo(c *gin.Context) {
	appG := app.Gin{C: c}

	var req ReqTicketInfo
	c.BindJSON(&req)
	if req.Detail {
		if req.Page == 0 {
			req.Page = int32(1)
		}
		if req.Pageszie == 0 {
			req.Pageszie = int32(10)
		}
	}
	var pai pai_service.Pai
	info,err := pai.GetTicketInfo(&req)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
	}
	appG.Response(http.StatusOK, e.SUCCESS, info)
}

func RecordMineTime(c *gin.Context) {
	appG := app.Gin{C: c}

	var req ReqMineTime
	err := c.BindJSON(&req)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
	}
	var stat stat_service.RaspMinerStat
	err = stat.SetMineTime(req.Addr,req.StartTime,req.EndTime)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
	}
	appG.Response(http.StatusOK, e.SUCCESS, nil)
}