package com.mogo.cloud;

import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.mogo.cloud.live.listener.ILivePushStatusListener;
import com.mogo.cloud.live.manager.LiveStreamManagerImpl;
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
import com.mogo.cloud.trafficlive.api.ITrafficCarLiveCallBack;
import com.mogo.cloud.trafficlive.api.MoGoAiCloudTrafficLive;
import com.mogo.cloud.wifi.IWifiStateListener;


/**
 * 推流和拉流页面
 */
public class LivePlayAndPushActivity extends BaseLiveActivity
            implements ITrafficCarLiveCallBack, IWifiStateListener {
    public static final String TAG = "LivePlayAndPushActivity";

    private String liveSn;

    LiveStreamManagerImpl liveStreamManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        WifiStateManager.getInstance().registerWifiStateListener(this);

        // 初始化直播流管理
        liveStreamManager = LiveStreamManagerImpl.getInstance(this.getApplication(),
                MoGoAiCloudClientConfig.getInstance().getSn(),true);

        // 设置状态回调
        liveStreamManager.setLivePushStatusChangeCallback(new ILivePushStatusListener() {
            @Override
            public void onChange(int status) {
                tvLiveStatus.post(new Runnable() {
                    @Override
                    public void run() {
                        if (status == 0) {
                            tvLiveStatus.setTextColor(getResources().getColor(R.color.colorStartLive));
                            tvLiveStatus.setText("正在直播中");
                        } else if (status == 1) {
                            tvLiveStatus.setTextColor(getResources().getColor(R.color.colorStopLive));
                            tvLiveStatus.setText("直播已停止");
                        } else if (status == 2) {
                            tvLiveStatus.setTextColor(getResources().getColor(R.color.colorForcedStopLive));
                            tvLiveStatus.setText("直播强制结束");
                        }
                    }
                });
            }
        });
    }

    @Override
    public void onVideoFrame(byte[] bytes, int bytesLength) {
        //Log.i(TAG, "onVideoFrame byte length: " + bytesLength);
        if (liveStreamManager != null) {
            // 将摄像头采集的YUV数据推送到ZEGO
            liveStreamManager.notifyYUVData(bytes, 1280, 720, 3);
        }
    }

    @Override
    public void toggleLive(boolean isLive) {
        if (isLive) {
            liveStreamManager.startLiveStream();
        } else {
            liveStreamManager.stopLiveStream();
        }
    }

    @Override
    public void toggleCameraState(boolean isLive) {
        Log.i(TAG, "toggleCameraState isLive: " + isLive);
        if (isLive) {
            // 上报摄像头状态，1-可用，2-不可用
            liveStreamManager.uploadCamStatus(1, 1);
        } else {
            liveStreamManager.uploadCamStatus(2, 2);
        }
    }

    @Override
    public void togglePlay(boolean isPlay) {
        if (isPlay) {
            liveSn = etLookRoomId.getText().toString().trim();
            MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn, textureView, this);
        } else {
            MoGoAiCloudTrafficLive.stopCarLive(liveSn);
        }
    }

    @Override
    public void toggleLoginMultiRoom(boolean isLogin) {
        if (isLogin) {
            liveSn = etLookRoomId.getText().toString().trim();
            liveStreamManager.getMoGoLiveManager().loginMultiRoom(liveSn);
        } else {
            liveStreamManager.getMoGoLiveManager().logoutMultiRoom(liveSn);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (liveStreamManager != null) {
            // 停止
            liveStreamManager.stopLiveStream();
            // 释放资源
            liveStreamManager.release();
        }

        MoGoAiCloudTrafficLive.stopCarLive(liveSn);
//        WifiStateManager.getInstance().unRegisterWifiStateListener(this);
    }

    @Override
    public void onLive(String liveSn) {
        Log.d(TAG, "开始直播 onLive");
    }

    @Override
    public void onFirstFrame() {
        textureView.setVisibility(View.VISIBLE);
        Log.d(TAG, "onFirstFrame");
    }

    @Override
    public void onDisConnect() {
        Log.d(TAG, "失去连接 onDisConnect");
    }

    @Override
    public void onPlaying() {
        Log.i(TAG, "拉流状态：拉流成功，播放中 …………");
    }

    @Override
    public void onPlaRequesting() {
        Log.i(TAG, "拉流状态：拉流还没成功，努力加载中 …………");
    }

    @Override
    public void onError(String errorMsg) {
        Log.d(TAG, "拉流状态：发生错误  onError msg: " + errorMsg);
    }

    @Override
    public void onWifiState(WifiInfo wifiInfo) {
        tvWifiState.setText("wifiLevel: " + Math.abs(wifiInfo.getRssi()));
    }
}