package com.bokecc.livemodule.replay.video;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.replay.DWReplayCoreHandler;
import com.bokecc.livemodule.utils.ImageLoader;
import com.bokecc.livemodule.view.VideoLoadingView;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplay;
import com.bokecc.sdk.mobile.live.stream.live.rtc.HDMediaView;

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


/**
 * CC 回放视频展示控件
 */
public class ReplayVideoView extends RelativeLayout implements DWReplayVideoListener{
    private static final String TAG = "ReplayVideoView";
    private final Context mContext;
    private VideoLoadingView mVideoProgressBar;

    /**
     * 音频模式界面
     */
    private View audioModeRoot;
    private ImageView audioUndulate;
    private FrameLayout mVideoRoot;
    private boolean isNeedListener = false;
    public ReplayVideoView(Context context) {
        super(context);
        this.mContext = context;
        inflateViews();
    }

    public ReplayVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        inflateViews();
    }

    public ReplayVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        inflateViews();
    }

    private void inflateViews() {
        DWReplayCoreHandler.getInstance().setReplayVideoListener(this);
        View mRootView = LayoutInflater.from(mContext).inflate(R.layout.replay_video_view, this);
        mVideoRoot = mRootView.findViewById(R.id.video_root);
        mVideoProgressBar = mRootView.findViewById(R.id.video_progressBar);
        audioModeRoot = mRootView.findViewById(R.id.audio_root);
        audioUndulate = mRootView.findViewById(R.id.iv_audio_undulate);
    }


    public void showProgress() {
        if (mVideoProgressBar != null) {
            mVideoProgressBar.setVisibility(VISIBLE);
        }
    }


    public void dismissProgress() {
        if (mVideoProgressBar != null) {
            mVideoProgressBar.setVisibility(GONE);
        }
    }

    /**
     * 切换音视频模式布局
     *
     * @param playMode DWLive.PlayMode
     */
    public void changeModeLayout(DWLiveReplay.PlayMode playMode) {
        if (playMode == DWLiveReplay.PlayMode.PLAY_MODE_TYEP_AUDIO) {
            audioModeRoot.setVisibility(VISIBLE);
            ImageLoader.loadGifIntoImageView(mContext,R.drawable.gif_audio_undulate,audioUndulate);
        } else {
            audioModeRoot.setVisibility(GONE);
        }
    }


    public void setTextureViewClickable(boolean clickable){
        List<TextureView> list = findAllTextureViews(mVideoRoot);
        if (!list.isEmpty()){
            for (int i = 0; i < list.size(); i++) {
                TextureView textureView = list.get(i);
                textureView.setClickable(clickable);
            }
        }
    }

    public static List<TextureView> findAllTextureViews(ViewGroup container) {
        List<TextureView> textureViews = new ArrayList<>();
        findTextureViews(container, textureViews);
        return textureViews;
    }

    private static void findTextureViews(ViewGroup container, List<TextureView> textureViews) {
        for (int i = 0; i < container.getChildCount(); i++) {
            View child = container.getChildAt(i);
            if (child instanceof TextureView) {
                textureViews.add((TextureView) child);
            } else if (child instanceof ViewGroup) {
                findTextureViews((ViewGroup) child, textureViews);
            }
        }
    }

    @Override
    public void onVideoViewPrepared(HDMediaView hdMediaView) {
        if (mVideoRoot!=null){
            if (mVideoRoot.getChildCount()>0){
                mVideoRoot.removeAllViews();
            }
            mVideoRoot.addView(hdMediaView);
        }
    }

    @Override
    public void onError(int code) {
        dismissProgress();
    }

    @Override
    public void onPrepared() {
        isNeedListener = true;
    }

    @Override
    public void onBufferStart() {
        showProgress();
    }

    @Override
    public void onBufferSpeed(float speed) {
        if (mVideoProgressBar != null) {
            mVideoProgressBar.setSpeed(speed);
        }
    }

    @Override
    public void onBufferEnd() {
        if (!isNeedListener)
            dismissProgress();
    }

    @Override
    public void onPlaying() {
        isNeedListener = false;
        dismissProgress();
    }
}
