package api

import (
	"easyaudioencode/internal/core/host"
	"easyaudioencode/internal/core/media"
	"encoding/json"
	"log/slog"
)

type PluginGRPC struct {
	Core  *host.Core
	Media *media.Core
	uc    *Usecase
}

func NewPluginGRPC(core *host.Core, mediaCore *media.Core, uc *Usecase) *PluginGRPC {
	return &PluginGRPC{
		Core:  core,
		Media: mediaCore,
		uc:    uc,
	}
}

// 与Host刚建立连接就会通信，如果回调函数放在了API层，来不及注册回调，影响通信

func RegisterPluginGRPC(hostCore *host.Core, mediaCore *media.Core, uc *Usecase) {
	hostGRPC := NewPluginGRPC(hostCore, mediaCore, uc)
	plugin := hostCore.Plugin
	plugin.AddResponseHandler("start", hostGRPC.start)
	// 这部分是主动处理Host的请求

}

func (pg PluginGRPC) start(requestID string, args json.RawMessage) (interface{}, error) {
	slog.Info("Received 'start' from host", "request_id", requestID, "args", args)
	return map[string]interface{}{
		"status": "started",
		"task":   "task",
	}, nil
}
