package com.mogo.cloud.live.manager;

import android.util.Log;

import com.elegant.network.utils.GsonUtil;
import com.mogo.cloud.live.model.BaseData;
import com.mogo.cloud.live.model.CameraStatusModel;
import com.mogo.cloud.live.network.LiveApiServer;
import com.mogo.cloud.network.NetConstants;
import com.mogo.cloud.network.RetrofitFactory;
import com.mogo.cloud.passport.MoGoAiCloudClient;

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;
import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 摄像头状态管理
 */
public class CameraStatusManager {
    private final String TAG = "CameraStatusManager";

    private static volatile CameraStatusManager requestLiveManager;
    private final LiveApiServer liveApiServer;

    private CameraStatusManager() {
        liveApiServer = RetrofitFactory.INSTANCE.getInstance(NetConstants.TECH_HOST)  //todo 涉及摄像头状态管理接口 没有对HttpDNS进行配置
                .create(LiveApiServer.class);
    }

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

    /**
     * 上传当前车机摄像头状态
     *
     * @param c_1 前置
     * @param c_2 后置
     */
    public void reportCameraStatus(int c_1, int c_2) {
        try {
            String sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();

            CameraStatusModel cameraStatusModel = new CameraStatusModel();
            CameraStatusModel.CameraStatus cameraStatus = new CameraStatusModel.CameraStatus();
            cameraStatus.setC_1(c_1);
            cameraStatus.setC_2(c_2);

            cameraStatusModel.setSn(sn);
            cameraStatusModel.setCaller("AiSDK");
            cameraStatusModel.setSdkVersion("1.0.1");
            cameraStatusModel.setCameraStatus(cameraStatus);

            RequestBody requestBody = RequestBody.create( MediaType.get( "application/json;charset=UTF-8" ), GsonUtil.jsonFromObject( cameraStatusModel ) );


            liveApiServer.reportCameraStatus(requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<BaseData>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {
                            Log.d(TAG, "上传摄像头状态 onSubscribe");
                        }

                        @Override
                        public void onNext(@NonNull BaseData baseData) {
                            Log.d(TAG, "上传摄像头状态 onNext");
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            Log.e(TAG, "上传摄像头状态 onError");
                        }

                        @Override
                        public void onComplete() {
                            Log.d(TAG, "上传摄像头状态 onComplete");
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
