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.video;
017
018import com.fasterxml.jackson.annotation.JsonIgnore;
019import com.fasterxml.jackson.annotation.JsonProperty;
020import com.vonage.client.JsonableBaseObject;
021import java.util.Objects;
022
023/**
024 * Defines the properties used for {@link VideoClient#updateArchiveLayout(String, StreamCompositionLayout)}.
025 */
026public class StreamCompositionLayout extends JsonableBaseObject {
027        @JsonIgnore String id;
028        private ScreenLayoutType type, screenshareType;
029        private String stylesheet;
030
031        protected StreamCompositionLayout() {
032        }
033
034        StreamCompositionLayout(Builder builder) {
035                type = Objects.requireNonNull(builder.type, "Initial layout type is required.");
036                if ((screenshareType = builder.screenshareType) != null && type != ScreenLayoutType.BEST_FIT) {
037                        throw new IllegalStateException(
038                                "Layout type must be set to '"+ScreenLayoutType.BEST_FIT+"' when setting the screen share layout."
039                        );
040                }
041                if (screenshareType == ScreenLayoutType.CUSTOM) {
042                        throw new IllegalArgumentException("Screen share type cannot be '"+ScreenLayoutType.CUSTOM+"'.");
043                }
044                if ((stylesheet = builder.stylesheet) != null && type != ScreenLayoutType.CUSTOM) {
045                        throw new IllegalStateException(
046                                "Layout type must be set to '"+ ScreenLayoutType.CUSTOM+"' when setting the stylesheet."
047                        );
048                }
049                if (type == ScreenLayoutType.CUSTOM && stylesheet == null) {
050                        throw new IllegalStateException(
051                                "Stylesheet must be set when initial layout type is set to '"+ ScreenLayoutType.CUSTOM+"'."
052                        );
053                }
054        }
055
056        /**
057         * @return The initial layout.
058         */
059        @JsonProperty("type")
060        public ScreenLayoutType getType() {
061                return type;
062        }
063
064        /**
065         * @return The layout when screen sharing.
066         */
067        @JsonProperty("screenshareType")
068        public ScreenLayoutType getScreenshareType() {
069                return screenshareType;
070        }
071
072        /**
073         * @return The stylesheet if using the {@link ScreenLayoutType#CUSTOM} layout.
074         */
075        @JsonProperty("stylesheet")
076        public String getStylesheet() {
077                return stylesheet;
078        }
079
080        /**
081         * Entry point for constructing an  object.
082         *
083         * @param initialLayout Initial layout for the composed archive.
084         *
085         * @return A new Builder.
086         */
087        public static Builder builder(ScreenLayoutType initialLayout) {
088                return new Builder(initialLayout);
089        }
090        
091        public static class Builder {
092                private final ScreenLayoutType type;
093                private ScreenLayoutType screenshareType;
094                private String stylesheet;
095        
096                Builder(ScreenLayoutType type) {
097                        this.type = type;
098                }
099
100                /**
101                 * (OPTIONAL) The layout type to use when there is a screen-sharing stream in the session.
102                 * If you set this property, the initial layout must be set to {@link ScreenLayoutType#BEST_FIT}
103                 * and the {@linkplain #stylesheet(String)} property left unset.
104                 *
105                 * @param screenshareType The screen share layout type.
106                 * @return This builder.
107                 */
108                public Builder screenshareType(ScreenLayoutType screenshareType) {
109                        this.screenshareType = screenshareType;
110                        return this;
111                }
112
113                /**
114                 * Used for the {@linkplain ScreenLayoutType#CUSTOM} layout to define the visual layout.
115                 * For other layout types, do not set this property.
116                 *
117                 * @param stylesheet The CSS code to use for the layout. For example: <br>
118                 * {@code stream.instructor {position: absolute; width: 100%;  height:50%;}}
119                 *
120                 * @return This builder.
121                 */
122                public Builder stylesheet(String stylesheet) {
123                        this.stylesheet = stylesheet;
124                        return this;
125                }
126
127        
128                /**
129                 * Builds the {@linkplain StreamCompositionLayout}.
130                 *
131                 * @return An instance of StreamCompositionLayout, populated with all fields from this builder.
132                 */
133                public StreamCompositionLayout build() {
134                        return new StreamCompositionLayout(this);
135                }
136        }
137}