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"
)

// zipcodeService holds methods to participating zipcode application service
type passwordResetService struct {
	svc       *service.Service
	mapEntity *entitymapping.Mapper
}

// newZipcodeService returns a zipcodeService instance
func newPasswordResetService(svc *service.Service, mapper *entitymapping.Mapper) *passwordResetService {
	return &passwordResetService{
		svc:       svc,
		mapEntity: mapper,
	}
}

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

func (s *passwordResetService) CreatePasswordResetEntry(passwordResetEntry viewmodel.PasswordReset) (viewmodel.PasswordReset, error) {
	passwordResetEntity := s.mapEntity.PasswordReset.ToPasswordResetEntity(passwordResetEntry)
	result, err := s.svc.PasswordReset.CreatePasswordResetEntry(passwordResetEntity)
	if err != nil {
		return viewmodel.PasswordReset{}, err
	}
	return s.mapEntity.PasswordReset.ToPasswordResetModel(result), nil
}

func (s *passwordResetService) GetByID(ID int64) (viewmodel.PasswordReset, error) {
	result, err := s.svc.PasswordReset.GetByID(ID)
	if err != nil {
		return viewmodel.PasswordReset{}, err
	}
	return s.mapEntity.PasswordReset.ToPasswordResetModel(result), nil
}

func (s *passwordResetService) GetByToken(token string) (viewmodel.PasswordReset, error) {
	result, err := s.svc.PasswordReset.GetByToken(token)
	if err != nil {
		return viewmodel.PasswordReset{}, err
	}
	return s.mapEntity.PasswordReset.ToPasswordResetModel(result), nil
}

func (s *passwordResetService) SetTokenOpened(token string) error {
	return s.svc.PasswordReset.SetTokenOpened(token)
}

func (s *passwordResetService) SetTokenUsed(token string) error {
	return s.svc.PasswordReset.SetTokenUsed(token)
}
