package com.platform.plugs.live.responseEntity;

import com.aliyuncs.live.model.v20161101.DescribeCasterScenesResponse;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author TONG
 * @Date 2019/3/5 16:23
 **/
@Data
public class CasterScenes {

    private int total;

    private List<Scene> sceneList;

    public static CasterScenes parse(DescribeCasterScenesResponse data) {
        CasterScenes casterScenes = new CasterScenes();
        List<DescribeCasterScenesResponse.Scene> dataSceneList = data.getSceneList();
        List<Scene> sceneList = new ArrayList<>();
        if(dataSceneList != null && dataSceneList.size() > 0) {
            for (DescribeCasterScenesResponse.Scene obj : dataSceneList) {
                Scene scene = Scene.parse(obj);
                sceneList.add(scene);
            }
        }
        casterScenes.setSceneList(sceneList);
        casterScenes.setTotal(data.getTotal());
        return casterScenes;
    }

    @Data
    public static class Scene {
        /** 场景ID*/
        private String sceneId;
        /** 场景名称*/
        private String sceneName;
        /** 是否正式输出。0：输出预监 1：输出正式*/
        private String outputType;
        /** 布局ID*/
        private String layoutId;
        /** 输出流地址*/
        private String streamUrl;
        /** 组件Id列表*/
        private List<String> componentIds;
        /** 场景状态。0：关闭 1：开启*/
        private Integer status;
        /** 播放地址列表*/
        private List<StreamInfo> streamInfos;

        public static Scene parse(DescribeCasterScenesResponse.Scene data) {
            Scene scene = new Scene();
            scene.setSceneId(data.getSceneId());
            scene.setSceneName(data.getSceneName());
            scene.setOutputType(data.getOutputType());
            scene.setLayoutId(data.getLayoutId());
            scene.setStreamUrl(data.getStreamUrl());
            scene.setComponentIds(data.getComponentIds());
            scene.setStatus(data.getStatus());
            List<DescribeCasterScenesResponse.Scene.StreamInfo> dataStreamInfos = data.getStreamInfos();
            List<StreamInfo> streamInfos = new ArrayList<>();
            if(dataStreamInfos != null && dataStreamInfos.size() > 0) {
                for (DescribeCasterScenesResponse.Scene.StreamInfo obj : dataStreamInfos) {
                    StreamInfo info = StreamInfo.parse(obj);
                    streamInfos.add(info);
                }
            }
            scene.setStreamInfos(streamInfos);
            return scene;
        }

        public String getStreamUrl() {
            if (StringUtils.isNotBlank(this.streamUrl)) {
                return this.streamUrl.replaceAll("http://", "https://");
            }
            return null;
        }
    }
}
