package api

import (
	"fmt"
	"go-account-register/libs"
	"go-account-register/services"
	"net/http"
	"strconv"

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

type Twitter struct {
}

type RegisterParams struct {
}

func (t *Twitter) Login(ctx *gin.Context) {
	// 参数
	var res *libs.ErrorInfo
	accountId := ctx.Query("accountId")
	id, _ := strconv.Atoi(accountId)

	var TwitterService services.TwitterService
	res = TwitterService.Login(id)
	UserService := services.InitTwitterAccountService()
	UserService.ChangeAccountLoginStatus(id, res.Msg)
	ctx.JSON(http.StatusOK, gin.H{
		"code": res.Code,
		"msg":  res.Msg,
		"data": res.Data,
	})
}

func (t *Twitter) Logout(ctx *gin.Context) {
	accountId := ctx.Query("accountId")
	id, _ := strconv.Atoi(accountId)
	var TwitterService services.TwitterService
	TwitterService.Logout(id)
	UserService := services.InitTwitterAccountService()
	UserService.ChangeAccountLoginStatus(id, "账号下线")
	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": "",
	})
}

func (*Twitter) Register(ctx *gin.Context) {
	var res *libs.ErrorInfo
	var paramsJson RegisterParams
	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 BitBrowserService services.BitBrowserService
	res = BitBrowserService.Register()
	ctx.JSON(http.StatusOK, gin.H{
		"code": res.Code,
		"msg":  res.Msg,
		"data": res.Data,
	})
}

func (*Twitter) Register1(ctx *gin.Context) {
	var res *libs.ErrorInfo
	var paramsJson RegisterParams
	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 TwitterService services.TwitterService
	res = TwitterService.Register()
	ctx.JSON(http.StatusOK, gin.H{
		"code": res.Code,
		"msg":  res.Msg,
		"data": res.Data,
	})
}
