package v1

import (
	"dsbqj-admin/app/service"
	"dsbqj-admin/pkg/app"
	"dsbqj-admin/pkg/e"
	"dsbqj-admin/pkg/serializer"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CheckVersion(c *gin.Context) {
	var appG = app.Gin{C: c}
	service := service.VersionCheckService{}
	if err := c.ShouldBind(&service); err == nil {
		if res, err := service.Check(); err != nil {
			appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
		} else {
			appG.Response(http.StatusOK, e.SUCCESS, serializer.BuildVersion(res))
		}
	} else {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
	}
}

func ServerVersion(c *gin.Context) {
	var appG = app.Gin{C: c}
	service := service.VersionServerService{}
	if err := c.ShouldBind(&service); err == nil {
		if res, err := service.Servers(); err != nil {
			appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
		} else {
			appG.Response(http.StatusOK, e.SUCCESS, serializer.BuildVersions(res))
		}
	} else {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
	}

}

func ReloadVersion(c *gin.Context) {
	var appG = app.Gin{C: c}
	service := service.VersionReloadService{}
	if err := c.ShouldBind(&service); err == nil {
		if err := service.Reload(); err != nil {
			appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
		} else {
			appG.Response(http.StatusOK, e.SUCCESS, nil)
		}
	} else {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
	}
}

func ShowVersions(c *gin.Context) {
	var appG = app.Gin{C: c}
	service := service.VersionShowService{}

	if err := c.ShouldBind(&service); err == nil {
		if res, err := service.List(); err != nil {
			appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
		} else {
			appG.Response(http.StatusOK, e.SUCCESS, res)
		}
	} else {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
	}
}

func VersionCreate(c *gin.Context) {
	var appG = app.Gin{C: c}
	service := service.VersionCreateService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.Create()
		appG.Response(http.StatusOK, e.SUCCESS, res)
	} else {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
	}
}

func VersionEdit(c *gin.Context) {
	var appG = app.Gin{C: c}
	service := service.VersionEditService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.Edit()
		appG.Response(http.StatusOK, e.SUCCESS, res)
	} else {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
	}
}

func VersionDelete(c *gin.Context) {
	var appG = app.Gin{C: c}
	service := service.VersionDeleteService{}
	key := appG.ValidKey()
	service.Id = key

	if err := service.Delete(); err == nil {
		appG.Response(http.StatusOK, e.SUCCESS, nil)

	} else {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
	}

}
