package api

import (
	"dhcp/api/request"
	"dhcp/global"
	"dhcp/internal/ip"
	"dhcp/internal/service"
	"dhcp/result"
	"github.com/gin-gonic/gin"
)

//
//func Redirect(c *gin.Context) {
//	c.Redirect(http.StatusMovedPermanently, "")
//}

func DHCPServerRegisterRoute(r *gin.Engine) {
	dhcpserver := r.Group("/dhcpserver")
	dhcpserver.POST("/start", DHCPServerStart)
	dhcpserver.POST("/stop", DHCPServerStop)
	dhcpserver.GET("/info", DHCPServerInfo)
	dhcpserver.GET("/localip", GetLocalIPAndInterfaces)
	dhcpserver.POST("/delete", DHCPServerDelete)
}

func DHCPServerStart(c *gin.Context) {
	var req request.DHCPService
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(200, result.Result{
			Code: 400,
			Data: nil,
			Msg:  "参数错误",
		})
		return
	}
	re := service.StartDHCPService(c.Request.Context(), global.Log, req)
	c.JSON(200, re)
}

func DHCPServerDelete(c *gin.Context) {
	var req request.DHCPService
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(200, result.Result{
			Code: 400,
			Data: nil,
			Msg:  "参数错误",
		})
		return
	}
	re := service.DeleteDHCPService(c.Request.Context(), global.Log, req)
	c.JSON(200, re)
}

func DHCPServerStop(c *gin.Context) {
	var req request.DHCPService
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(200, result.Result{
			Code: 400,
			Data: nil,
			Msg:  "参数错误",
		})
		return
	}
	re := service.StopDHCPService(c.Request.Context(), global.Log, req)
	c.JSON(200, re)
}

func DHCPServerInfo(c *gin.Context) {
	var d []request.DHCPService
	for _, v := range service.DHCPServiceInfoMap {
		d = append(d, v)
	}
	c.JSON(200, result.Result{
		Code: 200,
		Data: d,
		Msg:  "获取当前服务成功",
	})
}

func GetLocalIPAndInterfaces(c *gin.Context) {
	re := ip.GetLocalIPAndInterface()
	c.JSON(200, re)
}
