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.conversations;
017
018import com.fasterxml.jackson.annotation.JsonIgnore;
019import com.vonage.client.Jsonable;
020import com.fasterxml.jackson.annotation.JsonProperty;
021import com.vonage.client.JsonableBaseObject;
022import java.util.Objects;
023
024/**
025 * Options for updating a membership using {@link ConversationsClient#updateMember(UpdateMemberRequest)}.
026 */
027public final class UpdateMemberRequest extends ConversationResourceRequestWrapper implements Jsonable {
028        private final MemberState state;
029        private final String from;
030        @JsonProperty("reason") private final Reason reason;
031
032        UpdateMemberRequest(Builder builder) {
033                super(builder.conversationId, builder.memberId);
034                switch (state = Objects.requireNonNull(builder.state, "State is required.")) {
035                        case JOINED: case LEFT: break;
036                        default: throw new IllegalArgumentException("Invalid state: "+state);
037                }
038                if ((reason = builder.reason) != null && state != MemberState.LEFT) {
039                        throw new IllegalStateException("Reason is only applicable when leaving.");
040                }
041                from = builder.from;
042        }
043
044        static final class Reason extends JsonableBaseObject {
045                @JsonProperty("code") String code;
046                @JsonProperty("text") String text;
047        }
048
049        /**
050         * State to transition the member into.
051         *
052         * @return The updated state as an enum.
053         */
054        @JsonProperty("state")
055        public MemberState getState() {
056                return state;
057        }
058
059        /**
060         * TODO document this.
061         *
062         * @return The from, or {@code null} if unspecified.
063         */
064        @JsonProperty("from")
065        public String getFrom() {
066                return from;
067        }
068
069        /**
070         * Reason code for leaving. Only applicable when {@linkplain #getState()} is {@linkplain MemberState#LEFT}.
071         *
072         * @return The reason code, or {@code null} if unspecified / not applicable.
073         */
074        @JsonIgnore
075        public String getCode() {
076                return reason != null ? reason.code : null;
077        }
078
079        /**
080         * Reason text for leaving. Only applicable when {@linkplain #getState()} is {@linkplain MemberState#LEFT}.
081         *
082         * @return The reason text, or {@code null} if unspecified / not applicable.
083         */
084        @JsonIgnore
085        public String getText() {
086                return reason != null ? reason.text : null;
087        }
088
089        /**
090         * Unique Conversation identifier.
091         *
092         * @return The conversation ID.
093         */
094        @JsonIgnore
095        public String getConversationId() {
096                return conversationId;
097        }
098
099        /**
100         * Unique Member identifier.
101         *
102         * @return The member ID.
103         */
104        @JsonIgnore
105        public String getMemberId() {
106                return resourceId;
107        }
108
109        /**
110         * Entry point for constructing an instance of this class.
111         * 
112         * @return A new Builder.
113         */
114        public static Builder builder() {
115                return new Builder();
116        }
117
118        /**
119         * Builder for constructing an UpdateMemberRequest. Note the mandatory parameters.
120         */
121        public static final class Builder {
122                private String conversationId, memberId, from;
123                private MemberState state;
124                private Reason reason;
125        
126                Builder() {}
127
128                /**
129                 * (REQUIRED) Unique conversation identifier.
130                 *
131                 * @param conversationId The conversation ID.
132                 *
133                 * @return This builder.
134                 */
135                public Builder conversationId(String conversationId) {
136                        this.conversationId = conversationId;
137                        return this;
138                }
139
140                /**
141                 * (REQUIRED) Unique member identifier.
142                 *
143                 * @param memberId The member ID.
144                 *
145                 * @return This builder.
146                 */
147                public Builder memberId(String memberId) {
148                        this.memberId = memberId;
149                        return this;
150                }
151
152                /**
153                 * (REQUIRED) State to transition the member into.
154                 *
155                 * @param state The updated state as an enum.
156                 *
157                 * @return This builder.
158                 */
159                public Builder state(MemberState state) {
160                        this.state = state;
161                        return this;
162                }
163
164                /**
165                 * TODO document this
166                 *
167                 * @param from The from (??)
168                 *
169                 * @return This builder.
170                 */
171                public Builder from(String from) {
172                        this.from = from;
173                        return this;
174                }
175
176                /**
177                 * Reason code for leaving.
178                 * Only applicable when {@linkplain #state(MemberState)} is {@linkplain MemberState#LEFT}.
179                 *
180                 * @param code The reason code as a string.
181                 *
182                 * @return This builder.
183                 */
184                public Builder code(String code) {
185                        if (reason == null) reason = new Reason();
186                        reason.code = code;
187                        return this;
188                }
189
190                /**
191                 * Reason text for leaving.
192                 * Only applicable when {@linkplain #state(MemberState)} is {@linkplain MemberState#LEFT}.
193                 *
194                 * @param text The reason text.
195                 *
196                 * @return This builder.
197                 */
198                public Builder text(String text) {
199                        if (reason == null) reason = new Reason();
200                        reason.text = text;
201                        return this;
202                }
203        
204                /**
205                 * Builds the {@linkplain UpdateMemberRequest}.
206                 *
207                 * @return An instance of UpdateMemberRequest, populated with all fields from this builder.
208                 */
209                public UpdateMemberRequest build() {
210                        return new UpdateMemberRequest(this);
211                }
212        }
213}