package tmobile

import (
	"strings"

	"github.com/fiskerinc/cloud-services/pkg/utils/envtool"
)

type ICCIDFilter struct {
	filter []string
}

// InitFilter creates a filter that filters based on the following criteria
// If the env variable SMS_ICCID_FILTER_LIST is empty
// .	then we will allow all ICCIDs through
// If the filter is NOT empty, then only those specified ICCIDs will be sent
// .	all others will be automatically succeeded
func InitFilter() (filter ICCIDFilter) {
	envVal := envtool.GetEnv("SMS_ICCID_FILTER_LIST", "")
	// No characters, then no need to create the filter
	if len(envVal) == 0 {
		return
	}

	split := strings.Split(envVal, ",")
	for x := range split {
		split[x] = strings.TrimSpace(split[x])
	}
	filter.filter = split
	return
}

// If send is false, then automatically return a success message
func (filter ICCIDFilter) ShouldSend(iccid string) (send bool) {
	if len(filter.filter) == 0 {
		return true
	}

	for _, id := range filter.filter {
		if id == iccid {
			return true
		}
	}

	return false
}

func (filter *ICCIDFilter) SetFilter(filterList []string) {
	filter.filter = filterList
}
