package admin

import (
	"Wine-Server/utils"
	"Wine-Server/utils/tables"
	"github.com/gorilla/websocket"
)

func Query(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminQueryRes", utils.Fail("权限不足")))
		return
	}
	var param queryParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminQueryRes", utils.Fail("参数错误")))
		return
	}
	total, men, err := tables.ManagerQueryForAdmin(param.Cond, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminQueryRes", utils.Fail("查询管理员失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"superRoleAdminQueryRes",
		utils.Success(utils.JsonType{"total": total, "list": men}),
	))
}

func Update(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminUpdateRes", utils.Fail("权限不足")))
		return
	}
	var param updateParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminUpdateRes", utils.Fail("参数错误")))
		return
	}
	man := &tables.ManagerTable{Id: param.Id}
	err = man.Get()
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminUpdateRes", utils.Fail("管理员不存在")))
		return
	}
	if param.Password != "" {
		password, err := utils.Decrypt(param.Password)
		if err != nil {
			_ = conn.WriteJSON(utils.WsEvent("superRoleAdminUpdateRes", utils.Fail("解密失败")))
			return
		}
		man.Password = utils.HashPassword(password)
	}
	man.Name, man.Account = param.Name, param.Account
	err = man.UpdateSelf()
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminUpdateRes", utils.Fail("更新失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superRoleAdminUpdateRes", utils.Success(nil)))
	// TODO: inform admin login expired
}

func Delete(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminDeleteRes", utils.Fail("权限不足")))
		return
	}
	var ids []string
	err := utils.AnyTrans(data, &ids)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminDeleteRes", utils.Fail("参数错误")))
		return
	}
	err = tables.ManagersDelete(ids)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminDeleteRes", utils.Fail("删除失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superRoleAdminDeleteRes", utils.Success(nil)))
}

func Add(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminAddRes", utils.Fail("权限不足")))
		return
	}
	var param addParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminAddRes", utils.Fail("参数错误")))
		return
	}
	if param.Name == "" || param.Account == "" || param.Password == "" {
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminAddRes", utils.Fail("数据不可为空")))
		return
	}
	var password string
	password, err = utils.Decrypt(param.Password)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminAddRes", utils.Fail("解密失败")))
		return
	}
	id := utils.RandomString(16, utils.AlphaNumPatten)
	man := &tables.ManagerTable{Id: id, Name: param.Name, Account: param.Account, Password: utils.HashPassword(password)}
	err = man.Insert()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleAdminAddRes", utils.Fail("添加失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superRoleAdminAddRes", utils.Success(nil)))
}
