package hubrpc

import (
	context "context"
	"io"

	"gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
	rpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc"
)

type IOSwitchSvc interface {
	ExecuteIOPlan(ctx context.Context, req *ExecuteIOPlan) (*ExecuteIOPlanResp, *rpc.CodeError)
	SendIOStream(ctx context.Context, req *SendIOStream) (*SendIOStreamResp, *rpc.CodeError)
	GetIOStream(ctx context.Context, req *GetIOStream) (*GetIOStreamResp, *rpc.CodeError)
	SendIOVar(ctx context.Context, req *SendIOVar) (*SendIOVarResp, *rpc.CodeError)
	GetIOVar(ctx context.Context, req *GetIOVar) (*GetIOVarResp, *rpc.CodeError)
}

// 执行IO计划
type ExecuteIOPlan struct {
	Plan exec.Plan
}
type ExecuteIOPlanResp struct{}

func (c *Client) ExecuteIOPlan(ctx context.Context, req *ExecuteIOPlan) (*ExecuteIOPlanResp, *rpc.CodeError) {
	if c.fusedErr != nil {
		return nil, c.fusedErr
	}
	return rpc.UnaryClient[*ExecuteIOPlanResp](c.cli.ExecuteIOPlan, ctx, req)
}

func (s *Server) ExecuteIOPlan(ctx context.Context, req *rpc.Request) (*rpc.Response, error) {
	return rpc.UnaryServer(s.svrImpl.ExecuteIOPlan, ctx, req)
}

// 发送IO流
type SendIOStream struct {
	PlanID exec.PlanID
	VarID  exec.VarID
	Stream io.Reader `json:"-"`
}

func (s *SendIOStream) GetStream() io.Reader {
	return s.Stream
}
func (s *SendIOStream) SetStream(str io.Reader) {
	s.Stream = str
}

type SendIOStreamResp struct{}

func (c *Client) SendIOStream(ctx context.Context, req *SendIOStream) (*SendIOStreamResp, *rpc.CodeError) {
	if c.fusedErr != nil {
		return nil, c.fusedErr
	}
	return rpc.UploadStreamClient[*SendIOStreamResp](c.cli.SendIOStream, ctx, req)
}
func (s *Server) SendIOStream(req Hub_SendIOStreamServer) error {
	return rpc.UploadStreamServer(s.svrImpl.SendIOStream, req)
}

// 获取IO流
type GetIOStream struct {
	PlanID   exec.PlanID
	VarID    exec.VarID
	SignalID exec.VarID
	Signal   exec.VarValue
}

type GetIOStreamResp struct {
	Stream io.ReadCloser `json:"-"`
}

func (r *GetIOStreamResp) GetStream() io.ReadCloser {
	return r.Stream
}
func (r *GetIOStreamResp) SetStream(str io.ReadCloser) {
	r.Stream = str
}

func (c *Client) GetIOStream(ctx context.Context, req *GetIOStream) (*GetIOStreamResp, *rpc.CodeError) {
	if c.fusedErr != nil {
		return nil, c.fusedErr
	}

	return rpc.DownloadStreamClient[*GetIOStreamResp](c.cli.GetIOStream, ctx, req)
}
func (s *Server) GetIOStream(req *rpc.Request, ret Hub_GetIOStreamServer) error {
	return rpc.DownloadStreamServer(s.svrImpl.GetIOStream, req, ret)
}

// 发送IO变量
type SendIOVar struct {
	PlanID exec.PlanID
	VarID  exec.VarID
	Value  exec.VarValue
}
type SendIOVarResp struct{}

func (c *Client) SendIOVar(ctx context.Context, req *SendIOVar) (*SendIOVarResp, *rpc.CodeError) {
	if c.fusedErr != nil {
		return nil, c.fusedErr
	}
	return rpc.UnaryClient[*SendIOVarResp](c.cli.SendIOVar, ctx, req)
}

func (s *Server) SendIOVar(ctx context.Context, req *rpc.Request) (*rpc.Response, error) {
	return rpc.UnaryServer(s.svrImpl.SendIOVar, ctx, req)
}

// 获取IO变量
type GetIOVar struct {
	PlanID   exec.PlanID
	VarID    exec.VarID
	SignalID exec.VarID
	Signal   exec.VarValue
}
type GetIOVarResp struct {
	Value exec.VarValue
}

func (c *Client) GetIOVar(ctx context.Context, req *GetIOVar) (*GetIOVarResp, *rpc.CodeError) {
	if c.fusedErr != nil {
		return nil, c.fusedErr
	}
	return rpc.UnaryClient[*GetIOVarResp](c.cli.GetIOVar, ctx, req)
}

func (s *Server) GetIOVar(ctx context.Context, req *rpc.Request) (*rpc.Response, error) {
	return rpc.UnaryServer(s.svrImpl.GetIOVar, ctx, req)
}
