package qq_message

import (
	"encoding/json"
	"fmt"
	"regexp"
)

// FaceMessage QQ表情消息
type FaceMessage struct {
	Type string          `json:"type"`
	Data FaceMessageData `json:"data"`
}

type FaceMessageData struct {
	ID string `json:"id"`
}

func init() {
	RegisterMessageType(TypeFace, func() QQMessage {
		return NewFaceMessage()
	})
}

func NewFaceMessage() *FaceMessage {
	return &FaceMessage{Type: TypeFace}
}

func (msg *FaceMessage) GetMessageType() string {
	return TypeFace
}

func (msg *FaceMessage) SetData(data json.RawMessage) error {
	return json.Unmarshal(data, &msg.Data)
}

func (msg *FaceMessage) ToCQString() string {
	return fmt.Sprintf("[CQ:face,id=%s]", msg.Data.ID)
}

func (msg *FaceMessage) ParseMessage(data string) error {
	re := regexp.MustCompile(`\[CQ:face,id=(\d+)\]`)
	matches := re.FindStringSubmatch(data)
	if len(matches) < 2 {
		return fmt.Errorf("表情数据格式不正确")
	}
	msg.Data.ID = matches[1]
	return nil
}
