package controller

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

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, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	detail, err := service.MenuService.Get(iId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, detail))
}

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

}

func (c *menu) LazyList(ctx *gin.Context) {
	id := ctx.Query("parentId")
	name := ctx.Query("name")
	code := ctx.Query("code")
	alias := ctx.Query("alias")
	var parentId int64 = -1
	if id != "" {
		parentId, _ = strconv.ParseInt(id, 10, 64)
	}

	menus, err := service.MenuService.LazyList(parentId, name, code, alias)
	if err != nil {
		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, menus))
}

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, common.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, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	menus, err := service.MenuService.Routes(claims.RoleId)
	if err != nil {
		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, menus))
}

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

}

// 由于子菜单 没有分的很详细，这里直接先让他们全部有这个权限 2022-12-07 dsx
func (c *menu) Buttons(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	menus, err := service.MenuService.Routes(claims.RoleId)
	if err != nil {
		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
		return
	}
	var menus2 []model.MenuRouteDetail
	subMenus := make(map[string]string)
	subMenus["add"] = "新增"
	subMenus["edit"] = "修改"
	subMenus["view"] = "查看"
	subMenus["delete"] = "删除"
	for _, menu := range menus {
		if menu.Children != nil {
			for _, child := range menu.Children {
				var menus3 []model.MenuRouteDetail
				var i int64
				i = 1
				for k, v := range subMenus {
					var menu4 model.MenuRouteDetail
					menu4.ID = child.ID + i
					menu4.ParentId = child.ID
					child.Path = strings.ReplaceAll(child.Path, "-", "")
					subPaths := strings.Split(child.Path, "/")
					subPath := subPaths[len(subPaths)-1]
					menu4.Code = subPath + "_" + k
					menu4.Name = v
					menu4.Path = child.Path + "/" + k
					menu4.Source = k
					menus3 = append(menus3, menu4)
					i++
				}
				child.Children = menus3
				menus2 = append(menus2, child)
			}
		}
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, menus2))
}

func (c *menu) Tree(ctx *gin.Context) {
	menus, err := service.MenuService.Tree()
	if err != nil {
		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, menus))
}

func (c *menu) GrantTree(ctx *gin.Context) {
	//菜单权限
	tree, err := service.MenuService.GetTree()
	if err != nil {
		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
		return
	}
	rsp := model.ResGrantTree{
		Menu:      tree,
		DataScope: nil,
		ApiScope:  nil,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
	//数据权限
	//接口权限
}

func (c *menu) RoleTreeKeys(ctx *gin.Context) {
	i, _ := strconv.Atoi(ctx.Query("roleIds"))
	roleIds := int64(i)
	menuIds := service.RoleMenuService.GetMenuIdByRoleId(roleIds)
	rsp := model.ResMenuIds{
		Menu:      menuIds,
		DataScope: nil,
		ApiScope:  nil,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}

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) {

}
