package com.platform.plugs.live.responseEntity;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.live.model.v20161101.DescribeCasterLayoutsResponse;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * Description:
 * Author:zj
 * Date:2019/3/4 18:47
 */
@Data
@NoArgsConstructor
public class CasterLayout {

    /**
     * 布局Id
     */
    private String layOutId;
    /**
     * videolayer配置列表
     */
    private List<AudioLayer> audioLayers;
    /**
     * Audiolayer配置列表
     */
    private List<VideoLayer> videoLayers;
    /**
     * 位置关联列表，与VideoLayers顺序保持一致
     */
    private List<String> blendList;
    /**
     * 位置关联列表，与AudioLayers顺序保持一致
     */
    private List<String> mixList;

    public static CasterLayout parse(DescribeCasterLayoutsResponse.Layout data) {
        CasterLayout casterLayout = new CasterLayout();
        casterLayout.setLayOutId(data.getLayoutId());
        List<DescribeCasterLayoutsResponse.Layout.AudioLayer> requestAudioLayers = data.getAudioLayers();
        List<DescribeCasterLayoutsResponse.Layout.VideoLayer> requestVideoLayers = data.getVideoLayers();
        List<String> blendList = data.getBlendList();
        List<String> mixList =  data.getMixList();

        List<AudioLayer> audioLayerEntities = new ArrayList<>();
        List<VideoLayer> videoLayerEntities = new ArrayList<>();

        if (requestAudioLayers != null && requestAudioLayers.size() > 0) {
            for (DescribeCasterLayoutsResponse.Layout.AudioLayer obj : requestAudioLayers) {
                AudioLayer audioLayer = new AudioLayer();
                audioLayer.setVolumeRate(obj.getVolumeRate());
                audioLayer.setValidChannel(obj.getValidChannel());
                audioLayer.setFixedDelayDuration(obj.getFixedDelayDuration());
                audioLayerEntities.add(audioLayer);
            }
        }

        if (requestVideoLayers != null && requestVideoLayers.size() >= 0) {
            for(DescribeCasterLayoutsResponse.Layout.VideoLayer obj : requestVideoLayers) {
                VideoLayer videoLayer = new VideoLayer();
                videoLayer.setFillMode(obj.getFillMode());
                videoLayer.setFixedDelayDuration(obj.getFixedDelayDuration());
                videoLayer.setHeightNormalized(obj.getHeightNormalized());
                List<Float> positionNormalized = new ArrayList<>();
                if(obj.getPositionNormalizeds() != null && obj.getPositionNormalizeds().size() > 0) {
                    for(String position : obj.getPositionNormalizeds()) {
                        positionNormalized.add(Float.valueOf(position));
                    }
                }
                videoLayer.setPositionNormalized(positionNormalized);
                videoLayer.setPositionRefer(obj.getPositionRefer());
                videoLayer.setWidthNormalized(obj.getWidthNormalized());
                videoLayerEntities.add(videoLayer);
            }
        }
        casterLayout.setAudioLayers(audioLayerEntities);
        casterLayout.setVideoLayers(videoLayerEntities);
        casterLayout.setBlendList(blendList);
        casterLayout.setMixList(mixList);
        return casterLayout;
    }
}
