package controller

import (
	"github.com/gin-gonic/gin"
	"iot_manager_service/app/middleware"
	"iot_manager_service/app/system/dao"
	"iot_manager_service/app/system/service"
	"iot_manager_service/util"
	"net/http"
	"strconv"
)

var Menu = new(menu)

type menu struct{}

func (c *menu) GetDetail(ctx *gin.Context) {
	id := ctx.Query("id")
	iId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	detail, err := service.MenuService.Get(iId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, detail))
}

func (c *menu) List(ctx *gin.Context) {

}

func (c *menu) LazyList(ctx *gin.Context) {

}

func (c *menu) MenuList(ctx *gin.Context) {

}

func (c *menu) LazyMenuList(ctx *gin.Context) {

}

func (c *menu) Submit(ctx *gin.Context) {
	var req dao.Menu
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.MenuService.Submit(req)
	ctx.JSON(http.StatusOK, err)
}

func (c *menu) Remove(ctx *gin.Context) {

}

func (c *menu) Routes(ctx *gin.Context) {
	value, isExist := ctx.Get(middleware.Authorization)
	if !isExist || value == nil {
		ctx.JSON(http.StatusUnauthorized, util.NormalResponse(http.StatusUnauthorized, "", nil))
	}
	menus, err := service.MenuService.Routes(value.(*middleware.JwtToken).RoleId)
	if err != nil {
		ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
		return
	}
	ctx.JSON(http.StatusOK, menus)
}

func (c *menu) RoutesExt(cxt *gin.Context) {

}

func (c *menu) Buttons(ctx *gin.Context) {

}

func (c *menu) Tree(ctx *gin.Context) {

}

func (c *menu) GrantTree(ctx *gin.Context) {

}

func (c *menu) RoleTreeKeys(ctx *gin.Context) {

}

func (c *menu) GrantTopTree(ctx *gin.Context) {

}

func (c *menu) TopTreeKeys(ctx *gin.Context) {

}

func (c *menu) TopMenu(ctx *gin.Context) {

}

func (c *menu) authRoutes(ctx *gin.Context) {

}