package com.yunbao.one.views;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.tencent.rtmp.ITXLivePlayListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXLivePlayConfig;
import com.tencent.rtmp.TXLivePlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;
import com.yunbao.common.CommonAppConfig;
import com.yunbao.common.utils.L;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.common.utils.WordUtil;
import com.yunbao.one.R;

/**
 * 0/10.
 * 直播间播放器  腾讯播放器
 */

// +———————————————————————————————————
// | Created by Yunbao
// +———————————————————————————————————
// | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
// +———————————————————————————————————
// | Author: https://gitee.com/yunbaokeji
// +———————————————————————————————————
// | Date: 2022-03-22
// +———————————————————————————————————
public class ChatLivePlayTxViewHolder extends AbsChatLivePlayViewHolder implements ITXLivePlayListener {

    private static final String TAG = "ChatLivePlayTxViewHolder";
    private TXCloudVideoView mVideoView;
    private View mLoading;
    private TXLivePlayer mPlayer;
    private boolean mPaused;//是否切后台了
    private boolean mStarted;//是否开始了播放
    private boolean mEnd;//是否结束了播放
    private boolean mPausedPlay;//是否被动暂停了播放


    public ChatLivePlayTxViewHolder(Context context, ViewGroup parentView) {
        super(context, parentView);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_live_play_tx;
    }

    @Override
    public void init() {
        super.init();
        mVideoView = (TXCloudVideoView) findViewById(R.id.video_view);
        mLoading = findViewById(R.id.loading);
        mPlayer = new TXLivePlayer(mContext);
        mPlayer.setPlayListener(this);
        mPlayer.setPlayerView(mVideoView);
        mPlayer.enableHardwareDecode(false);
        mPlayer.setRenderRotation(TXLiveConstants.RENDER_ROTATION_PORTRAIT);
        mPlayer.setRenderMode(TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN);
        TXLivePlayConfig playConfig = new TXLivePlayConfig();
        playConfig.setAutoAdjustCacheTime(true);
        playConfig.setMaxAutoAdjustCacheTime(5.0f);
        playConfig.setMinAutoAdjustCacheTime(1.0f);
        mPlayer.setConfig(playConfig);

    }


    @Override
    public void onPlayEvent(int e, Bundle bundle) {
        if (mEnd) {
            return;
        }
        switch (e) {
            case TXLiveConstants.PLAY_EVT_PLAY_BEGIN://播放开始
                if (mLoading != null && mLoading.getVisibility() == View.VISIBLE) {
                    mLoading.setVisibility(View.INVISIBLE);
                }
                break;
            case TXLiveConstants.PLAY_EVT_PLAY_LOADING:
                if (mLoading != null && mLoading.getVisibility() != View.VISIBLE) {
                    mLoading.setVisibility(View.VISIBLE);
                }
                break;
            case TXLiveConstants.PLAY_EVT_RCV_FIRST_I_FRAME://第一帧
                break;
            case TXLiveConstants.PLAY_EVT_PLAY_END://播放结束
                replay();
                break;
//            case TXLiveConstants.PLAY_EVT_CHANGE_RESOLUTION://获取视频宽高
//                float width = bundle.getInt("EVT_PARAM1", 0);
//                float height = bundle.getInt("EVT_PARAM2", 0);
//                L.e(TAG, "流---width----->" + width);
//                L.e(TAG, "流---height----->" + height);
//                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mVideoView.getLayoutParams();
//                int targetH = 0;
//                if (width / height > 0.5625f) {//横屏 9:16=0.5625
//                    targetH = (int) (mVideoView.getWidth() / width * height);
//                } else {
//                    targetH = ViewGroup.LayoutParams.MATCH_PARENT;
//                }
//                if (targetH != params.height) {
//                    params.height = targetH;
//                    params.gravity = Gravity.CENTER;
//                    mVideoView.requestLayout();
//                }
//                break;
            case TXLiveConstants.PLAY_ERR_NET_DISCONNECT://播放失败
            case TXLiveConstants.PLAY_ERR_FILE_NOT_FOUND:
                ToastUtil.show(WordUtil.getString(R.string.live_play_error));
                break;
        }
    }

    @Override
    public void onNetStatus(Bundle bundle) {

    }


    /**
     * 循环播放
     */
    private void replay() {
        if (mStarted && mPlayer != null) {
            mPlayer.seek(0);
            mPlayer.resume();
        }
    }


    /**
     * 暂停播放
     */
    @Override
    public void pausePlay() {
        if (!mPausedPlay) {
            mPausedPlay = true;
            if (!mPaused) {
                if (mPlayer != null) {
                    mPlayer.pause();
                }
            }
        }
    }

    /**
     * 暂停播放后恢复
     */
    @Override
    public void resumePlay() {
        if (mPausedPlay) {
            mPausedPlay = false;
            if (!mPaused) {
                if (mPlayer != null) {
                    mPlayer.resume();
                }
            }
        }
    }

    /**
     * 开始播放
     *
     * @param url 流地址
     */
    @Override
    public void startPlay(String url) {
        //L.e("推拉流问题-- "+ CommonAppConfig.getInstance().getUid()+" --> 开始播放对方的流---> "+url);
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (mPlayer != null) {
            int result = mPlayer.startPlay(url, TXLivePlayer.PLAY_TYPE_LIVE_RTMP_ACC);
            if (result == 0) {
                mStarted = true;
            }
        }
//        L.e(TAG, "play----url--->" + url);
    }

    @Override
    public void stopPlay() {
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.stopPlay(true);
        }
        L.e(TAG, "stopPlay------->");
    }


    @Override
    public void release() {
        super.release();
        stopPlay();
        if (mPlayer != null) {
            mPlayer.setPlayListener(null);
        }
        mEnd = true;
        mPlayer = null;
    }


//    @Override
//    public void onResume() {
//        if (!mPausedPlay && mPaused && mPlayer != null) {
//            mPlayer.resume();
//        }
//        mPaused = false;
//    }
//
//    @Override
//    public void onPause() {
//        if (!mPausedPlay && mPlayer != null) {
//            mPlayer.pause();
//        }
//        mPaused = true;
//    }

}
