package com.mogo.cloud.live.manager;

import static com.mogo.cloud.live.constant.LiveConstant.FRONT_CAMERA;

import android.util.Log;

import com.google.gson.Gson;
import com.mogo.cloud.live.constant.LiveConstant;
import com.mogo.cloud.live.constant.LiveHostConst;
import com.mogo.cloud.live.listener.IRequestLiveListener;
import com.mogo.cloud.live.model.BaseData;
import com.mogo.cloud.live.model.Camera;
import com.mogo.cloud.live.model.LiveCamera;
import com.mogo.cloud.live.model.LivePush;
import com.mogo.cloud.live.model.LiveReceive;
import com.mogo.cloud.live.network.LiveApiServer;
import com.mogo.cloud.network.RetrofitFactory;
import com.mogo.cloud.passport.MoGoAiCloudClient;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 请求直播管理类
 */
public class RequestLiveManager {

    private static volatile RequestLiveManager requestLiveManager;
    private final LiveApiServer liveApiServer;

    private RequestLiveManager() {
        liveApiServer = RetrofitFactory.INSTANCE.getInstance(LiveHostConst.getEagleHost())
                .create(LiveApiServer.class);
    }

    public static RequestLiveManager getInstance() {
        if (requestLiveManager == null) {
            synchronized (RequestLiveManager.class) {
                if (requestLiveManager == null) {
                    requestLiveManager = new RequestLiveManager();
                }
            }
        }
        return requestLiveManager;
    }

    /**
     * 请求服务器查看指定车机开启或关闭直播
     *
     * @param type                0:open  1:close
     * @param liveSn              直播SN
     * @param requestLiveListener {@link IRequestLiveListener}
     */
    public void requestDesignativeVehicleLive(String type, String liveSn, IRequestLiveListener requestLiveListener) {
        Gson gson = new Gson();
        LivePush livePush = new LivePush(liveSn, type, FRONT_CAMERA);
        String sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();
        Map<String, String> map = new HashMap<>();
        map.put("sn", sn);
        map.put("data", gson.toJson(livePush));
        liveApiServer.getDesignativeVehicleLive(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseData baseData) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (requestLiveListener != null) {
                            requestLiveListener.onError(e);
                        }
                        Log.e(LiveConstant.TAG, "requestDesignativeVehicleLive exception : " + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

        if (requestLiveListener != null) {
            requestLiveListener.onSuccess();
        }
    }

    /**
     * 请求服务器查看前方车机开启直播
     *
     * @param lat                 纬度
     * @param lon                 经度
     * @param bearing             方向角
     * @param requestLiveListener {@link IRequestLiveListener}
     */
    public void requestFrontVehicleLive(double lat, double lon, int bearing, IRequestLiveListener requestLiveListener) {
        Gson gson = new Gson();
        String sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();
        LivePush livePush = new LivePush(sn, FRONT_CAMERA, lat, lon, bearing);
        Map<String, String> map = new HashMap<>();
        map.put("sn", sn);
        map.put("data", gson.toJson(livePush));
        liveApiServer.getFrontVehicleLive(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData<LiveReceive>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseData<LiveReceive> baseData) {
                        if (requestLiveListener != null && baseData != null) {
                            LiveReceive liveReceive = baseData.result;
                            if (liveReceive != null && liveReceive.getSn() != null) {
                                requestLiveListener.onSuccess(liveReceive.getSn(), liveReceive.getLat(), liveReceive.getLon());
                            } else {
                                requestLiveListener.onError(new Throwable("前方暂无可直播车机"));
                            }
                        } else {
                            requestLiveListener.onError(new Throwable("前方暂无可直播车机"));
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (requestLiveListener != null) {
                            requestLiveListener.onError(e);
                        }
                        Log.e(LiveConstant.TAG, "requestFrontVehicleLive exception : " + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 请求服务器查看前方路口直播
     *
     * @param lat                 纬度
     * @param lon                 经度
     * @param bearing             方向角
     * @param requestLiveListener {@link IRequestLiveListener}
     */
    public void requestFrontIntersectionLive(double lat, double lon, int bearing, IRequestLiveListener requestLiveListener) {
        Gson gson = new Gson();
        String sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();
        LivePush livePush = new LivePush(sn, lat, lon, bearing);
        Map<String, String> map = new HashMap<>();
        map.put("sn", sn);
        map.put("data", gson.toJson(livePush));
        liveApiServer.getFrontIntersectionLive(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData<LiveReceive>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseData<LiveReceive> baseData) {
                        if (requestLiveListener != null && baseData != null) {
                            LiveReceive liveReceive = baseData.result;
                            if (liveReceive != null && liveReceive.getLive() != null) {
                                requestLiveListener.onSuccess(liveReceive.getLive());
                            } else {
                                requestLiveListener.onError(new Throwable("前方路口暂无可直播设备"));
                            }
                        } else {
                            requestLiveListener.onError(new Throwable("前方路口暂无可直播设备"));
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (requestLiveListener != null) {
                            requestLiveListener.onError(e);
                        }
                        Log.e(LiveConstant.TAG, "requestFrontIntersectionLive exception : " + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 请求服务器查看特定设备路口直播
     *
     * @param cameraId            设备id
     * @param requestLiveListener {@link IRequestLiveListener}
     */
    public void requestDesignativeIntersectionLive(String cameraId, IRequestLiveListener requestLiveListener) {
        Gson gson = new Gson();
        String sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();
        Map<String, Object> map = new HashMap<>();
        map.put("unitSn", cameraId);
        liveApiServer.getDesignativeIntersectionLive(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData<LiveCamera>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseData<LiveCamera> baseData) {
                        if (requestLiveListener != null && baseData != null) {
                            LiveCamera liveData = baseData.result;
                            if (liveData == null || liveData.getCamera() == null || liveData.getCamera().getRtmpUrl() == null) {
                                requestLiveListener.onError(new Throwable("此路口摄像头暂无直播流"));
                                return;
                            }
                            Camera camera = liveData.getCamera();
                            requestLiveListener.onSuccess(camera.getRtmpUrl());
                        } else {
                            requestLiveListener.onError(new Throwable("此路口摄像头暂无直播流"));
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (requestLiveListener != null) {
                            requestLiveListener.onError(e);
                        }
                        Log.e(LiveConstant.TAG, "requestDesignativeIntersectionLive exception : " + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
