package admin

import (
	"github.com/gin-gonic/gin"
	"server/dao"
	"server/model/common/response"
	"server/utils/logger"
	"strconv"
)

type TunnelApi struct{}

func (ta *TunnelApi) QueryAllTunnels(c *gin.Context) {
	tunnels, err := tunnelService.QueryAllTunnels()
	if err != nil {
		logger.Get().Error("QueryAllTunnels ---- " + err.Error())
		response.FailWithMessage("获取隧道失败", c)
		return
	}
	response.OkWithData(tunnels, c)
}

func (ta *TunnelApi) CreateTunnel(c *gin.Context) {
	var tunnel dao.Tunnel
	if err := c.ShouldBindJSON(&tunnel); err != nil {
		logger.Get().Error("CreateTunnel ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err := tunnelService.CreateTunnel(tunnel)
	if err != nil {
		logger.Get().Error("CreateTunnel ---- " + err.Error())
		response.FailWithMessage("创建隧道失败", c)
		return
	}
	response.OkWithMessage("创建隧道成功", c)
}

func (ta *TunnelApi) UpdateTunnel(c *gin.Context) {
	var tunnel dao.Tunnel
	if err := c.ShouldBindJSON(&tunnel); err != nil {
		logger.Get().Error("UpdateTunnel ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err := tunnelService.UpdateTunnel(tunnel)
	if err != nil {
		logger.Get().Error("UpdateTunnel ---- " + err.Error())
		response.FailWithMessage("更新隧道失败", c)
		return
	}
	response.OkWithMessage("更新隧道成功", c)
}

func (ta *TunnelApi) DeleteTunnel(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		logger.Get().Error("DeleteTunnel ---- " + err.Error())
		response.FailWithMessage("参数错误", c)
		return
	}
	err = tunnelService.DeleteTunnel(id)
	if err != nil {
		logger.Get().Error("DeleteTunnel ---- " + err.Error())
		response.FailWithMessage("删除隧道失败", c)
		return
	}
	response.OkWithMessage("删除隧道成功", c)
}
