package say

import (
	"math/rand/v2"
	"strconv"

	"git.lxtend.com/lixiangwuxian/qqbot/constants"
	"git.lxtend.com/lixiangwuxian/qqbot/handler"
	"git.lxtend.com/lixiangwuxian/qqbot/model"
	"git.lxtend.com/lixiangwuxian/qqbot/service/kw"
	"git.lxtend.com/lixiangwuxian/qqbot/util"
)

func init() {
	handler.RegisterHandler("kw", setKw, constants.LEVEL_TRUSTED)
	handler.RegisterHelpInform("kw [关键词] [回复内容]", "kw", "添加关键词回复,kw [关键词] 删除关键词下全部回复")
	handler.RegisterHandler("kwd", deleteKw, constants.LEVEL_TRUSTED)
	handler.RegisterHelpInform("kwd [关键词] [回复内容]", "kw", "删除关键词下指定回复")
	handler.RegisterHandler("kwall", getGroupAllKW, constants.LEVEL_TRUSTED)
	handler.RegisterHelpInform("kwall", "kw", "kwall 查看本群所有关键词回复")
	initKw()
}

func initKw() {
	kwList, err := kw.GetAllKW()
	if err != nil {
		return
	}
	for _, v := range kwList {
		group, _ := strconv.Atoi(v.GroupID)
		regKw(v.Kw, int64(group))
	}
}

func setKw(msg model.Message) (reply *model.Reply) {
	tokens := util.SplitN(msg.RawMsg, 3)
	if len(tokens) < 2 {
		return &model.Reply{
			ReplyMsg:       "参数不足, 请使用\"kw [关键词] [回复内容]\"添加关键词回复",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}

	if _, err := kw.AddKW(tokens[1], tokens[2], strconv.Itoa(int(msg.GroupInfo.GroupId)), strconv.Itoa(int(msg.UserId))); err != nil {
		return &model.Reply{
			ReplyMsg:       "添加失败",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	regKw(tokens[1], msg.GroupInfo.GroupId)
	return &model.Reply{
		ReplyMsg:       "记下了",
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func deleteKw(msg model.Message) (reply *model.Reply) {
	tokens := util.SplitN(msg.RawMsg, 3)
	if len(tokens) == 2 {
		if err := kw.DeleteKW(tokens[1], strconv.Itoa(int(msg.GroupInfo.GroupId))); err != nil {
			return &model.Reply{
				ReplyMsg:       "删除失败",
				ReferOriginMsg: true,
				FromMsg:        msg,
			}
		}
		unRegKw(tokens[1], msg.GroupInfo.GroupId)
		return &model.Reply{
			ReplyMsg:       "清空了",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	if len(tokens) < 2 {
		return &model.Reply{
			ReplyMsg:       "参数不足, 请使用\"kwd [关键词] [回复内容]\"删除对应的关键词回复",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	if err := kw.DeleteKWReply(tokens[1], tokens[2], strconv.Itoa(int(msg.GroupInfo.GroupId))); err != nil {
		return &model.Reply{
			ReplyMsg:       "删除失败",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	return &model.Reply{
		ReplyMsg:       "删除了",
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func getGroupAllKW(msg model.Message) (reply *model.Reply) {
	kwList, err := kw.GetGroupAllKW(strconv.Itoa(int(msg.GroupInfo.GroupId)))
	if err != nil {
		return &model.Reply{
			ReplyMsg:       "获取失败",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	kws := ""
	for k, v := range kwList {
		kws += v.Kw + " -> " + v.Reply
		if k != len(kwList)-1 {
			kws += "\n"
		}
	}
	return &model.Reply{
		ReplyMsg:       kws,
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func regKw(keyword string, groupId int64) {
	handler.RegisterGroupHandler(groupId, keyword, kwReply, constants.LEVEL_USER)
}

func unRegKw(keyword string, groupId int64) {
	handler.UnRegisterGroupHandler(groupId, keyword)
}

func kwReply(msg model.Message) (reply *model.Reply) {
	w, err := kw.GetKW(msg.RawMsg, strconv.Itoa(int(msg.GroupInfo.GroupId)))
	if err != nil || len(w) == 0 {
		return nil
	}
	wlist := []string{}
	for _, v := range w {
		wlist = append(wlist, v.Reply)
	}
	pick := rand.IntN(len(wlist))
	return &model.Reply{
		ReplyMsg:       wlist[pick],
		ReferOriginMsg: false,
		FromMsg:        msg,
	}
}
