package com.cooleshow.student.widgets.helper;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.student.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.rongcloud.rtc.api.stream.RCRTCVideoView;
import cn.rongcloud.rtc.api.stream.view.RCRTCRendererEventsListener;
import cn.rongcloud.rtc.core.RendererCommon;

import static cn.rongcloud.rtc.core.RendererCommon.ScalingType.SCALE_ASPECT_FILL;

public class VideoViewManager {

    private static final String TAG = "VideoViewManager";
    private static final int MAX_WAIT_RENDER_TIME = 8000;
    ArrayList<RCRTCVideoView> arrayListVideoView;
    int mWidth, mHeight;
    private RelativeLayout flSurfaceContainer;
    private Map<String, RCRTCVideoView> linkedHashMap = new LinkedHashMap<>();
    private boolean isFullScreen = false;
    private int mainScreenPosition = 0;
    private int otherVideoViewMaxWidth = 0;
    private int otherVideoViewMaxHeight = 0;
    private View mStatusView;
    private ProgressBar mPbLoading;
    private TextView mTvLoading;
    private TextView mTvLiveStatusTipText;
    private TextView mTvRetry;

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            showRetryStatus();
        }
    };

    public VideoViewManager(RelativeLayout surfaceContainer, int width, int height) {
        flSurfaceContainer = surfaceContainer;
        mWidth = width;
        mHeight = height;
        otherVideoViewMaxWidth = SizeUtils.dp2px(100);
        otherVideoViewMaxHeight = SizeUtils.dp2px(57);
        initStatusView();
    }

    private void initStatusView() {
        mStatusView = LayoutInflater.from(flSurfaceContainer.getContext()).inflate(R.layout.view_live_video_status_layout, flSurfaceContainer, false);
        mPbLoading = mStatusView.findViewById(R.id.pb_loading);
        mTvLoading = mStatusView.findViewById(R.id.tv_loading);
        mTvLiveStatusTipText = mStatusView.findViewById(R.id.tv_live_status_tip_text);
        mTvRetry = mStatusView.findViewById(R.id.tv_retry);
    }

    /**
     * 2 列多行显示 videoview
     *
     * @param list
     */
    public void update(ArrayList<RCRTCVideoView> list, boolean isPc, boolean isShowLoading) {
        Log.i("pq", "update" + list.size());
        Log.i("pq", "mainScreenPosition" + mainScreenPosition);
        arrayListVideoView = list;
        this.mainScreenPosition = mainScreenPosition;
        int row = 0;
        int column = 0;

//        column = list.size() > 1 ? 2 : 1;
//        row = (list.size() + 1) / 2;
        row = list.size();

        flSurfaceContainer.removeAllViews();


        int width = mWidth;
        int height = mHeight;
        int index = 0;
        for (int i = 0; i < row; i++) {
            RelativeLayout.LayoutParams layoutParams;
//            if (i == mainScreenPosition) {
//            } else {
//                layoutParams = new RelativeLayout.LayoutParams(otherVideoViewMaxWidth, otherVideoViewMaxHeight);
//                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//            }

            layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
//                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            int marginLeft = j * (width / column);
//            int marginRight = mWidth - (marginLeft + width / column);
//            int marginTop = i * (height / row);
//            int marginBottom = mHeight - (marginTop + height / row);
//            layoutParams.setMargins(marginLeft, marginTop, marginRight, marginBottom);
            if (index >= arrayListVideoView.size()) {
                break;
            }
            arrayListVideoView.get(i).setRendererEventsListener(new RCRTCRendererEventsListener() {
                @Override
                public void onFirstFrame() {
                    Log.i("pq", "视频第一帧渲染");
                    hideStatusView();

                }
            });
            arrayListVideoView.get(i).setScalingType(isPc ? RendererCommon.ScalingType.SCALE_ASPECT_FIT : SCALE_ASPECT_FILL);
            flSurfaceContainer.addView(arrayListVideoView.get(i), layoutParams);
            index++;
        }
        if (isShowLoading) {
            showLoadStatus();
        }
    }

    public void setRetryClickListener(View.OnClickListener retryClickListener) {
        if (mTvRetry != null) {
            mTvRetry.setOnClickListener(retryClickListener);
        }
    }

    private void hideStatusView() {
        if (mStatusView == null) {
            return;
        }
        mStatusView.removeCallbacks(mRunnable);
        mStatusView.post(new Runnable() {
            @Override
            public void run() {
                if (mStatusView != null) {
                    mStatusView.setVisibility(View.GONE);
                }
            }
        });
    }

    private void showLoadStatus() {
        if (mStatusView == null) {
            return;
        }
        flSurfaceContainer.addView(mStatusView);
        mStatusView.setVisibility(View.VISIBLE);
        mPbLoading.setVisibility(View.VISIBLE);
        mTvLoading.setVisibility(View.VISIBLE);
        mTvLiveStatusTipText.setVisibility(View.GONE);
        mTvRetry.setVisibility(View.GONE);
        mStatusView.postDelayed(mRunnable, MAX_WAIT_RENDER_TIME);
    }

    private void showRetryStatus() {
        if (mStatusView == null) {
            return;
        }
        mStatusView.setVisibility(View.VISIBLE);
        mPbLoading.setVisibility(View.GONE);
        mTvLoading.setVisibility(View.GONE);
        mTvLiveStatusTipText.setVisibility(View.VISIBLE);
        mTvRetry.setVisibility(View.VISIBLE);
    }

    public void release() {
        if (null != arrayListVideoView && 0 != arrayListVideoView.size()) {
            for (RCRTCVideoView v : arrayListVideoView) {
                v.release();
            }
        }
    }

    public ArrayList<RCRTCVideoView> getAllVideoViews() {
        return arrayListVideoView;
    }

    public void setIsNeedFillScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
    }

    public void onRelease() {
        if (mStatusView != null && mRunnable != null) {
            mStatusView.removeCallbacks(mRunnable);
        }
    }
}
