package pluginlog

import (
	"context"

	"github.com/jeanhua/AniaBot/bot/utils"
	"github.com/jeanhua/AniaBot/common/bot"
	"github.com/jeanhua/AniaBot/common/model/command"
	"github.com/jeanhua/AniaBot/common/model/message"
	"github.com/jeanhua/AniaBot/common/plugin"
	"github.com/jeanhua/AniaBot/common/plugininfo"
	"github.com/spf13/viper"
)

type LogPlugin struct {
	plugin.Meta
}

func NewPlugin() *LogPlugin {
	p := &LogPlugin{}
	p.Name = "日志打印插件"
	p.HelpWords = "用于在控制台打印日志信息"
	p.Author = "jeanhua"
	p.Version = "1.0.0"
	p.AdminOnly = true
	p.Order = plugin.LevelLog
	p.ShowFor = plugininfo.ShowForNone
	return p
}

func (p *LogPlugin) Start(ctx context.Context, cfg *viper.Viper) error {
	lastStartTime, ok := p.Storage.GetString(ctx, "last_start_time")
	if !ok {
		lastStartTime = "未保存"
	}
	p.Storage.SetString(ctx, "last_start_time", utils.GetFormattedTime())
	p.Logger.Info("日志打印插件初始化完成", "lastStartTime", lastStartTime)
	return nil
}

func (p *LogPlugin) OnGroupMsg(ctx context.Context, bot bot.Bot, cmd command.Command, msg message.Message) (bool, error) {
	p.Logger.Info("[收<-群]", "groupId", msg.GroupId, "userId", msg.Sender.UserId, "message", msg.FriendlyText(false,
		message.WithGetMsgFunc(bot.GetMsgDetail),
		message.WithGetForwardMsgFunc(bot.GetForwardMsg),
	))
	return true, nil
}

func (p *LogPlugin) OnFriendMsg(ctx context.Context, bot bot.Bot, cmd command.Command, msg message.Message) (bool, error) {
	p.Logger.Info("[收<-好友]", "userId", msg.Sender.UserId, "message", msg.FriendlyText(false,
		message.WithGetMsgFunc(bot.GetMsgDetail),
		message.WithGetForwardMsgFunc(bot.GetForwardMsg),
	))
	return true, nil
}
