package api

import (
	"fmt"
	"go-account-register/libs"
	"go-account-register/task"
	paramsTypes "go-account-register/types"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Task struct {
}

type CreateRequestParams struct {
	ID              string `form:"id" json:"id" uri:"id" xml:"id"`
	Name            string `form:"name" json:"name" uri:"name" xml:"name"`
	RunStatus       int    `form:"runStatus" json:"runStatus" uri:"runStatus" xml:"runStatus" binding:"required"`
	Type            int    `form:"type" json:"type" uri:"type" xml:"type" binding:"required"`
	AccountClassify string `form:"accountClassify" json:"accountClassify" uri:"accountClassify" xml:"accountClassify"`
	ClassifyId      []int  `form:"classifyId" json:"classifyId" uri:"classifyId" xml:"classifyId"`
	Keyword         string `form:"keyword" json:"keyword" uri:"keyword" xml:"keyword"`
	TwitterId       string `form:"twitterId" json:"twitterId" uri:"twitterId" xml:"twitterId"`
	Compensate      int    `form:"compensate" json:"compensate" uri:"compensate" xml:"compensate"`
}

type StartRequestParams struct {
	TaskId string `form:"taskId" json:"taskId" uri:"taskId" xml:"taskId" binding:"required"`
}

// 创建任务
func (*Task) Create(ctx *gin.Context) {
	// 参数
	var res *libs.ErrorInfo
	var paramsJson CreateRequestParams
	if err := ctx.ShouldBindJSON(&paramsJson); err != nil {
		res = libs.ErrorCode["ParamsError"]
		fmt.Println(err)
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": res.Code,
			"msg":  res.Msg,
			"data": res.Data,
		})
		return
	}
	var scheduler = task.GetScheduler()
	var status string
	if paramsJson.RunStatus == 0 {
		status = "stop"
	} else {
		status = "pending"
	}
	taskId := scheduler.AddTask(&paramsTypes.TaskParams{
		ID:              paramsJson.ID,
		Name:            paramsJson.Name,
		Status:          status,
		Type:            paramsJson.Type,
		AccountClassify: paramsJson.AccountClassify,
		DataClassify:    paramsJson.ClassifyId,
		Keyword:         paramsJson.Keyword,
		TwitterId:       paramsJson.TwitterId,
		Compensate:      paramsJson.Compensate,
	})
	ctx.JSON(http.StatusBadRequest, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": taskId,
	})

}

// 开始任务
func (*Task) Start(ctx *gin.Context) {
	// 参数
	var res *libs.ErrorInfo
	var paramsJson StartRequestParams
	if err := ctx.ShouldBindJSON(&paramsJson); err != nil {
		res = libs.ErrorCode["ParamsError"]
		fmt.Println(err)
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": res.Code,
			"msg":  res.Msg,
			"data": res.Data,
		})
		return
	}
}

// 暂停任务
func (*Task) Stop(ctx *gin.Context) {
	// 参数
	var res *libs.ErrorInfo
	var paramsJson StartRequestParams
	if err := ctx.ShouldBindJSON(&paramsJson); err != nil {
		res = libs.ErrorCode["ParamsError"]
		fmt.Println(err)
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": res.Code,
			"msg":  res.Msg,
			"data": res.Data,
		})
		return
	}
	var scheduler = task.GetScheduler()
	scheduler.StopTask(paramsJson.TaskId)
	ctx.JSON(http.StatusBadRequest, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": "",
	})
}

// 删除任务
func (*Task) Delete(ctx *gin.Context) {
	// 参数
	var res *libs.ErrorInfo
	var paramsJson StartRequestParams
	if err := ctx.ShouldBindJSON(&paramsJson); err != nil {
		res = libs.ErrorCode["ParamsError"]
		fmt.Println(err)
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": res.Code,
			"msg":  res.Msg,
			"data": res.Data,
		})
		return
	}
}

// 获取任务信息
func (*Task) GetTaskInfo(ctx *gin.Context) {

}
