package com.mogo.cloud;

import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

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;
import com.mogo.cloud.wifi.WifiStateManager;


/**
 * @author mogoauto
 */
public class LivePlayOneActivity extends AppCompatActivity implements ITrafficCarLiveCallBack, IWifiStateListener {
    private String TAG = "LivePlayOneActivity";
    private TextView tvWifiState;

    private SurfaceView surfaceView;
    private ToggleButton liveToggleBtn;
    private EditText etLookRoomId;
    private String liveSn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_play_on);

        LiveStreamManagerImpl.getInstance(this.getApplication(),
                MoGoAiCloudClientConfig.getInstance().getSn(), false);
        tvWifiState = findViewById(R.id.tvWifiState);

        surfaceView = findViewById(R.id.surfaceView);
        etLookRoomId = findViewById(R.id.etLookRoomId);
        liveToggleBtn = findViewById(R.id.liveToggleBtn);
        liveToggleBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn = etLookRoomId.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn, surfaceView, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn);
            }
        });

        surfaceView.setOnClickListener(v -> {
            if (liveToggleBtn.getVisibility() == View.VISIBLE) {
                liveToggleBtn.setVisibility(View.GONE);
                etLookRoomId.setVisibility(View.GONE);
            } else {
                liveToggleBtn.setVisibility(View.VISIBLE);
                etLookRoomId.setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MoGoAiCloudTrafficLive.stopCarLive(liveSn);
        WifiStateManager.getInstance().unRegisterWifiStateListener(this);
    }

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

    @Override
    public void onDisConnect() {
        Log.w(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()));
    }
}