package v1

import (
	"chain33-pai/models"
	"chain33-pai/pkg/app"
	"chain33-pai/pkg/e"
	"chain33-pai/pkg/logging"
	"chain33-pai/service/pai_service"
	"chain33-pai/service/stat_service"
	"chain33-pai/service/version_service"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"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)
		return
	}
	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)
		return
	}
	appG.Response(http.StatusOK, e.SUCCESS, pai)
}

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

	var req models.ReqTicketInfo
	err := c.ShouldBindJSON(&req)
	if err != nil {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
		return
	}
	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)
		return
	}
	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.INVALID_PARAMS, nil)
		return
	}
	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)
		return
	}
	appG.Response(http.StatusOK, e.SUCCESS, nil)
}

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

	var req pai_service.ReqVersion

	err := c.ShouldBindJSON(&req)
	if err != nil {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
		return
	}
	var v version_service.RaspVersion
	version,err := v.GetLatestVersion(req.Type)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
		return
	}
	appG.Response(http.StatusOK, e.SUCCESS, version)
}


var KP map[string]string

func IsComplete(c *gin.Context){
	appG:=app.Gin{C:c}
	err:=appG.C.ShouldBindJSON(&KP)
	if err!=nil{
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
		return
	}
	if KP["port"]!="" {
		if models.IsComplete(KP["port"]) {
			appG.Response(http.StatusOK, e.SUCCESS, gin.H{
				"status": "ok",
			})
			return
		}
		appG.Response(http.StatusOK, e.UNDONE, nil)
		return
	}
	appG.Response(http.StatusOK,e.ERROR,"参数错误")
}

func FeedBack(c *gin.Context){
	appG:=app.Gin{C:c}
	err:=appG.C.ShouldBindJSON(&KP)
	if err!=nil{
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
		return
	}
	app.AppendKeyPair(KP["public"])
	//fmt.Println(KP["public"])
	port,daemonPort,err:=app.GennerPort()
	if err!=nil{
		appG.C.JSON(http.StatusOK,gin.H{
			"nsg":"fail",
			"code":"500",
			"port":fmt.Sprint(port),
			"daemon":fmt.Sprint(daemonPort),
		})
		log.Println(err)
		return
	}
	models.AddFeedBack(models.RaspFeedback{
		Id:	fmt.Sprint(port),
		Addr:KP["addr"],
		Description: KP["description"],
		Complete:    false,
	})
	models.AddFeedBackList(models.RaspFeedbackList{
		Addr:KP["addr"],
		Description: KP["description"],
		Addtime:time.Now().Unix(),
	})
	appG.C.JSON(http.StatusOK,gin.H{
		"msg":"ok",
		"code":"200",
		"port":fmt.Sprint(port),
		"daemon":fmt.Sprint(daemonPort),
	})
}

func GetFeedList(c *gin.Context){
	appG:=app.Gin{C:c}
	var req models.ReqRaspFeedbackList
	err:=appG.C.ShouldBindJSON(&req)
	if err!=nil{
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
		return
	}
	list,err := models.GetAddrFeedBackList(&models.ReqRaspFeedbackList{
		Addr : req.Addr,
		Pagesize:req.Pagesize,
		Page:req.Page,
	})
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
		return
	}
	count := models.GetAddrFeedBackCount(req.Addr)

	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, nil)
		return
	}
	appG.Response(http.StatusOK, e.SUCCESS, gin.H{
		"list" :list,
		"count":count,
	})
}
