/*
 * Copyright © 2021 RongCloud. All rights reserved.
 */

package com.rong.io.live.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.cooleshow.base.utils.UiUtils;

import androidx.annotation.NonNull;
import cn.rongcloud.rtc.api.stream.RCRTCVideoView;
import cn.rongcloud.rtc.core.RendererCommon;
import cn.rongcloud.rtc.utils.FinLog;

/**
 * Created by Administrator on 2017/3/30.
 */
public class VideoContainerLayout extends RelativeLayout {
    private final String TAG = VideoContainerLayout.class.getSimpleName();
    private Context context;
    private static boolean isNeedFillScrren = false;
    SurfaceView currentView;
    private final int mMaxHeightAtRatio16_9;

    public VideoContainerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        mMaxHeightAtRatio16_9 = UiUtils.getMaxHeightAtRatio16_9(getContext());
    }

    public void addView(final SurfaceView videoView) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        this.screenWidth = wm.getDefaultDisplay().getWidth();
        this.screenHeight = wm.getDefaultDisplay().getHeight();
        Log.i(TAG,
                "---xx-- add view "
                        + videoView.toString()
                        + " Height: "
                        + ((RCRTCVideoView) videoView).rotatedFrameHeight
                        + " Width: "
                        + ((RCRTCVideoView) videoView).rotatedFrameWidth);
        super.addView(videoView, getBigContainerParams((RCRTCVideoView) videoView));
        currentView = videoView;
        ((RCRTCVideoView) videoView)
                .setOnSizeChangedListener(
                        new RCRTCVideoView.OnSizeChangedListener() {
                            @Override
                            public void onChanged(RCRTCVideoView.Size size) {
                                try {
                                    VideoContainerLayout.this.removeAllViews();
                                    Log.i(TAG,
                                            "---xx-- change view "
                                                    + videoView.toString()
                                                    + " Height: "
                                                    + ((RCRTCVideoView) videoView)
                                                    .rotatedFrameHeight
                                                    + " Width: "
                                                    + ((RCRTCVideoView) videoView)
                                                    .rotatedFrameWidth);
                                    VideoContainerLayout.this.addView(
                                            videoView,
                                            getBigContainerParams((RCRTCVideoView) videoView));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
    }

    @NonNull
    private LayoutParams getBigContainerParams(RCRTCVideoView videoView) {
        LayoutParams layoutParams = null;
        if (!isNeedFillScrren) {
            if (screenHeight > screenWidth) { // V
//                int layoutParamsHeight =
//                        (videoView.rotatedFrameHeight == 0 || videoView.rotatedFrameWidth == 0)
//                                ? ViewGroup.LayoutParams.MATCH_PARENT
//                                : screenWidth
//                                * videoView.rotatedFrameHeight
//                                / videoView.rotatedFrameWidth;

                int layoutParamsWidth =
                        (videoView.rotatedFrameHeight == 0 || videoView.rotatedFrameWidth == 0)
                                ? ViewGroup.LayoutParams.MATCH_PARENT
                                : mMaxHeightAtRatio16_9
                                * videoView.rotatedFrameWidth
                                / videoView.rotatedFrameHeight;
                layoutParams = new LayoutParams(layoutParamsWidth, ViewGroup.LayoutParams.MATCH_PARENT);
            } else {
                int layoutParamsWidth =
                        (videoView.rotatedFrameWidth == 0 || videoView.rotatedFrameHeight == 0)
                                ? ViewGroup.LayoutParams.WRAP_CONTENT
                                : (screenWidth
                                * videoView.rotatedFrameWidth
                                / videoView.rotatedFrameHeight
                                > screenWidth
                                ? screenWidth
                                : screenHeight
                                * videoView.rotatedFrameWidth
                                / videoView.rotatedFrameHeight);
                layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            }
        } else {
            int layoutParamsWidth =
                    (videoView.rotatedFrameHeight == 0 || videoView.rotatedFrameWidth == 0)
                            ? ViewGroup.LayoutParams.MATCH_PARENT
                            : screenHeight
                            * videoView.rotatedFrameWidth
                            / videoView.rotatedFrameHeight;
            layoutParams = new LayoutParams(layoutParamsWidth, LayoutParams.MATCH_PARENT);
        }
        videoView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        Log.i("pq", "width:" + layoutParams.width);
        Log.i("pq", "height:" + layoutParams.height);
        return layoutParams;
    }

    public void setIsNeedFillScrren(boolean isNeed) {
        isNeedFillScrren = isNeed;
    }

    @Override
    public void removeAllViews() {
        if (currentView != null) ((RCRTCVideoView) currentView).setOnSizeChangedListener(null);
        super.removeAllViews();
    }

    private int screenWidth;
    private int screenHeight;
}
