package com.mogo.cloud.tanlu.api;

import android.content.Context;

import com.elegant.network.utils.GsonUtil;
import com.mogo.cloud.network.BaseData;
import com.mogo.cloud.network.NetConstants;
import com.mogo.cloud.network.RetrofitFactory;
import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.tanlu.bean.RoadInfoRequest;
import com.mogo.cloud.tanlu.bean.RoadInfos;
import com.mogo.cloud.tanlu.net.TanluApiService;

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;

/**
 * @author lixiaopeng
 * @description 道路搜索
 * @since 2021/1/21
 */
public class MogoRoadSearchManager {
    private static MogoRoadSearchManager sInstance;
    private Context mContext;
    private TanluApiService apiService;


    private MogoRoadSearchManager(Context context) {
        mContext = context;
    }

    public static MogoRoadSearchManager getInstance(Context context) {
        if (sInstance == null) {
            synchronized (MogoRoadSearchManager.class) {
                sInstance = new MogoRoadSearchManager(context);
            }
        }

        return sInstance;
    }

    /**
     * 初始化
     */
    public void init() {
        apiService = RetrofitFactory.INSTANCE.getInstance(NetConstants.LAUNCHER_SNAPSHOT_HOST)
                .create(TanluApiService.class);
    }


    /**
     * 通过经纬度信息查询
     *
     * @param roadInfoRequest 道路查询入参
     * @param callback {@link IRoadInfoSearchCallback}
     */
    public void queryRoadInfo(RoadInfoRequest roadInfoRequest, IRoadInfoSearchCallback callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("sn", MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn());
        map.put("data", GsonUtil.jsonFromObject(roadInfoRequest));

        apiService.queryRoadInfos(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData<RoadInfos>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseData<RoadInfos> roadInfos) {
                        callback.onSuccess(roadInfos);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callback.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
