package com.mogo.cloud;

import android.os.Bundle;
import android.util.Log;

import com.mogo.cloud.live.listener.ILivePushStatusListener;
import com.mogo.cloud.live.manager.ILiveStreamManager;
import com.mogo.cloud.live.manager.LiveStreamManagerImpl;
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;


/**
 * 推流页面
 */
public class LivePushActivity extends BaseLiveActivity {
    public static final String TAG = "PushActivity";

    ILiveStreamManager liveStreamManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化直播流管理
        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));
                        } else {
                            tvLiveStatus.setTextColor(getResources().getColor(R.color.colorStopLive));
                        }
                    }
                });
            }
        });
    }

    @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) {

    }

    @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) {

    }

    @Override
    public void toggleLoginMultiRoom(boolean isLogin) {

    }

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