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.meetings;
017
018import com.fasterxml.jackson.annotation.JsonIgnore;
019import com.fasterxml.jackson.annotation.JsonProperty;
020import com.vonage.client.Jsonable;
021import com.vonage.client.JsonableBaseObject;
022import java.net.URI;
023import java.util.UUID;
024import java.util.regex.Pattern;
025
026public class Theme extends JsonableBaseObject {
027        static final Pattern COLOR_PATTERN = Pattern.compile("(#[a-fA-F0-9]{6}|[a-fA-F0-9]{3})");
028
029        @JsonIgnore private boolean update = false;
030        private UUID themeId, applicationId;
031        private ThemeDomain domain;
032        private String themeName, mainColor, accountId, shortCompanyUrl,
033                        brandText, brandImageColored, brandImageWhite, brandedFavicon;
034        private URI brandImageColoredUrl, brandImageWhiteUrl, brandedFaviconUrl;
035
036        protected Theme() {
037        }
038
039        Theme(Builder builder) {
040                if ((mainColor = builder.mainColor) != null && !COLOR_PATTERN.matcher(mainColor).matches()) {
041                        throw new IllegalArgumentException("Main color must be a valid hex pallet.");
042                }
043
044                if ((brandText = builder.brandText) != null) {
045                        if (brandText.length() > 200) {
046                                throw new IllegalArgumentException("Brand text cannot exceed 200 characters.");
047                        }
048                        else if (brandText.trim().isEmpty()) {
049                                throw new IllegalArgumentException("Brand text cannot be blank.");
050                        }
051                }
052
053                if ((themeName = builder.themeName) != null) {
054                        if (themeName.length() > 200) {
055                                throw new IllegalArgumentException("Theme name cannot exceed 200 characters.");
056                        }
057                        else if (themeName.trim().isEmpty()) {
058                                throw new IllegalArgumentException("Theme name cannot be blank.");
059                        }
060                }
061
062                if ((shortCompanyUrl = builder.shortCompanyUrl) != null) {
063                        if (shortCompanyUrl.length() > 128) {
064                                throw new IllegalArgumentException("Short company URL cannot exceed 128 characters.");
065                        }
066                        else if (shortCompanyUrl.trim().isEmpty()) {
067                                throw new IllegalArgumentException("Short company URL cannot be blank.");
068                        }
069                }
070        }
071
072        @JsonIgnore
073        void setThemeIdAndFlagUpdate(UUID themeId) {
074                this.themeId = themeId;
075                update = true;
076        }
077
078        /**
079         * Unique theme ID.
080         *
081         * @return The theme ID.
082         */
083        @JsonProperty("theme_id")
084        public UUID getThemeId() {
085                return themeId;
086        }
087
088        /**
089         * Theme name.
090         *
091         * @return Name of the theme.
092         */
093        @JsonProperty("theme_name")
094        public String getThemeName() {
095                return themeName;
096        }
097
098        /**
099         * The theme's application domain.
100         *
101         * @return The domain as an enum.
102         */
103        @JsonProperty("domain")
104        public ThemeDomain getDomain() {
105                return domain;
106        }
107
108        /**
109         * Account ID.
110         *
111         * @return The account ID (API key).
112         */
113        @JsonProperty("account_id")
114        public String getAccountId() {
115                return accountId;
116        }
117
118        /**
119         * Unique application ID.
120         *
121         * @return The application ID.
122         */
123        @JsonProperty("application_id")
124        public UUID getApplicationId() {
125                return applicationId;
126        }
127
128        /**
129         * Main colour hex code.
130         *
131         * @return The main theme colour (hex value).
132         */
133        @JsonProperty("main_color")
134        public String getMainColor() {
135                return mainColor;
136        }
137
138        /**
139         * Company name to include in the URL.
140         *
141         * @return The company's short URL.
142         */
143        @JsonProperty("short_company_url")
144        public String getShortCompanyUrl() {
145                return shortCompanyUrl;
146        }
147
148        /**
149         * Brand text that will appear in the application.
150         *
151         * @return The branding text.
152         */
153        @JsonProperty("brand_text")
154        public String getBrandText() {
155                return brandText;
156        }
157
158        /**
159         * Coloured logo's key in storage system.
160         *
161         * @return The brand image in colour.
162         */
163        @JsonProperty("brand_image_colored")
164        public String getBrandImageColored() {
165                return brandImageColored;
166        }
167
168        /**
169         * White logo's key in storage system.
170         *
171         * @return The brand image in white.
172         */
173        @JsonProperty("brand_image_white")
174        public String getBrandImageWhite() {
175                return brandImageWhite;
176        }
177
178        /**
179         * Favicon's key in storage system.
180         *
181         * @return The brand favicon key.
182         */
183        @JsonProperty("branded_favicon")
184        public String getBrandedFavicon() {
185                return brandedFavicon;
186        }
187
188        /**
189         * Coloured logo's link.
190         *
191         * @return The coloured brand image URL.
192         */
193        @JsonProperty("brand_image_colored_url")
194        public URI getBrandImageColoredUrl() {
195                return brandImageColoredUrl;
196        }
197
198        /**
199         * White logo's link.
200         *
201         * @return The white brand image URL.
202         */
203        @JsonProperty("brand_image_white_url")
204        public URI getBrandImageWhiteUrl() {
205                return brandImageWhiteUrl;
206        }
207
208        /**
209         * Favicon's link.
210         *
211         * @return The favicon URL.
212         */
213        @JsonProperty("branded_favicon_url")
214        public URI getBrandedFaviconUrl() {
215                return brandedFaviconUrl;
216        }
217
218        @Override
219        public String toJson() {
220                if (update) {
221                        UUID tempId = themeId;
222                        themeId = null;
223                        String json = "{\"update_details\":" + super.toJson() + "}";
224                        update = false;
225                        themeId = tempId;
226                        return json;
227                }
228                else {
229                        return super.toJson();
230                }
231        }
232
233        /**
234         * Creates an instance of this class from a JSON payload.
235         *
236         * @param json The JSON string to parse.
237         * @return An instance of this class with the fields populated, if present.
238         */
239        public static Theme fromJson(String json) {
240                return Jsonable.fromJson(json);
241        }
242        
243        /**
244         * Entry point for constructing an instance of this class.
245         * 
246         * @return A new Builder.
247         */
248        public static Builder builder() {
249                return new Builder();
250        }
251        
252        public static class Builder {
253                private String themeName, mainColor, brandText, shortCompanyUrl;
254        
255                Builder() {}
256        
257                /**
258                 * (OPTIONAL) The name to give the theme (maximum 200 characters).
259                 *
260                 * @param themeName The theme name.
261                 *
262                 * @return This builder.
263                 */
264                public Builder themeName(String themeName) {
265                        this.themeName = themeName;
266                        return this;
267                }
268
269                /**
270                 * (REQUIRED) The main theme colour code (hex value).
271                 *
272                 * @param mainColor The colour's hex code, including the # character.
273                 *
274                 * @return This builder.
275                 */
276                public Builder mainColor(String mainColor) {
277                        this.mainColor = mainColor;
278                        return this;
279                }
280
281                /**
282                 * (OPTIONAL) The short company URL. Must be a valid URI, cannot exceed 128 characters.
283                 *
284                 * @param shortCompanyUrl The URL as a String.
285                 *
286                 * @return This builder.
287                 */
288                public Builder shortCompanyUrl(String shortCompanyUrl) {
289                        this.shortCompanyUrl = shortCompanyUrl;
290                        return this;
291                }
292
293                /**
294                 * (REQUIRED) The brand / logo text.
295                 *
296                 * @param brandText The text.
297                 *
298                 * @return This builder.
299                 */
300                public Builder brandText(String brandText) {
301                        this.brandText = brandText;
302                        return this;
303                }
304
305        
306                /**
307                 * Builds the {@linkplain Theme}.
308                 *
309                 * @return An instance of Theme, populated with all fields from this builder.
310                 */
311                public Theme build() {
312                        return new Theme(this);
313                }
314        }
315}