package handler

import (
	"github.com/gin-gonic/gin"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	"github.com/go-nunu/nunu-layout-advanced/internal/service"
	"github.com/go-nunu/nunu-layout-advanced/internal/service/api/gameShield"
	"net/http"
)

type GameShieldHandler struct {
	*Handler
	gameShieldService gameShield.GameShieldService
	crawlerService    service.CrawlerService
}

func NewGameShieldHandler(
	handler *Handler,
	gameShieldService gameShield.GameShieldService,
	crawlerService service.CrawlerService,
) *GameShieldHandler {
	return &GameShieldHandler{
		Handler:           handler,
		gameShieldService: gameShieldService,
		crawlerService:    crawlerService,
	}
}

func (h *GameShieldHandler) SubmitGameShield(ctx *gin.Context) {
	req := new(v1.GameShieldSubmitRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.gameShieldService.SubmitGameShield(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *GameShieldHandler) EditGameShield(ctx *gin.Context) {
	req := new(v1.GameShieldSubmitRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err)
		return
	}
	res, err := h.gameShieldService.EditGameShield(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *GameShieldHandler) GetGameShieldField(ctx *gin.Context) {
	req := new(v1.GetGameShieldRuleIdRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err)
		return
	}
	val, err := h.crawlerService.GetField(ctx, req.AppName)
	res, exists := val[req.Name] // name 例如 "rule_id"、"priority" 等
	if !exists {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, "field not exists")
		return
	}
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *GameShieldHandler) GetGameShieldKey(ctx *gin.Context) {
	req := new(v1.GetGameShieldRuleIdRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}

	res, err := h.gameShieldService.GetGameShieldKey(ctx, req.HostId)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *GameShieldHandler) DeleteGameShield(ctx *gin.Context) {
	req := new(v1.GameShieldRuleIdRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.gameShieldService.DeleteGameShield(ctx, req.RuleId)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *GameShieldHandler) GetGameShieldOnlineList(ctx *gin.Context) {
	req := new(v1.GameShieldRuleIdRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.gameShieldService.GetGameShieldOnlineList(ctx, req.RuleId)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *GameShieldHandler) IsExistGameShieldKey(ctx *gin.Context) {
	req := new(v1.KeyAndFieldResponse)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.gameShieldService.IsExistGameShieldKey(ctx, req.Key)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}
