package help

import (
	"log"
	"strconv"

	"git.lxtend.com/lixiangwuxian/qqbot/action"
	"git.lxtend.com/lixiangwuxian/qqbot/constants"
	"git.lxtend.com/lixiangwuxian/qqbot/handler"
	"git.lxtend.com/lixiangwuxian/qqbot/model"
	"git.lxtend.com/lixiangwuxian/qqbot/qq_message"
	"git.lxtend.com/lixiangwuxian/qqbot/util"
)

func init() {
	handler.RegisterHandler("!help", help, constants.LEVEL_BANNED)
}

func help(msg model.Message) *model.Reply {
	var helpInfo string
	if len(util.SplitN(msg.RawMsg, 2)) == 1 {
		helpInfo = `请输入!help [分类] 查看下面的分类功能`
		for k := range handler.HelpInforms {
			helpInfo += "\n" + k
		}
		helpInfo += "\n" + "新功能请求请移步https://git.lxtend.com/lixiangwuxian/qq_bot/issues"
	} else {
		category := util.SplitN(msg.RawMsg, 2)[1]
		helpInfos := handler.HelpInforms[category]
		helpInfo = `以下是` + category + `的功能：`
		for _, v := range helpInfos {
			helpInfo += "\n" + v.Trigger + " : " + v.Inform
		}
	}
	textMsg := qq_message.NewTextMessage()
	textMsg.Data.Text = helpInfo
	nodeMsg := qq_message.NewNodeMessage()
	loginAccountInfo, err := action.GetLoginAccountInfo()
	if err != nil {
		log.Println("GetLoginAccountInfo error:", err)
		return nil
	}
	if loginAccountInfo == nil {
		return nil
	}
	nodeMsg.Data.UserID = strconv.FormatInt(int64(loginAccountInfo.Data.UserID), 10)
	nodeMsg.Data.Nickname = loginAccountInfo.Data.Nickname
	nodeMsg.Data.Content = []any{&textMsg}
	action.ActionManager.SendForward(&model.Reply{
		ReplyMsg:       []any{&nodeMsg},
		ReferOriginMsg: false,
		FromMsg:        msg,
	})
	return nil
}
