package com.mogo.cloud;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.mogo.cloud.passport.IMoGoTokenCallback;
import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
import com.mogo.cloud.trafficlive.api.ITrafficCarLiveCallBack;
import com.mogo.cloud.trafficlive.api.ITrafficIntersectionLiveCallBack;
import com.mogo.cloud.trafficlive.api.MoGoAiCloudTrafficLive;
import com.mogo.v2x.V2XManager;
import com.mogo.v2x.callback.IV2XCallback;
import com.mogo.v2x.config.V2XConfig;
import com.mogo.v2x.event.V2XEvent;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private Button btnJumpPassPort;
    private Button btnJumpConfigInfo;
    private Button btnJumpNetWorkPort;
    private Button btnJumpRealTime;
    private Button btnJumpLocation;
    private Button btnJumpRoadCondition;
    private Button btnJumpLivePlayAndPush;
    private Button btnJumpLivePush;
    private Button btnJumpLivePlay;
    private Button btnJumpLivePlayOne;
    private Button btnRequestXINGLive;
    private Button btnRequestCarLive;
    private Button btnV2XFunctionTest;


    private boolean v2xHasBeenInitialized = false;
    private Button btnNSDNetty;
    private SurfaceView surfacePreviewView;

    private TextView tvSn;
    private TextView tvToken;
    private TextView tvAppKey;
    private TextView tvDevicesId;

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

        tvAppKey = findViewById(R.id.tvAppKey);
        tvDevicesId = findViewById(R.id.tvDevicesId);
        tvSn = findViewById(R.id.tvSn);
        tvToken = findViewById(R.id.tvToken);

        btnJumpPassPort = findViewById(R.id.btnJumpPassPort);
        btnJumpPassPort.setOnClickListener(v -> {
            MoGoAiCloudClient.getInstance().refreshToken();
        });

        btnJumpRealTime = findViewById(R.id.btnJumpRealTime);
        btnJumpRealTime.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, RealTimeActivity.class);
            startActivity(intent);
        });

        btnJumpLocation = findViewById(R.id.btnJumpLocation);
        btnJumpLocation.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, LocationActivity.class);
            startActivity(intent);
        });

        btnJumpRoadCondition = findViewById(R.id.btnJumpRoadcondition);
        btnJumpRoadCondition.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, RoadConditionActivity.class);
            startActivity(intent);
        });

        btnJumpConfigInfo = findViewById(R.id.btnJumpConfigInfo);
        btnJumpConfigInfo.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, ConfigInfoActivity.class);
            startActivity(intent);
        });

        btnJumpLivePlayAndPush = findViewById(R.id.btnJumpLivePlayAndPush);
        btnJumpLivePlayAndPush.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, LivePlayAndPushActivity.class);
            startActivity(intent);
        });

        btnJumpLivePush = findViewById(R.id.btnJumpLivePush);
        btnJumpLivePush.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, LivePushActivity.class);
            startActivity(intent);
        });

        btnJumpLivePlay = findViewById(R.id.btnJumpLivePlay);
        btnJumpLivePlay.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, LivePlayActivity.class);
            startActivity(intent);
        });

        btnJumpLivePlayOne = findViewById(R.id.btnJumpLivePlayOne);
        btnJumpLivePlayOne.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, LivePlayOneActivity.class);
            startActivity(intent);
        });

        btnRequestXINGLive = findViewById(R.id.btnRequestXINGLive);
        btnRequestXINGLive.setOnClickListener(v -> {
            MoGoAiCloudTrafficLive.viewDesignativeIntersectionLive("556834859_trafficcamera_100514_89", new ITrafficIntersectionLiveCallBack() {
                @Override
                public void liveUrlResult(String liveUrl) {
                    Log.i(TAG, "liveUrl : " + liveUrl);
                }

                @Override
                public void onError(String errorMsg) {
                    Log.e(TAG, "errorMsg : " + errorMsg);
                }
            });
//            MoGoAiCloudTrafficLive.viewDesignativeIntersectionLive("POLE1617103029423", new ITrafficIntersectionLiveCallBack() {
//                @Override
//                public void liveUrlResult(String liveUrl) {
//                    Log.d(TAG, "liveUrl : " + liveUrl);
//                }
//
//                @Override
//                public void onError(String errorMsg) {
//                    Log.e(TAG, "errorMsg : " + errorMsg);
//                }
//            });
//            MoGoAiCloudTrafficLive.viewFrontIntersectionLive(40.11547, 116.22544, 60, new ITrafficIntersectionLiveCallBack() {
//                @Override
//                public void liveUrlResult(String liveUrl) {
//                    Log.d(TAG, "liveUrl : " + liveUrl);
//                }
//
//                @Override
//                public void onError(String errorMsg) {
//                    Log.e(TAG, "errorMsg : " + errorMsg);
//                }
//            });
        });

        btnRequestXINGLive = findViewById(R.id.btnRequestXINGLive);
        btnRequestXINGLive.setOnClickListener(v -> {
            MoGoAiCloudTrafficLive.viewDesignativeIntersectionLive("556834859_trafficcamera_100514_89", new ITrafficIntersectionLiveCallBack() {
                @Override
                public void liveUrlResult(String liveUrl) {
                    Log.i(TAG, "liveUrl : " + liveUrl);
                }

                @Override
                public void onError(String errorMsg) {
                    Log.e(TAG, "errorMsg : " + errorMsg);
                }
            });
        });

        btnRequestCarLive = findViewById(R.id.btnRequestCarLive);
        btnNSDNetty = findViewById(R.id.btnNSDNetty);
        surfacePreviewView = findViewById(R.id.surfacePreviewView);
        btnRequestCarLive.setOnClickListener(v -> {
            MoGoAiCloudTrafficLive.viewFrontVehicleLive(40.11547, 116.22544,
                    60, surfacePreviewView, new ITrafficCarLiveCallBack() {

                        @Override
                        public void onLiveConnecting() {

                        }

                        @Override
                        public void onLiveConnected() {

                        }

                        @Override
                        public void onLive(String liveSn) {
                            Log.e(TAG, "liveSn : " + liveSn);
                        }

                        @Override
                        public void onFirstFrame() {
                            Log.e(TAG, "onFirstFrame");
                        }

                        @Override
                        public void onDisConnect() {
                            Log.e(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);
                        }
                    });
        });
        btnNSDNetty.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, NSDNettyActivity.class);
            startActivity(intent);
        });

        //V2X功能测试
        btnV2XFunctionTest = findViewById(R.id.btnV2XFunctionTest);
        btnV2XFunctionTest.setOnClickListener(v -> {
            if (v2xHasBeenInitialized) {
                return;
            }
            v2xHasBeenInitialized = true;
            V2XConfig.Builder builder = new V2XConfig.Builder();
            builder.loggable(true)
                    .aiCloudConfig(MoGoAiCloudClientConfig.getInstance())
                    .context(v.getContext())
                    .distanceForTriggerRefresh(300)
                    .durationForTriggerRefresh(1, TimeUnit.MINUTES);
            V2XManager.INSTANCE.init(builder.build());
            V2XManager.INSTANCE.addCallback(new IV2XCallback() {
                @Override
                public void onAck(@NonNull V2XEvent event) {
                    if (event instanceof V2XEvent.ForwardsWarning) {
                        V2XEvent.ForwardsWarning warning = (V2XEvent.ForwardsWarning) event;
                        Log.d("V2XManager", "forward-warning<->onAck::" + warning.toString());
                    }
                    if (event instanceof V2XEvent.OptimalRoute) {
                        V2XEvent.OptimalRoute route = (V2XEvent.OptimalRoute) event;
                        Log.d("V2XManager", "route<->onAck::" + route.toString());
                    }

                    if (event instanceof V2XEvent.Warning) {
                        V2XEvent.Warning warning = (V2XEvent.Warning) event;
                        Log.d("V2XManager", "warning<->onAck::" + warning.toString());
                    }

                    if (event instanceof V2XEvent.Road) {
                        V2XEvent.Road road = (V2XEvent.Road) event;
                        Log.d("V2XManager", "road<-->onAck::" + road.toString());
                    }

                    if (event instanceof V2XEvent.Marker) {
                        V2XEvent.Marker marker = (V2XEvent.Marker) event;
                        Log.d("V2XManager", "marker<-->onAck::" + marker.toString());
                    }
                }

                @Override
                public void onFail(@NonNull String s) {
                    Log.d("V2XManager", "onFail::" + s);
                }
            });
        });

        MoGoAiCloudClient.getInstance().addTokenCallbacks(new IMoGoTokenCallback() {
            @Override
            public void onTokenGot(String token, String sn) {
                if (btnV2XFunctionTest.getVisibility() != View.VISIBLE) {
                    btnV2XFunctionTest.setVisibility(View.VISIBLE);
                }
                initPassportInfo();
            }

            @Override
            public void onError(int code, String msg) {
                initPassportInfo();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MoGoAiCloudClient.getInstance().refreshToken();
    }

    /**
     * 初始化令牌信息
     */
    private void initPassportInfo() {
        tvAppKey.setText("AppKey：" + MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getThirdPartyAppKey());
        tvDevicesId.setText("DevicesId：" + MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getThirdPartyDeviceId());

        String sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();
        String token = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getToken();

        if (TextUtils.isEmpty(sn)) {
            sn = "获取失败，请重试";
        }
        if (TextUtils.isEmpty(token)) {
            token = "获取失败，请重试";
        }
        tvSn.setText("SN：" + sn);
        tvToken.setText("Token：" + token);
    }
}