package core

import (
	"encoding/json"
	"time"

	"github.com/cdle/sillyplus/utils"
)

type PMsg struct {
	Class   string `json:"class"`
	Unix    int    `json:"unix"`
	Content string `json:"content"`
	Version string `json:"version"`
}

var plugin_messages = MakeBucket("plugin_messages")

func WritePluginMessage(uuid string, class string, content string) {
	if uuid == "" {
		return
	}
	var version = ""
	f := GetFunctionByUUID(uuid)
	if f != nil {
		version = f.Version
	}
	var data = plugin_messages.GetBytes(uuid)
	pmsgs := []PMsg{}
	json.Unmarshal(data, &pmsgs)
	s := &Strings{}
	ok := false
	new := PMsg{
		Class:   class,
		Unix:    int(time.Now().Unix()),
		Content: content,
		Version: version,
	}
	for i := range pmsgs {
		if pmsgs[i].Class != class {
			continue
		}
		if content == "" {
			continue
		}
		if s.Similarity(pmsgs[i].Content, content) > 0.9 {
			pmsgs[i] = new
			ok = true
			break
		}
	}
	if !ok {
		pmsgs = append(pmsgs, new)
	}
	plugin_messages.Set(uuid, utils.JsonMarshal(pmsgs))
}

func GetPluginMessage(uuid string) []PMsg {
	var data = plugin_messages.GetBytes(uuid)
	pmsgs := []PMsg{}
	json.Unmarshal(data, &pmsgs)
	return pmsgs
}
