package api

import (
	"gadmin/config"
	"gadmin/internal/admin/forms"
	"gadmin/internal/admin/service"
	"gadmin/utility/serializer"
	"gadmin/utility/token"
	"github.com/gin-gonic/gin"
	"os"
)

type (
	Map = map[string]interface{}
)

func MenuDynamic(c *gin.Context) {
	systemId := token.GetSystemId(c)
	t := c.GetHeader("authorization")
	roleId, _ := c.Get("admin_role_id")
	list, err := service.Menu.GetRoleMenuList(systemId, roleId.(int64), t)
	if err != nil {
		c.JSON(200, ErrorResponse(err))
		return
	}
	c.JSON(200, serializer.Suc(list, "获取成功"))
	return
}

func GetAllMenuList(c *gin.Context) (map[int32][]*forms.Menu, error) {
	systems, err := service.AdminRole.GetAllSystem()
	if err != nil {
		return nil, err
	}
	t := c.GetHeader("authorization")
	roleId, _ := c.Get("admin_role_id")

	systemMap := make(map[int32][]*forms.Menu)
	for _, sys := range systems {
		menus, err := service.Menu.GetRoleMenuList(sys.ID, roleId.(int64), t)
		if err != nil {
			return nil, err
		}
		systemMap[sys.ID] = menus
	}
	return systemMap, nil
}

func GetMenuList(c *gin.Context) []Map {
	var (
		lists       []Map
		localHidden = true // 本地显示的菜单
	)

	if os.Getenv("ADMIN_IS_LOCAL") == "1" {
		localHidden = false
	}

	// 控制台
	lists = append(lists, Map{
		"id":        1,
		"path":      "/dashboard",
		"name":      "Dashboard",
		"component": "LAYOUT",
		"redirect":  "/dashboard/console",
		"meta": Map{
			"title": "Dashboard",
			"icon":  "DashboardOutlined",
		},
		"children": []Map{
			{
				"id":        1001,
				"path":      "console",
				"name":      "dashboard_console",
				"component": "/dashboard/console/console",
				"meta": Map{
					"title": "主控台",
				},
			},
		},
	})

	// 玩家管理
	lists = append(lists, Map{
		"id":        2,
		"path":      "/account",
		"name":      "Account",
		"component": "LAYOUT",
		"redirect":  "/account/account-list",
		"meta": Map{
			"icon":  "UserOutlined",
			"title": "玩家管理",
			"sort":  2,
		},
		"children": []Map{
			{
				"id":        2001,
				"path":      "account-list",
				"name":      "account-list",
				"component": "/account/accountList/index",
				"meta": Map{
					"title": "玩家列表",
				},
			},
			{
				"id":        2002,
				"path":      "account-info/:id?",
				"name":      "account-info",
				"component": "/account/accountList/info",
				"meta": Map{
					"title":      "基础详情",
					"hidden":     true,
					"activeMenu": "account-list",
				},
			},
			{
				"id":        2003,
				"path":      "account-search",
				"name":      "account-search",
				"component": "/account/accountList/search",
				"meta": Map{
					"title": "全服查找",
				},
			},
			/*{
				"path":      "account-banLogs",
				"name":      "account-banLogs",
				"component": "/account/accountList/banLogs",
				"meta": Map{
					"title": "拉黑记录",
				},
			},
			{
				"path":      "retrofit-list",
				"name":      "retrofit-list",
				"component": "/retrofit/index",
				"meta": Map{
					"title": "配装模板",
				},
			},
			{
				"path":      "consumption-details",
				"name":      "consumption-details",
				"component": "/recharge/consumption/index",
				"meta": Map{
					"title": "消费变动记录",
				},
			},
			{
				"path":      "consumption-statistics",
				"name":      "consumption-statistics",
				"component": "/recharge/consumptionStatistics/index",
				"meta": Map{
					"title": "消费统计",
				},
			},
			{
				"path":      "game-data-alarm",
				"name":      "game-data-alarm",
				"component": "/account/gameDataAlarm/index",
				"meta": Map{
					"title": "游戏异常",
				},
			},
			{
				"path":      "game-cheating-alarm",
				"name":      "game-cheating-alarm",
				"component": "/account/gameCheatingAlarm/index",
				"meta": Map{
					"title": "作弊数据筛选",
				},
			},
			{
				"path":      "chat-report-list",
				"name":      "chat-report-list",
				"component": "/account/chatReportList/index",
				"meta": Map{
					"title": "聊天举报记录",
				},
			},
			{
				"path":      "chat-log-list",
				"name":      "chat-log-list",
				"component": "/account/chatLogList/index",
				"meta": Map{
					"title": "聊天记录",
				},
			},*/
		},
	})

	if config.IsSuperRole(service.User.GetUserRoleId(c)) {
		// 权限管理
		lists = append(lists, Map{
			"id":        3,
			"path":      "/permission",
			"name":      "Permission",
			"component": "LAYOUT",
			"redirect":  "/permission/menu",
			"meta": Map{
				"icon":  "SafetyCertificateOutlined",
				"title": "权限管理",
				"sort":  1,
			},
			"children": []Map{
				{
					"id":        3003,
					"path":      "menu",
					"name":      "permission_menu",
					"component": "/permission/menu/menu",
					"meta": Map{
						"title": "菜单权限",
					},
				},
				{
					"id":        3001,
					"path":      "user",
					"name":      "permission_user",
					"component": "/permission/user/user",
					"meta": Map{
						"title": "后台用户",
					},
				},
				{
					"id":        3002,
					"path":      "role",
					"name":      "permission_role",
					"component": "/permission/role/role",
					"meta": Map{
						"title": "角色管理",
					},
				},
			},
		})
	}

	// 充值管理
	/*lists = append(lists, Map{
		"id":        4,
		"path":      "/recharge",
		"name":      "Recharge",
		"component": "LAYOUT",
		"redirect":  "/recharge/recharge-list",
		"meta": Map{
			"icon":  "PayCircleOutlined",
			"title": "充值订单",
			"sort":  2,
		},
		"children": []Map{
			{
				"id":        4001,
				"path":      "recharge-list",
				"name":      "recharge-list",
				"component": "/recharge/rechargeList/index",
				"meta": Map{
					"title": "订单记录",
				},
			},
			{
				"id":        4002,
				"path":      "recharge-dailyStatistics",
				"name":      "recharge-dailyStatistics",
				"component": "/recharge/dailyStatistics/index",
				"meta": Map{
					"title": "每日统计",
				},
			},
			{
				"id":        4003,
				"path":      "recharge-ordersSettle",
				"name":      "recharge-ordersSettle",
				"component": "/recharge/ordersSettle/index",
				"meta": Map{
					"title":     "余额平账",
					"keepAlive": true,
				},
			},
			{
				"id":        4004,
				"path":      "recharge-abnormalOrder",
				"name":      "recharge-abnormalOrder",
				"component": "/recharge/abnormalOrder/index",
				"meta": Map{
					"title":     "异常订单",
					"keepAlive": true,
				},
			},
		},
	})*/

	// 排行榜
	/*lists = append(lists, Map{
		"id":        5,
		"path":      "/ranking",
		"name":      "Ranking",
		"component": "LAYOUT",
		"redirect":  "/ranking/index",
		"meta": Map{
			"icon":  "CellularOutline",
			"title": "排行榜",
			"sort":  2,
		},
		"children": []Map{
			{
				"id":        5001,
				"path":      "recharge-ranking",
				"name":      "recharge-ranking",
				"component": "/ranking/recharge/index",
				"meta": Map{
					"title": "充值排行",
				},
			},
			{
				"id":        5002,
				"path":      "diamond-ranking",
				"name":      "diamond-ranking",
				"component": "/ranking/diamond/index",
				"meta": Map{
					"title": "消费排行",
				},
			},
			{
				"id":        5003,
				"path":      "level-ranking",
				"name":      "level-ranking",
				"component": "/ranking/level/index",
				"meta": Map{
					"title": "等级排行",
				},
			},
			{
				"id":        5004,
				"path":      "elrank-ranking",
				"name":      "elrank-ranking",
				"component": "/ranking/elrank/index",
				"meta": Map{
					"title": "无尽排行",
				},
			},
			{
				"id":        5005,
				"path":      "duel-ranking",
				"name":      "duel-ranking",
				"component": "/ranking/duel/index",
				"meta": Map{
					"title": "狭路排行",
				},
			},
			{
				"id":        5006,
				"path":      "gudong-ranking",
				"name":      "gudong-ranking",
				"component": "/ranking/gudong/index",
				"meta": Map{
					"title": "古玩排行",
				},
			},
			{
				"id":        5007,
				"path":      "idiom-ranking",
				"name":      "idiom-ranking",
				"component": "/ranking/idiom/index",
				"meta": Map{
					"title": "金榜题名",
				},
			},
			{
				"id":        5008,
				"path":      "boss-ranking",
				"name":      "boss-ranking",
				"component": "/ranking/boss/index",
				"meta": Map{
					"title": "暗影突袭",
				},
			},
			{
				"id":        5009,
				"path":      "adv-ranking",
				"name":      "adv-ranking",
				"component": "/ranking/adv/index",
				"meta": Map{
					"title": "看广告排行",
				},
			},
			{
				"id":        5010,
				"path":      "login-ranking",
				"name":      "login-ranking",
				"component": "/ranking/login/index",
				"meta": Map{
					"title": "登录排行",
				},
			},
		},
	})*/

	// 数据统计
	/*lists = append(lists, Map{
		"id":        6,
		"path":      "/echarts",
		"name":      "Echarts",
		"component": "LAYOUT",
		"redirect":  "/echarts/index",
		"meta": Map{
			"icon":  "BarChartOutline",
			"title": "数据统计",
			"sort":  2,
		},
		"children": []Map{
			{
				"id":        6001,
				"path":      "login-echarts",
				"name":      "login-echarts",
				"component": "/echarts/login/index",
				"meta": Map{
					"title": "登录数据",
				},
			},
			{
				"id":        6002,
				"path":      "chapter-echarts",
				"name":      "chapter-echarts",
				"component": "/echarts/chapter/index",
				"meta": Map{
					"title": "关卡数据",
				},
			},
			{
				"id":        6003,
				"path":      "basic-echarts",
				"name":      "basic-echarts",
				"component": "/echarts/basic/index",
				"meta": Map{
					"title": "基础数据",
				},
			},
			{
				"id":        6004,
				"path":      "adv-echarts",
				"name":      "adv-echarts",
				"component": "/echarts/adv/index",
				"meta": Map{
					"title": "广告数据",
				},
			},
			{
				"id":        6005,
				"path":      "echarts-tests",
				"name":      "echarts-tests",
				"component": "/echarts/adv/index2",
				"meta": Map{
					"hidden": localHidden,
					"title":  "图表测试",
				},
			},
			{
				"id":        6006,
				"path":      "goods-echarts",
				"name":      "goods-echarts",
				"component": "/echarts/goods/index",
				"meta": Map{
					"title": "商品数据",
				},
			},
			{
				"id":        6007,
				"path":      "gudong-echarts",
				"name":      "gudong-echarts",
				"component": "/echarts/gudong/index",
				"meta": Map{
					"title": "古玩数据",
				},
			},
			{
				"id":        6008,
				"path":      "duel-echarts",
				"name":      "duel-echarts",
				"component": "/echarts/duel/index",
				"meta": Map{
					"title": "狭路对决",
				},
			},
			{
				"id":        6009,
				"path":      "expedition-echarts",
				"name":      "expedition-echarts",
				"component": "/echarts/expedition/index",
				"meta": Map{
					"title": "远征数据",
				},
			},
			{
				"id":        6010,
				"path":      "idiom-echarts",
				"name":      "idiom-echarts",
				"component": "/echarts/idiom/index",
				"meta": Map{
					"title": "金榜题名",
				},
			},
			{
				"id":        6011,
				"path":      "boss-echarts",
				"name":      "boss-echarts",
				"component": "/echarts/boss/index",
				"meta": Map{
					"title": "暗影突袭",
				},
			},
			{
				"id":        6012,
				"path":      "seven-echarts",
				"name":      "seven-echarts",
				"component": "/echarts/seven/index",
				"meta": Map{
					"title": "七日任务",
				},
			},
			{
				"id":        6013,
				"path":      "disconnect-echarts",
				"name":      "disconnect-echarts",
				"component": "/echarts/disconnect/index",
				"meta": Map{
					"title": "重连数据",
				},
			},
			{
				"id":        6014,
				"path":      "gem-echarts",
				"name":      "gem-echarts",
				"component": "/echarts/gem/index",
				"meta": Map{
					"title": "宝石数据",
				},
			},
			{
				"id":        6015,
				"path":      "limitgift-echarts",
				"name":      "limitgift-echarts",
				"component": "/echarts/limitgift/index",
				"meta": Map{
					"title": "限时礼包",
				},
			},
			{
				"id":        6016,
				"path":      "treasure-echarts",
				"name":      "treasure-echarts",
				"component": "/echarts/treasure/index",
				"meta": Map{
					"title": "宝物数据",
				},
			},
			{
				"id":        6017,
				"path":      "grandmaster-echarts",
				"name":      "grandmaster-echarts",
				"component": "/echarts/grandmaster/index",
				"meta": Map{
					"title": "最强王者",
				},
			},
			{
				"id":        6018,
				"path":      "gradeDistribution-echarts",
				"name":      "gradeDistribution-echarts",
				"component": "/echarts/gradeDistribution/index",
				"meta": Map{
					"title": "玩家等级分布",
				},
			},
			{
				"id":        6019,
				"path":      "roles-echarts",
				"name":      "roles-echarts",
				"component": "/echarts/roles/index",
				"meta": Map{
					"title": "玩家拥有角色",
				},
			},
			{
				"id":        6020,
				"path":      "heroLevelDistribution-echarts",
				"name":      "heroLevelDistribution-echarts",
				"component": "/echarts/heroLevelDistribution/index",
				"meta": Map{
					"title": "角色等级分布",
				},
			},
			{
				"id":        6021,
				"path":      "levelOutput-echarts",
				"name":      "levelOutput-echarts",
				"component": "/echarts/levelOutput/index",
				"meta": Map{
					"title": "玩家等级产出",
				},
			},
		},
	})*/

	gameToolMap := make([]Map, 0)
	gameToolMap = append(gameToolMap, Map{
		"id":        7001,
		"path":      "config",
		"name":      "system_config",
		"component": "/system/config/config",
		"meta": Map{
			"title": "服务配置",
		},
	}, Map{
		"id":        7002,
		"path":      "deploy",
		"name":      "system_deploy",
		"component": "/deploy/index",
		"meta": Map{
			"title": "服务部署",
		},
	})

	//// 非正式环境加载工具库
	//if os.Getenv("GIN_MODE") != "release" && os.Getenv("GIN_MODE") != "" {
	//	gameToolMap = append(gameToolMap, Map{
	//		"path":      "reboot",
	//		"name":      "system_reboot",
	//		"component": "/system/reboot/index",
	//		"meta": Map{
	//			"title": "重启服务",
	//		}})
	//}

	/*gameToolMap = append(gameToolMap, Map{
		"path":      "activity-tool",
		"name":      "activity-tool",
		"component": "/tool/activity/index",
		"meta": Map{
			"title": "活动查询",
		},
	})*/

	// 游戏工具
	lists = append(lists, Map{
		"id":        7,
		"path":      "/tool",
		"name":      "Tool",
		"component": "LAYOUT",
		"redirect":  "/tool/index",
		"meta": Map{
			"icon":  "ToolOutlined",
			"title": "游戏工具",
			"sort":  5,
		},
		"children": gameToolMap,
	})

	// 邮件通知
	/*lists = append(lists, Map{
		"id":        8,
		"path":      "/email",
		"name":      "email",
		"component": "LAYOUT",
		//"redirect":  "/frame/docs",
		"meta": Map{
			"icon":       "MailOutlined",
			"sort":       10,
			"isRoot":     true,
			"activeMenu": "email_index",
		},
		"children": []Map{
			{
				"id":        8001,
				"path":      "index",
				"name":      "email_index",
				"component": "/email/index",
				"meta": Map{
					"title":      "邮件通知",
					"activeMenu": "email_index",
				},
			}},
	})*/

	// 邮件通知
	/*lists = append(lists, Map{
		"id":        9,
		"path":      "/Mail",
		"name":      "mail",
		"component": "LAYOUT",
		//"redirect":  "/frame/docs",
		"meta": Map{
			"icon":       "MailOutlined",
			"sort":       10,
			"isRoot":     true,
			"activeMenu": "mail_index",
		},
		"children": []Map{
			{
				"id":        9001,
				"path":      "index",
				"name":      "mail_index",
				"component": "/mail/index",
				"meta": Map{
					"title":      "邮件通知(旧)",
					"activeMenu": "mail_index",
				},
			}},
	})*/

	// 广播通知
	/*lists = append(lists, Map{
		"id":        10,
		"path":      "/notice",
		"name":      "notice",
		"component": "LAYOUT",
		//"redirect":  "/frame/docs",
		"meta": Map{
			"icon":  "MegaphoneOutline",
			"sort":  10,
			"title": "广播管理",
			//"isRoot": true,
			//"activeMenu": "noticev2_index",
		},
		"children": []Map{
			{
				"id":        10001,
				"path":      "noticev2_index", // ?
				"name":      "noticev2_index",
				"component": "/noticev2/index",
				"meta": Map{
					"title": "广播通知",
					//"activeMenu": "noticev2_index",
				},
			},
			//{
			//	"path":      "index",
			//	"name":      "notice_index",
			//	"component": "/notice/index",
			//	"meta": Map{
			//		"title": "广播通知(旧)",
			//		//"activeMenu": "notice_index",
			//	},
			//},
		},
	})*/
	// 客服记录
	/*lists = append(lists, Map{
		"id":        11,
		"path":      "/chatLog",
		"name":      "ChatLog",
		"component": "LAYOUT",
		"meta": Map{
			"icon":        "ChatboxEllipsesOutline",
			"sort":        10,
			"title":       "客服记录",
			"permissions": "ChatLog",
		},
		"children": []Map{
			{
				"id":        11001,
				"path":      "chatLog-index",
				"name":      "chatLog-index",
				"component": "/chatLog/index",
				"meta": Map{
					"title": "客服记录",
				},
			},
		},
	})*/
	// 兑换码
	/*lists = append(lists, Map{
		"id":        12,
		"path":      "/cdk",
		"name":      "cdk",
		"component": "LAYOUT",
		//"redirect":  "/frame/docs",
		"meta": Map{
			"icon": "TicketOutline",
			"sort": 10,
			//"isRoot": true,
			//"activeMenu": "cdk_index",
			"title": "兑换码管理",
		},
		"children": []Map{
			{
				"id":        12001,
				"path":      "index",
				"name":      "cdk_index",
				"component": "/cdk/index",
				"meta": Map{
					"title":      "批次列表",
					"activeMenu": "cdk_index",
				},
			},
			{
				"id":        12002,
				"path":      "cdk-redeemCodeList/:sn?",
				"name":      "cdk-redeemCodeList",
				"component": "/cdk/redeemCodeList",
				"meta": Map{
					"title": "兑换码列表",
					//"hidden":     true,
					//"activeMenu": "cdk_index",
				},
			},
		},
	})*/

	// 设置页面
	lists = append(lists, Map{
		"id":        13,
		"path":      "/setting",
		"name":      "Setting",
		"component": "LAYOUT",
		"redirect":  "/setting/account",
		"meta": Map{
			"icon":  "SettingOutlined",
			"title": "账户设置",
			"sort":  20,
		},
		"hidden": false,
		"children": []Map{
			{
				"id":        13001,
				"path":      "account",
				"name":      "setting-account",
				"component": "/setting/account/account",
				"meta": Map{
					"title": "个人信息",
				},
			},
			//{
			//	"path":      "info",
			//	"name":      "result-info",
			//	"component": "/setting/system/system",
			//	"meta": Map{
			//		"title": "信息页",
			//	},
			//},
			{
				"id":        13002,
				"path":      "log",
				"name":      "log-index",
				"component": "/log/index",
				"meta": Map{
					"title": "操作日志",
				},
			},
			{
				"id":        13003,
				"path":      "log-view/:id?",
				"name":      "log-view",
				"component": "/log/view",
				"meta": Map{
					"title":      "日志详情",
					"hidden":     true,
					"activeMenu": "log-index",
				},
			},
		},
	})
	t := c.GetHeader("authorization")
	// 管理后台切换
	lists = append(lists, Map{
		"id":        14,
		"path":      "/server_select",
		"name":      "ServerSelect",
		"component": "LAYOUT",
		"meta": Map{
			"icon":  "ServerOutline",
			"title": "管理后台切换",
			"sort":  5,
		},
		"children": []Map{
			{
				"id":        14001,
				"path":      "/redirect",
				"name":      "http://101.43.249.6:7004/gadmin/?access-token=%s" + t,
				"component": "LAYOUT",
				"meta": Map{
					"title": "魔君测试服",
				},
			},
			{
				"id":        14002,
				"path":      "/redirect",
				"name":      "http://101.43.249.6:7006/cadmin/?access-token=" + t,
				"component": "LAYOUT",
				"meta": Map{
					"title": "空之契约测试服",
				},
			},
			{
				"id":        14003,
				"path":      "/redirect",
				"name":      "http://192.168.0.186:8253/gadmin/?access-token=" + t,
				"component": "LAYOUT",
				"meta": Map{
					"title": "魔君本地",
				},
			},
		},
	})

	// 文档
	if localHidden == false {
		lists = append(lists, Map{
			"id":        15,
			"path":      "/external",
			"name":      "https://www.naiveui.com",
			"component": "LAYOUT",
			"meta": Map{
				"icon":  "DocumentTextOutline",
				"title": "NaiveUi文档",
				"sort":  99,
			},
			"children": []Map{},
		})
	}
	return lists
}
