package http

import (
	"github.com/gin-gonic/gin"
	"gitlink.org.cn/cloudream/common/pkgs/logger"
	hubapi "gitlink.org.cn/cloudream/jcs-pub/hub/sdk/api"
)

type Server struct {
	engine     *gin.Engine
	listenAddr string
	svc        *Service
}

func NewServer(listenAddr string, svc *Service) (*Server, error) {
	engine := gin.New()

	return &Server{
		engine:     engine,
		listenAddr: listenAddr,
		svc:        svc,
	}, nil
}

func (s *Server) Serve() error {
	s.initRouters()

	logger.Infof("start serving http at: %s", s.listenAddr)
	err := s.engine.Run(s.listenAddr)

	if err != nil {
		logger.Infof("http stopped with error: %s", err.Error())
		return err
	}

	logger.Infof("http stopped")
	return nil
}

func (s *Server) initRouters() {
	s.engine.GET(hubapi.GetStreamPath, s.IOSvc().GetStream)
	s.engine.POST(hubapi.SendStreamPath, s.IOSvc().SendStream)
	s.engine.POST(hubapi.ExecuteIOPlanPath, s.IOSvc().ExecuteIOPlan)
	s.engine.POST(hubapi.SendVarPath, s.IOSvc().SendVar)
	s.engine.GET(hubapi.GetVarPath, s.IOSvc().GetVar)
}
