package com.navinfo.outdoor.util;

import android.content.Context;
import android.location.Location;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.navinfo.outdoor.api.Constant;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;
import com.tencent.tencentmap.mapsdk.maps.LocationSource;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class TecentLocationUtils implements TencentLocationListener{
    private TencentLocationManager locationManager;
    private TencentLocationRequest locationRequest;
    private List<LocationSource.OnLocationChangedListener> locationChangedListenerList;
    private boolean isLocationStart; // 标识是否已经启动定位

    private static TecentLocationUtils instance;

    public static TecentLocationUtils getInstance(Context mContext) {
        if (instance == null) {
            instance = new TecentLocationUtils(mContext);
        }
        return instance;
    }

    public TecentLocationUtils(Context mContext) {
        init(mContext);
    }

    private void init(Context mContext) {
        locationChangedListenerList = new ArrayList<>();
        //用于访问腾讯定位服务的类, 周期性向客户端提供位置更新
        locationManager = TencentLocationManager.getInstance(mContext);
        //设置坐标系
        locationManager.setCoordinateType(TencentLocationManager.COORDINATE_TYPE_GCJ02);
        //创建定位请求
        locationRequest = TencentLocationRequest.create();
        locationRequest.setInterval(1000); // 每1秒返回一次地址数据
        locationRequest.setAllowDirection(true);
        locationRequest.setAllowGPS(true);
        locationRequest.setIndoorLocationMode(true);
    }

    public void startLocation(Context mContext) { // 启动定位服务
        if (locationManager == null) {
            this.init(mContext);
        }
        if (!isLocationStart) {
            int err = locationManager.requestLocationUpdates(locationRequest, this, Looper.myLooper());
            isLocationStart = true;
            switch (err) {
                case 1:
                    ToastUtil.showShort(mContext, Constant.GET_ERR_MESSAGE1);
                    break;
                case 2:
                    ToastUtil.showShort(mContext, Constant.GET_ERR_MESSAGE2);
                    break;
                case 3:
                    ToastUtil.showShort(mContext, Constant.GET_ERR_MESSAGE3);
                    break;
                default:
                    break;
            }
        }
    }

    public void stopLocation() { // 停止定位服务
        if (isLocationStart) {
            locationManager.removeUpdates(this);
            locationManager = null;
            locationRequest = null;
            locationChangedListenerList.clear();
            locationChangedListenerList = null;
        }
    }

    /**
     * 实现位置监听
     *
     * @param tencentLocation
     * @param i
     * @param s
     */
    @Override
    public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
        if (i == TencentLocation.ERROR_OK) {
            Location location = new Location(tencentLocation.getProvider());
//            //设置经纬度以及精度
            location.setLatitude(tencentLocation.getLatitude());
            location.setLongitude(tencentLocation.getLongitude());
            location.setAccuracy(tencentLocation.getAccuracy());
            if (tencentLocation.getBearing()!=0) {
                location.setBearing(tencentLocation.getBearing());
            } else {
                location.setBearing((float) tencentLocation.getDirection());
            }
            if (locationChangedListenerList!=null) {
                for (LocationSource.OnLocationChangedListener locationChangedListener:locationChangedListenerList) {
                    locationChangedListener.onLocationChanged(location);
                }
            }
            Constant.currentLocation = tencentLocation;
            Message currentLocationMsg = Message.obtain();
            currentLocationMsg.what = Constant.EVENT_WHAT_LOCATION_CHANGE;
            currentLocationMsg.obj = Constant.currentLocation;
            EventBus.getDefault().post(currentLocationMsg);
        }
    }

    @Override
    public void onStatusUpdate(String s, int i, String s1) {
        //GPS, WiFi, Radio 等状态发生变化
        Log.v("State changed", s + "===" + s1);
    }

    public List<LocationSource.OnLocationChangedListener> getLocationChangedListenerList() {
        return locationChangedListenerList;
    }
}
