package service

import (
	"reflect"

	cda "git.loafle.net/commons/di-go/annotation"
	cdr "git.loafle.net/commons/di-go/registry"
	occp "git.loafle.net/overflow/commons-go/config/probe"
	"git.loafle.net/overflow/commons-go/core/util"
	ocmd "git.loafle.net/overflow/commons-go/model/discovery"
	ocsp "git.loafle.net/overflow/commons-go/service/probe"

	// For annotation
	_ "git.loafle.net/overflow/commons-go/core/annotation"
)

var DiscoveryServiceType = reflect.TypeOf((*DiscoveryService)(nil))

func init() {
	cdr.RegisterType(DiscoveryServiceType)
}

type DiscoveryService struct {
	ocsp.DiscoveryService
	cda.TypeAnnotation `annotation:"@overflow:RPCService()"`

	ContainerService   *ContainerService   `annotation:"@Inject()"`
	ProbeClientService *ProbeClientService `annotation:"@Inject()"`
}

func (s *DiscoveryService) InitService() error {

	return nil
}

func (s *DiscoveryService) StartService() error {

	return nil
}

func (s *DiscoveryService) StopService() {

}

func (s *DiscoveryService) DestroyService() {

}

func (s *DiscoveryService) DiscoverZone(requesterID string, dz *ocmd.DiscoverZone) error {
	return s.ContainerService.Send(occp.ContainerDiscovery, "DiscoveryService.DiscoverZone", requesterID, dz)
}

func (s *DiscoveryService) DiscoverHost(requesterID string, zone *ocmd.Zone, dh *ocmd.DiscoverHost) error {
	return s.ContainerService.Send(occp.ContainerDiscovery, "DiscoveryService.DiscoverHost", requesterID, zone, dh)
}

func (s *DiscoveryService) DiscoverPort(requesterID string, host *ocmd.Host, dp *ocmd.DiscoverPort) error {
	return s.ContainerService.Send(occp.ContainerDiscovery, "DiscoveryService.DiscoverPort", requesterID, host, dp)
}

func (s *DiscoveryService) DiscoverService(requesterID string, port *ocmd.Port, dService *ocmd.DiscoverService) error {
	return s.ContainerService.Send(occp.ContainerDiscovery, "DiscoveryService.DiscoverService", requesterID, port, dService)
}

func (s *DiscoveryService) StopDiscovery(requesterID string) error {
	return s.ContainerService.Send(occp.ContainerDiscovery, "DiscoveryService.StopDiscovery", requesterID)
}

// use by discovery
func (s *DiscoveryService) DiscoveryStart(requesterID string, t *util.Timestamp) error {
	return s.ProbeClientService.Send("DiscoveryService.discoveryStart", requesterID, t)
}

func (s *DiscoveryService) DiscoveryStop(requesterID string, t *util.Timestamp) error {
	return s.ProbeClientService.Send("DiscoveryService.discoveryStop", requesterID, t)
}

func (s *DiscoveryService) DiscoveredZone(requesterID string, zone *ocmd.Zone) error {
	return s.ProbeClientService.Send("DiscoveryService.discoveredZone", requesterID, zone)
}

func (s *DiscoveryService) DiscoveredHost(requesterID string, host *ocmd.Host) error {
	return s.ProbeClientService.Send("DiscoveryService.discoveredHost", requesterID, host)
}

func (s *DiscoveryService) DiscoveredPort(requesterID string, port *ocmd.Port) error {
	return s.ProbeClientService.Send("DiscoveryService.discoveredPort", requesterID, port)
}

func (s *DiscoveryService) DiscoveredService(requesterID string, service *ocmd.Service) error {
	return s.ProbeClientService.Send("DiscoveryService.discoveredService", requesterID, service)
}
