package middleware

import (
	"game-driver/internal/schema"
	"game-driver/leaf"
	"game-driver/pkg/relay"
	"go.uber.org/zap"
)

// RelayMaster 继电器中间件
func RelayMaster(r relay.Relay) leaf.HandlerFunc {
	return func(c *leaf.Context) {
		pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
		if r != nil && pm.Power {
			r.On(0)
			defer r.Off(0)

			zap.S().Infoln("开启电源")
			defer zap.S().Infoln("关闭电源")
		} else {
			zap.S().Infoln("继电器未开启/不需要电源控制")
		}
		c.Next()
	}
}
