package com.mogo.cloud;

import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
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;


public class LivePlayActivity extends AppCompatActivity implements ITrafficCarLiveCallBack, IWifiStateListener {
    private String TAG = "LivePlayActivity";
    private TextView tvWifiState;

    private SurfaceView surfaceView0;
    private ToggleButton liveToggleBtn0;
    private EditText etLookRoomId0;
    private String liveSn0;

    private SurfaceView surfaceView1;
    private ToggleButton liveToggleBtn1;
    private EditText etLookRoomId1;
    private String liveSn1;

    private SurfaceView surfaceView2;
    private ToggleButton liveToggleBtn2;
    private EditText etLookRoomId2;
    private String liveSn2;

    private SurfaceView surfaceView3;
    private ToggleButton liveToggleBtn3;
    private EditText etLookRoomId3;
    private String liveSn3;

    private SurfaceView surfaceView4;
    private ToggleButton liveToggleBtn4;
    private EditText etLookRoomId4;
    private String liveSn4;

    private SurfaceView surfaceView5;
    private ToggleButton liveToggleBtn5;
    private EditText etLookRoomId5;
    private String liveSn5;

    private SurfaceView surfaceView6;
    private ToggleButton liveToggleBtn6;
    private EditText etLookRoomId6;
    private String liveSn6;

    private SurfaceView surfaceView7;
    private ToggleButton liveToggleBtn7;
    private EditText etLookRoomId7;
    private String liveSn7;


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

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

        surfaceView0 = findViewById(R.id.surfaceView0);
        etLookRoomId0 = findViewById(R.id.etLookRoomId0);
        liveToggleBtn0 = findViewById(R.id.liveToggleBtn0);
        liveToggleBtn0.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn0 = etLookRoomId0.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn0, surfaceView0, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn0);
            }
        });

        surfaceView1 = findViewById(R.id.surfaceView1);
        etLookRoomId1 = findViewById(R.id.etLookRoomId1);
        liveToggleBtn1 = findViewById(R.id.liveToggleBtn1);
        liveToggleBtn1.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn1 = etLookRoomId1.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn1, surfaceView1, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn1);
            }
        });


        surfaceView2 = findViewById(R.id.surfaceView2);
        etLookRoomId2 = findViewById(R.id.etLookRoomId2);
        liveToggleBtn2 = findViewById(R.id.liveToggleBtn2);
        liveToggleBtn2.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn2 = etLookRoomId2.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn2, surfaceView2, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn2);
            }
        });


        surfaceView3 = findViewById(R.id.surfaceView3);
        etLookRoomId3 = findViewById(R.id.etLookRoomId3);
        liveToggleBtn3 = findViewById(R.id.liveToggleBtn3);
        liveToggleBtn3.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn3 = etLookRoomId3.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn3, surfaceView3, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn3);
            }
        });

        surfaceView4 = findViewById(R.id.surfaceView4);
        etLookRoomId4 = findViewById(R.id.etLookRoomId4);
        liveToggleBtn4 = findViewById(R.id.liveToggleBtn4);
        liveToggleBtn4.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn4 = etLookRoomId4.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn4, surfaceView4, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn4);
            }
        });

        surfaceView5 = findViewById(R.id.surfaceView5);
        etLookRoomId5 = findViewById(R.id.etLookRoomId5);
        liveToggleBtn5 = findViewById(R.id.liveToggleBtn5);
        liveToggleBtn5.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn5 = etLookRoomId5.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn5, surfaceView5, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn5);
            }
        });

        surfaceView6 = findViewById(R.id.surfaceView6);
        etLookRoomId6 = findViewById(R.id.etLookRoomId6);
        liveToggleBtn6 = findViewById(R.id.liveToggleBtn6);
        liveToggleBtn6.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn6 = etLookRoomId6.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn6, surfaceView6, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn6);
            }
        });

        surfaceView7 = findViewById(R.id.surfaceView7);
        etLookRoomId7 = findViewById(R.id.etLookRoomId7);
        liveToggleBtn7 = findViewById(R.id.liveToggleBtn7);
        liveToggleBtn7.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
            if (isChecked) {
                liveSn7 = etLookRoomId7.getText().toString().trim();
                MoGoAiCloudTrafficLive.viewDesignativeVehicleLive(liveSn7, surfaceView7, this);
            } else {
                MoGoAiCloudTrafficLive.stopCarLive(liveSn7);
            }
        });

        WifiStateManager.getInstance().registerWifiStateListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MoGoAiCloudTrafficLive.stopCarLive(liveSn0);
        MoGoAiCloudTrafficLive.stopCarLive(liveSn1);
        MoGoAiCloudTrafficLive.stopCarLive(liveSn2);
        MoGoAiCloudTrafficLive.stopCarLive(liveSn3);
        MoGoAiCloudTrafficLive.stopCarLive(liveSn4);
        MoGoAiCloudTrafficLive.stopCarLive(liveSn5);
        MoGoAiCloudTrafficLive.stopCarLive(liveSn6);
        MoGoAiCloudTrafficLive.stopCarLive(liveSn7);
        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()));
    }
}