package serverimp

import (
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/server"
	"go.uber.org/zap"
)

type Instance struct {
	srv     server.Application
	logger  mlogger.Logger
	file    string
	debug   bool
	factory server.ServerFactoryT
}

func (i *Instance) Start() error {
	var err error
	if i.srv, err = i.factory(i.logger, i.file, i.debug); err != nil {
		i.logger.Warn("Failed to create server instance", zap.Error(err))
		return err
	}
	return i.srv.Start()
}

func (i *Instance) Shutdown() {
	if i.srv != nil {
		i.srv.Shutdown()
	}
}

func NewInstance(factory server.ServerFactoryT, logger mlogger.Logger, file string, debug bool) *Instance {
	return &Instance{
		srv:     nil,
		logger:  logger,
		file:    file,
		debug:   debug,
		factory: factory,
	}
}
