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"
	"chain33-pai/pkg/logging"
)


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 pai_service.ReqTicketInfo
	err := c.ShouldBindJSON(&req)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
	}
	logging.Info("req:",req)
	if req.Detail {
		if req.Page == 0 {
			req.Page = int32(1)
		}
		if req.Pagesize == 0 {
			req.Pagesize = int32(10)
		}
	}
	var pai pai_service.Pai
	info,err := pai.GetTicketInfo(&req)
	if err != nil {
		logging.Error("GetTicketInfo err",err)
		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 pai_service.ReqMineTime


	err := c.ShouldBindJSON(&req)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
	}
	logging.Info("req:",req)
	var stat stat_service.RaspMinerStat
	err = stat.SetMineTime(req.Addr,req.Flag)
	if err != nil {
		logging.Error("GetTicketInfo err",err)
		appG.Response(http.StatusOK, e.ERROR, nil)
	}
	appG.Response(http.StatusOK, e.SUCCESS, nil)
}