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/mcuadros/go-defaults"
	"net/http"
)

type UdpForWardingHandler struct {
	*Handler
	udpForWardingService service.UdpForWardingService
}

func NewUdpForWardingHandler(
	handler *Handler,
	udpForWardingService service.UdpForWardingService,
) *UdpForWardingHandler {
	return &UdpForWardingHandler{
		Handler:              handler,
		udpForWardingService: udpForWardingService,
	}
}

func (h *UdpForWardingHandler) GetUdpForWarding(ctx *gin.Context) {
	req := new(v1.GetForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	res, err := h.udpForWardingService.GetUdpForWarding(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *UdpForWardingHandler) AddUdpForWarding(ctx *gin.Context) {
	req := new(v1.UdpForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	 err := h.udpForWardingService.AddUdpForwarding(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)

}

func (h *UdpForWardingHandler) EditUdpForWarding(ctx *gin.Context) {
	req := new(v1.UdpForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	err := h.udpForWardingService.EditUdpForwarding(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *UdpForWardingHandler) DeleteUdpForWarding(ctx *gin.Context) {
	req := new(v1.DeleteUdpForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	 err := h.udpForWardingService.DeleteUdpForwarding(ctx, req.Ids)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *UdpForWardingHandler) GetUdpForWardingList(ctx *gin.Context) {
	req := new(v1.GetForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	res, err := h.udpForWardingService.GetUdpForwardingWafUdpAllIps(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}