package com.tencent.qcloud.tuicore.calling.videocall;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.liteav.TXLiteAVCode;
import com.tencent.liteav.device.TXDeviceManager;
import com.tencent.qcloud.tuicore.R;
import com.tencent.qcloud.tuicore.calling.basic.UserModel;
import com.tencent.qcloud.tuicore.calling.basic.UserModelManager;
import com.tencent.rtmp.ui.TXCloudVideoView;
import com.tencent.trtc.TRTCCloud;
import com.tencent.trtc.TRTCCloudDef;
import com.tencent.trtc.TRTCCloudListener;
import com.yidianling.common.tools.LogUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * TRTC视频通话的主页面
 *
 * 包含如下简单功能：
 * - 进入视频通话房间{@link VideoCallingActivity#enterRoom()}
 * - 退出视频通话房间{@link VideoCallingActivity#exitRoom()}
 * - 切换前置/后置摄像头{@link VideoCallingActivity#switchCamera()}
 * - 打开/关闭摄像头{@link VideoCallingActivity#muteVideo()}
 * - 打开/关闭麦克风{@link VideoCallingActivity#muteAudio()}
 * - 显示房间内其他用户的视频画面（当前示例最多可显示6个其他用户的视频画面）{@link TRTCCloudImplListener#refreshRemoteVideoViews()}
 *
 * - 详见接入文档{https://cloud.tencent.com/document/product/647/42045}
 */
public class VideoCallingActivity extends TRTCBaseActivity implements View.OnClickListener {

    private static final String TAG = "VideoCallingActivity";
    private static final int OVERLAY_PERMISSION_REQ_CODE = 1234;

    private TextView mTextTitle;
    private TXCloudVideoView mTXCVVLocalPreviewView;
    private ImageView mImageBack;
    private TextView tvLeave;
    private LinearLayout btnVideo;
    private LinearLayout btnAudio;
    private ImageView ivVideo;
    private ImageView ivAudio;

    private TRTCCloud mTRTCCloud;
    private TXDeviceManager mTXDeviceManager;
    private boolean mIsFrontCamera = true;
    private List<String> mRemoteUidList;
    private List<TXCloudVideoView> mRemoteViewList;
    private List<ImageView> mIvNetWorkViewList;
    private List<ImageView> mIvAudioViewList;
    private List<LinearLayout> mLlUserViewList;
    private int mUserCount = 0;
    private String mRoomId;
    private String mUserId;
    private boolean mAudioRouteFlag = true;



    @Override
    protected void initDataAndEvent() {
        handleIntent();
    }

    @Override
    protected int layoutResId() {
        return R.layout.videocall_activity_calling;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (checkPermission()) {
            initView();
            enterRoom();
        }
    }

    private void handleIntent() {
        Intent intent = getIntent();
        if (null != intent) {
            if (intent.getStringExtra(Constant.USER_ID) != null) {
                mUserId = intent.getStringExtra(Constant.USER_ID);
            }
            if (intent.getStringExtra(Constant.ROOM_ID) != null) {
                mRoomId = intent.getStringExtra(Constant.ROOM_ID);
            }

        }
    }

    private void initView() {
        mTextTitle = findViewById(R.id.tv_room_number);
        mImageBack = findViewById(R.id.iv_back);
        mTXCVVLocalPreviewView = findViewById(R.id.trtc_view_main);
        btnVideo = findViewById(R.id.btn_video);
        btnAudio = findViewById(R.id.btn_audio);
        btnVideo.setSelected(true);
        btnAudio.setSelected(true);
        ivVideo = findViewById(R.id.iv_video);
        ivAudio = findViewById(R.id.iv_audio);
        tvLeave = findViewById(R.id.tv_leave);


        if (!TextUtils.isEmpty(mRoomId)) {
            mTextTitle.setText("房间号:" + mRoomId);
        }
        mImageBack.setOnClickListener(this);
        btnVideo.setOnClickListener(this);
        btnAudio.setOnClickListener(this);
        tvLeave.setOnClickListener(this);

        mRemoteUidList = new ArrayList<>();
        mRemoteViewList = new ArrayList<>();
        mIvNetWorkViewList = new ArrayList<>();
        mIvAudioViewList = new ArrayList<>();
        mLlUserViewList = new ArrayList<>();
        mRemoteViewList.add(findViewById(R.id.trtc_view_1));
        mRemoteViewList.add(findViewById(R.id.trtc_view_2));
        mRemoteViewList.add(findViewById(R.id.trtc_view_3));
        mRemoteViewList.add(findViewById(R.id.trtc_view_4));
        mRemoteViewList.add(findViewById(R.id.trtc_view_5));
        mRemoteViewList.add(findViewById(R.id.trtc_view_6));
        mIvNetWorkViewList.add(findViewById(R.id.iv_network_1));
        mIvNetWorkViewList.add(findViewById(R.id.iv_network_2));
        mIvNetWorkViewList.add(findViewById(R.id.iv_network_3));
        mIvNetWorkViewList.add(findViewById(R.id.iv_network_4));
        mIvNetWorkViewList.add(findViewById(R.id.iv_network_5));
        mIvNetWorkViewList.add(findViewById(R.id.iv_network_6));
        mIvAudioViewList.add(findViewById(R.id.iv_other_audio_1));
        mIvAudioViewList.add(findViewById(R.id.iv_other_audio_2));
        mIvAudioViewList.add(findViewById(R.id.iv_other_audio_3));
        mIvAudioViewList.add(findViewById(R.id.iv_other_audio_4));
        mIvAudioViewList.add(findViewById(R.id.iv_other_audio_5));
        mIvAudioViewList.add(findViewById(R.id.iv_other_audio_6));
        mLlUserViewList.add(findViewById(R.id.ll_user_1));
        mLlUserViewList.add(findViewById(R.id.ll_user_2));
        mLlUserViewList.add(findViewById(R.id.ll_user_3));
        mLlUserViewList.add(findViewById(R.id.ll_user_4));
        mLlUserViewList.add(findViewById(R.id.ll_user_5));
        mLlUserViewList.add(findViewById(R.id.ll_user_6));
    }

    private void enterRoom() {
        mTRTCCloud = TRTCCloud.sharedInstance(getApplicationContext());
        mTRTCCloud.setListener(new TRTCCloudImplListener(VideoCallingActivity.this));
        mTXDeviceManager = mTRTCCloud.getDeviceManager();
        UserModel userModel = UserModelManager.getInstance().getUserModel();
        TRTCCloudDef.TRTCParams trtcParams = new TRTCCloudDef.TRTCParams();
        trtcParams.sdkAppId = userModel.appId;
        trtcParams.userId = mUserId;
        trtcParams.roomId = Integer.parseInt(mRoomId);
        LogUtil.d("userSig",userModel.userSig);
        trtcParams.userSig = userModel.userSig;

        mTRTCCloud.startLocalPreview(mIsFrontCamera, mTXCVVLocalPreviewView);
        mTRTCCloud.startLocalAudio(TRTCCloudDef.TRTC_AUDIO_QUALITY_SPEECH);
        mTRTCCloud.enterRoom(trtcParams, TRTCCloudDef.TRTC_APP_SCENE_VIDEOCALL);

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        exitRoom();
    }

    private void exitRoom() {
        if (mTRTCCloud != null) {
            mTRTCCloud.stopLocalAudio();
            mTRTCCloud.stopLocalPreview();
            mTRTCCloud.exitRoom();
            mTRTCCloud.setListener(null);
        }
        mTRTCCloud = null;
        TRTCCloud.destroySharedInstance();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPermissionGranted() {
        initView();
        enterRoom();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back|| id == R.id.tv_leave) {
            exitRoom();
            finish();
        } else if (id == R.id.btn_audio){
            muteAudio();
        } else if (id == R.id.btn_video){
            muteVideo();
        }
    }


    /**
     * 操作摄像头打开或者关闭
     */
    private void muteVideo() {
        boolean isSelected = btnVideo.isSelected();
        if (isSelected) {
            mTRTCCloud.stopLocalPreview();
            ivVideo.setImageResource(R.drawable.trtccalling_ic_me_video_close);
        } else {
            mTRTCCloud.startLocalPreview(mIsFrontCamera, mTXCVVLocalPreviewView);
            ivVideo.setImageResource(R.drawable.trtccalling_ic_me_video_open);
        }
        btnVideo.setSelected(!isSelected);
    }

    /**
     * 操作麦克风开启或者关闭
     */
    private void muteAudio() {
        boolean isSelected = btnAudio.isSelected();
        if (!isSelected) {
            mTRTCCloud.muteLocalAudio(true);
            ivAudio.setImageResource(R.drawable.trtccalling_ic_me_audio_open);
        } else {
            mTRTCCloud.muteLocalAudio(false);
            ivAudio.setImageResource(R.drawable.trtccalling_ic_me_audio_close);
        }
        btnAudio.setSelected(!isSelected);
    }

    /**
     * 转换摄像头
     */
    private void switchCamera() {
        mIsFrontCamera = !mIsFrontCamera;
        mTXDeviceManager.switchCamera(mIsFrontCamera);
        if(mIsFrontCamera){
//            mButtonSwitchCamera.setText( "使用后置摄像头");
        }else{
//            mButtonSwitchCamera.setText("使用前置摄像头");
        }
    }

    /**
     * 听筒和扬声器切换
     */
    private void audioRoute() {
        if(mAudioRouteFlag){
            mAudioRouteFlag = false;
            mTXDeviceManager.setAudioRoute(TXDeviceManager.TXAudioRoute.TXAudioRouteEarpiece);
//            mButtonAudioRoute.setText("使用扬声器");
        }else{
            mAudioRouteFlag = true;
            mTXDeviceManager.setAudioRoute(TXDeviceManager.TXAudioRoute.TXAudioRouteSpeakerphone);
//            mButtonAudioRoute.setText("使用听筒");
        }
    }

    @Override
    public void onBackPressed() {

    }

    private class TRTCCloudImplListener extends TRTCCloudListener {

        private WeakReference<VideoCallingActivity> mContext;

        public TRTCCloudImplListener(VideoCallingActivity activity) {
            super();
            mContext = new WeakReference<>(activity);
        }

        /**
         * 离开房间
         * */
        @Override
        public void onExitRoom(int reason) {
            super.onExitRoom(reason);
            finish();
        }

        /**
         * 网络质量的实时统计回调
         * @param localQuality
         * @param remoteQuality
         */
        @Override
        public void onNetworkQuality(TRTCCloudDef.TRTCQuality localQuality, ArrayList<TRTCCloudDef.TRTCQuality> remoteQuality) {
            Log.e(TAG,"网络质量的实时统计回调 userId:"+localQuality.userId+"uid:"+localQuality.quality+remoteQuality.get(0).userId+"quality:"+localQuality.quality+remoteQuality.get(0).quality);
//            if (remoteQuality.size()>0){
//                refreshNetWorkStatus(remoteQuality);
//            }
        }

        /**
         * 音频麦克风开启或关闭回调
         * @param userId
         * @param available
         */
        @Override
        public void onUserAudioAvailable(String userId, boolean available) {
            Log.e(TAG, "onUserAudioAvailable userId " + userId + ", mUserCount " + mUserCount + ",available " + available);
            if (!TextUtils.isEmpty(userId)){
                refreshAudioStatus(userId, available);
            }
        }

        /**
         * 视频开启或关闭回调
         * @param userId
         * @param available
         */
        @Override
        public void onUserVideoAvailable(String userId, boolean available) {
            Log.e(TAG, "onUserVideoAvailable userId " + userId + ", mUserCount " + mUserCount + ",available " + available);
            int index = mRemoteUidList.indexOf(userId);
            if (available) {
                if (index != -1) {
                    return;
                }
                mRemoteUidList.add(userId);
            } else {
                if (index == -1) {
                    return;
                }
                mTRTCCloud.stopRemoteView(userId);
                mRemoteUidList.remove(index);
            }
            refreshRemoteVideoViews();

        }

        private void refreshRemoteVideoViews() {
            for (int i = 0; i < mRemoteViewList.size(); i++) {
                if (i < mRemoteUidList.size()) {
                    String remoteUid = mRemoteUidList.get(i);
                    mRemoteViewList.get(i).setVisibility(View.VISIBLE);
                    mTRTCCloud.startRemoteView(remoteUid, TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_SMALL,mRemoteViewList.get(i));
                } else {
                    mRemoteViewList.get(i).setVisibility(View.GONE);
                }
            }
        }

        private void  refreshAudioStatus(String userId,boolean available){
            if (mRemoteUidList.size()>0){
                for (int i = 0; i < mIvAudioViewList.size(); i++) {
                    if (userId.equals(mRemoteUidList.get(i))){
                        if (available){
                            mIvAudioViewList.get(i).setImageResource(R.drawable.trtccalling_ic_other_audio_open);
                        }else {
                            mIvAudioViewList.get(i).setImageResource(R.drawable.trtccalling_ic_other_audio_close);
                        }
                        return;
                    }
                }
            }
        }

        private void refreshNetWorkStatus(ArrayList<TRTCCloudDef.TRTCQuality> remoteQuality){
            for (int i = 0; i < mIvNetWorkViewList.size(); i++) {
                int quality = remoteQuality.get(i).quality;
                if (quality==TRTCCloudDef.TRTC_QUALITY_Excellent){
                    mIvNetWorkViewList.get(i).setImageResource(R.drawable.trtccalling_ic_network_excellent);
                }else if (quality==TRTCCloudDef.TRTC_QUALITY_Good){
                    mIvNetWorkViewList.get(i).setImageResource(R.drawable.trtccalling_ic_network_good);
                }else if (quality==TRTCCloudDef.TRTC_QUALITY_Poor){
                    mIvNetWorkViewList.get(i).setImageResource(R.drawable.trtccalling_ic_network_poor);
                }else if (quality==TRTCCloudDef.TRTC_QUALITY_Bad){
                    mIvNetWorkViewList.get(i).setImageResource(R.drawable.trtccalling_ic_network_bad);
                }
            }
        }

        @Override
        public void onError(int errCode, String errMsg, Bundle extraInfo) {
            Log.d(TAG, "sdk callback onError");
            VideoCallingActivity activity = mContext.get();
            if (activity != null) {
                Toast.makeText(activity, "onError: " + errMsg + "[" + errCode+ "]" , Toast.LENGTH_SHORT).show();
                if (errCode == TXLiteAVCode.ERR_ROOM_ENTER_FAIL) {
                    activity.exitRoom();
                }
            }
        }
    }


}
