package com.platform.plugs.live.responseEntity;

import com.alibaba.fastjson.annotation.JSONField;
import com.aliyuncs.live.model.v20161101.DescribeCasterConfigResponse;
import lombok.Data;

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

/**
 * @Author TONG
 * @Date 2019/3/4 15:20
 **/
@Data
public class CasterConfig {
    /** 导播台Id*/
    private String casterId;
    /** 导播台名称*/
    private String casterName;
    /** 域名*/
    private String domainName;
    /** 延时播放。0：禁用延时，大于0：启用延时。单位：秒*/
    private Float delay;
    /** 备播视频，媒资库素材Id*/
    private String urgentMaterialId;
    /** 用户自定义导播台旁路输出地址*/
    private String sideOutputUrl;
    /** 用户回调地址*/
    private String callbackUrl;
    /** 轮播台名称*/
    private String programName;
    /** 轮播生效标志。0：不生效，1：生效。*/
    private Integer programEffect;
    /** 是否启用Channel。0：不启用Channel，1：启用Channel*/
    private Integer channelEnable;
    /** 转码配置*/
    private TranscodeConfig transcodeConfig;
    /** 录制配置参数为空时表示不启用录制功能*/
    private RecordConfig recordConfig;

    public static CasterConfig parse(DescribeCasterConfigResponse data) {
        CasterConfig casterConfig = new CasterConfig();

        casterConfig.setCasterId(data.getCasterId());
        casterConfig.setCasterName(data.getCasterName());
        casterConfig.setDomainName(data.getDomainName());
        casterConfig.setDelay(data.getDelay());
        casterConfig.setUrgentMaterialId(data.getUrgentMaterialId());
        casterConfig.setSideOutputUrl(data.getSideOutputUrl());
        casterConfig.setCallbackUrl(data.getCallbackUrl());
        casterConfig.setProgramName(data.getProgramName());
        casterConfig.setProgramEffect(data.getProgramEffect());
        casterConfig.setChannelEnable(data.getChannelEnable());
        if(data.getTranscodeConfig() != null) {
            TranscodeConfig transcodeConfig = TranscodeConfig.parse(data.getTranscodeConfig());
            casterConfig.setTranscodeConfig(transcodeConfig);
        }
        if(data.getRecordConfig() != null) {
            RecordConfig recordConfig = RecordConfig.parse(data.getRecordConfig());
            casterConfig.setRecordConfig(recordConfig);
        }
        return casterConfig;
    }

    @Data
    public static class RecordConfig {
        @JSONField(name="OssEndpoint")
        private String ossEndpoint;
        @JSONField(name="OssBucket")
        private String ossBucket;
        @JSONField(name="RecordFormat")
        private List<RecordFormatItem> recordFormat;

        public static RecordConfig parse(DescribeCasterConfigResponse.RecordConfig data) {
            RecordConfig recordConfig = new RecordConfig();
            recordConfig.setOssEndpoint(data.getOssEndpoint());
            recordConfig.setOssBucket(data.getOssBucket());
            List<RecordFormatItem> recordFormatItems = new ArrayList<>();
            List<DescribeCasterConfigResponse.RecordConfig.RecordFormatItem> dataList = data.getRecordFormat();
            if(dataList != null && dataList.size() > 0) {
                for(DescribeCasterConfigResponse.RecordConfig.RecordFormatItem obj : dataList) {
                    RecordFormatItem item = RecordFormatItem.parse(obj);
                    recordFormatItems.add(item);
                }
            }
            recordConfig.setRecordFormat(recordFormatItems);
            return recordConfig;
        }
    }

    @Data
    public static class TranscodeConfig {
        /** 导播台转码模板。lp_ld：流畅 lp_sd：标清 lp_hd：高清 lp_ud：超清 lp_ld_v：竖屏流畅 lp_sd_v：竖屏标清 lp_hd_v：竖屏高清 lp_ud_v：竖屏超清*/
        @JSONField(name="CasterTemplate")
        private String casterTemplate;
        /** 直播转码配置。lsd：标清 lld：流畅 lud：超清 lhd：高清自适应转码模板 daobo-lsd：标清 daobo-lld：流畅 daobo-lud：超清 daobo-lhd：高清*/
        @JSONField(name="LiveTemplate")
        private List<String> liveTemplate;

        public static TranscodeConfig parse(DescribeCasterConfigResponse.TranscodeConfig data) {
            TranscodeConfig transcodeConfig = new TranscodeConfig();
            transcodeConfig.setCasterTemplate(data.getCasterTemplate());
            transcodeConfig.setLiveTemplate(data.getLiveTemplateIds());
            return transcodeConfig;
        }
    }

    @Data
    public static class RecordFormatItem {
        /** 录制格式*/
        @JSONField(name="Format")
        private String format;
        /** 录制文件名*/
        @JSONField(name="OssObjectPrefix")
        private String ossObjectPrefix;
        /** 切片名称*/
        @JSONField(name="SliceOssObjectPrefix")
        private String sliceOssObjectPrefix;
        /** 录制时长*/
        @JSONField(name="CycleDuration")
        private Integer cycleDuration;

        public static RecordFormatItem parse(DescribeCasterConfigResponse.RecordConfig.RecordFormatItem data) {
            RecordFormatItem recordFormatItem = new RecordFormatItem();
            recordFormatItem.setFormat(data.getFormat());
            recordFormatItem.setOssObjectPrefix(data.getOssObjectPrefix());
            recordFormatItem.setSliceOssObjectPrefix(data.getSliceOssObjectPrefix());
            recordFormatItem.setCycleDuration(data.getCycleDuration());
            return recordFormatItem;
        }
    }
}
