package com.platform.plugs.live.responseEntity;

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

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

/**
 * @Author TONG
 * @Date 2019/3/4 11:55
 **/
@Data
public class StartCaster {
    /** PVW场景信息列表*/
    List<PvwSceneInfo> pvwSceneInfos;
    /** PGM场景信息列表*/
    List<PgmSceneInfo> pgmSceneInfos;

    @Data
    public static class PvwSceneInfo {
        /** 场景ID*/
        private String sceneId;
        /** 导播台场景对应播放流地址，非旁路输出流地址*/
        private String streamUrl;

        public static PvwSceneInfo parse(StartCasterResponse.SceneInfo data) {
            PvwSceneInfo info = new PvwSceneInfo();
            info.setSceneId(data.getSceneId());
            info.setStreamUrl(data.getStreamUrl());
            return info;
        }

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

    @Data
    public static class PgmSceneInfo {
        /** 场景ID*/
        private String sceneId;
        /** 导播台场景对应播放流地址，非旁路输出流地址*/
        private String streamUrl;
        /** 旁路输出播放地址列表*/
        private List<StreamInfo> streamInfos;

        public static PgmSceneInfo parse(StartCasterResponse.SceneInfo1 data) {
            PgmSceneInfo info = new PgmSceneInfo();
            info.setSceneId(data.getSceneId());
            info.setStreamUrl(data.getStreamUrl());
            List<StreamInfo> streamInfos = new ArrayList<>();
            List<StartCasterResponse.SceneInfo1.StreamInfo> streams = data.getStreamInfos();
            if(streams != null && streams.size() > 0)
                for(StartCasterResponse.SceneInfo1.StreamInfo stream : streams) {
                    StreamInfo streamInfo = StreamInfo.parse(stream);
                    streamInfos.add(streamInfo);
                }
            info.setStreamInfos(streamInfos);
            return info;
        }

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

    public static StartCaster parse(StartCasterResponse data) {

        List<StartCasterResponse.SceneInfo> dataPvws = data.getPvwSceneInfos();

        List<StartCasterResponse.SceneInfo1> dataPgms = data.getPgmSceneInfos();
        StartCaster caster = new StartCaster();
        List<PvwSceneInfo> pvwSceneInfos = new ArrayList<>();
        List<PgmSceneInfo> pgmSceneInfos = new ArrayList<>();

        if(dataPvws != null && dataPvws.size() > 0) {
            for (StartCasterResponse.SceneInfo obj : dataPvws) {
                PvwSceneInfo pvwSceneIfo = PvwSceneInfo.parse(obj);
                pvwSceneInfos.add(pvwSceneIfo);
            }
        }
        if(dataPgms != null && dataPgms.size() > 0) {
            for (StartCasterResponse.SceneInfo1 obj : dataPgms) {
                PgmSceneInfo pgmSceneInfo = PgmSceneInfo.parse(obj);
                pgmSceneInfos.add(pgmSceneInfo);
            }
        }
        caster.setPgmSceneInfos(pgmSceneInfos);
        caster.setPvwSceneInfos(pvwSceneInfos);
        return caster;
    }
}
