package applicationservice

import (
	"bitbucket.org/nemt/nemt-portal-api/application/entitymapping"
	"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
	"bitbucket.org/nemt/nemt-portal-api/domain/service"
)

// providerService holds methods to provider application service
type profileService struct {
	svc       *service.Service
	mapEntity *entitymapping.Mapper
}

// newProviderService returns a providerService instance
func newProfileService(svc *service.Service, mapper *entitymapping.Mapper) *profileService {
	return &profileService{
		svc:       svc,
		mapEntity: mapper,
	}
}

func (s *profileService) GetAll() ([]viewmodel.Profile, error) {
	result, err := s.svc.Profile.GetAll()
	if err != nil {
		return nil, err
	}
	return s.mapEntity.Profile.ToProfileModelSlice(result), nil
}

func (s *profileService) GetByKey(key string) (viewmodel.Profile, error) {
	result, err := s.svc.Profile.GetByKey(key)
	if err != nil {
		return viewmodel.Profile{}, err
	}
	return s.mapEntity.Profile.ToProfileModel(result), nil
}

func (s *profileService) GetVisibles(visible bool) ([]viewmodel.Profile, error) {
	result, err := s.svc.Profile.GetVisibles(visible)
	if err != nil {
		return nil, err
	}
	return s.mapEntity.Profile.ToProfileModelSlice(result), nil
}

func (s *profileService) GetByOrganizationType(organizationTypeKey string) ([]viewmodel.Profile, error) {
	orgType, err := s.svc.Organization.GetTypeByKey(organizationTypeKey)
	if err != nil {
		return nil, err
	}

	result, err := s.svc.Profile.GetByOrganizationType(orgType.ID)
	if err != nil {
		return nil, err
	}
	return s.mapEntity.Profile.ToProfileModelSlice(result), nil
}
