001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.messages;
017
018import com.fasterxml.jackson.annotation.JsonCreator;
019import com.fasterxml.jackson.annotation.JsonValue;
020import static com.vonage.client.messages.MessageType.*;
021import java.util.EnumSet;
022import java.util.Set;
023import java.util.stream.Collectors;
024
025/**
026 * Represents the services available for sending messages.
027 */
028public enum Channel {
029        SMS (TEXT),
030        MMS (TEXT, IMAGE, VCARD, AUDIO, VIDEO),
031        RCS (TEXT, IMAGE, VIDEO, FILE, CUSTOM, AUDIO, LOCATION, VCARD, REPLY, BUTTON),
032        WHATSAPP (TEXT, IMAGE, AUDIO, VIDEO, FILE, TEMPLATE, CUSTOM, LOCATION,
033                        STICKER, ORDER, REPLY, REACTION, CONTACT, BUTTON, UNSUPPORTED),
034        MESSENGER (TEXT, IMAGE, AUDIO, VIDEO, FILE, UNSUPPORTED),
035        VIBER (TEXT, IMAGE, VIDEO, FILE);
036
037        private final Set<MessageType> supportedTypes;
038
039        Channel(MessageType type1, MessageType... additionalTypes) {
040                this.supportedTypes = EnumSet.of(type1, additionalTypes);
041        }
042
043        /**
044         * This method is useful for determining which message types are applicable to this messaging service.
045         *
046         * @return The Set of message types that this service can handle.
047         */
048        public Set<MessageType> getSupportedMessageTypes() {
049                return supportedTypes;
050        }
051
052        /**
053         * Similar to {@link #getSupportedMessageTypes()} but excludes message types used only for inbound / webhooks.
054         *
055         * @return The Set of message types that this service can send.
056         * @since 7.5.0
057         */
058        public Set<MessageType> getSupportedOutboundMessageTypes() {
059                return getSupportedMessageTypes().stream().filter(mt -> mt != MessageType.UNSUPPORTED &&
060                                mt != MessageType.REPLY && mt != MessageType.ORDER &&
061                                mt != MessageType.CONTACT && mt != MessageType.BUTTON &&
062                                (this != Channel.MMS || mt != MessageType.TEXT) &&
063                                (this != Channel.RCS || (
064                                        mt != AUDIO && mt != LOCATION && mt != BUTTON && mt != VCARD
065                                ))
066                ).collect(Collectors.toSet());
067        }
068
069        @JsonCreator
070        public static Channel fromString(String value) {
071                if (value == null) return null;
072                String upper = value.toUpperCase();
073                return upper.equals("VIBER_SERVICE") ? VIBER : Channel.valueOf(upper);
074        }
075
076        @JsonValue
077        @Override
078        public String toString() {
079                if (this == VIBER) {
080                        return "viber_service";
081                }
082                return name().toLowerCase();
083        }
084}