package com.example.shz.hainuo_ims.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.example.shz.hainuo_ims.R;
import com.example.shz.hainuo_ims.activity.MainActivity;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * Created by shz on 2017/8/12.
 */

public class LocationFragment extends BaseTabFragment {

    public AMapLocationClientOption mLocationOption;
    public AMapLocationClient mLocationClient;
    public AMapLocationListener mLocationListener;
    private MapView mMapView;
    private AMap mAMap;
    private TextView mTvLoction;
    private double lat;
    private double lon;
    private String country;
    private String province;
    private String city;
    private StringBuilder mStringBuilder;

    @Override
    protected void init() {
        if (mAMap == null) {
            mAMap = mMapView.getMap();
            setupMap();
        }
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.navi_map_gps_locked);
//        BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
//        MyLocationStyle myLocationStyle = new MyLocationStyle(descriptor);
//        myLocationStyle.interval(2000);
//        mAMap.setMyLocationStyle(myLocationStyle);

        initLocationOption();
        initLocation();

    }



    private void initLocationOption() {
        if (mLocationOption == null) {
            mLocationOption = new AMapLocationClientOption();
        }
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setNeedAddress(true);
        mLocationOption.setInterval(2000);
    }

    private void initLocation() {
        mLocationClient = new AMapLocationClient(getContext());
        mLocationClient.setLocationOption(mLocationOption);
        mLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        lat = aMapLocation.getLatitude();//获取纬度
                        lon = aMapLocation.getLongitude();//获取经度
                        country = aMapLocation.getCountry();//国家信息
                        province = aMapLocation.getProvince();//省信息
                        city = aMapLocation.getCity();//城市信息

                        if (mStringBuilder == null) {
                            mStringBuilder = new StringBuilder();
                            mStringBuilder.append("经度:");
                            mStringBuilder.append(lon);
                            mStringBuilder.append("  ");
                            mStringBuilder.append("纬度:" + lat + "\n");
                            mStringBuilder.append("国家:" + country + " ");
                            mStringBuilder.append("省:" + province + " ");
                            mStringBuilder.append("城市:" + city);
                        }

//                        mAMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon)));
                        mTvLoction.post(new Runnable() {
                            @Override
                            public void run() {
                                mTvLoction.setText(mStringBuilder.toString());
                            }
                        });


                    }
                }
            }
        };

        mLocationClient.setLocationListener(mLocationListener);
        mLocationClient.startLocation();
    }

    private void setupMap() {
        mAMap.moveCamera(CameraUpdateFactory.zoomTo(14));
        mAMap.getUiSettings().setMyLocationButtonEnabled(true);
        mAMap.setMyLocationEnabled(true);
        setupLocationStyle();
    }

    private void setupLocationStyle() {
        // 自定义系统定位蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 自定义定位蓝点图标
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
                fromResource(R.drawable.gps_point));
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);
        // 自定义精度范围的圆形边框颜色
//        myLocationStyle.strokeColor(STROKE_COLOR);
        //自定义精度范围的圆形边框宽度
//        myLocationStyle.strokeWidth(5);
        // 设置圆形的填充颜色
//        myLocationStyle.radiusFillColor(FILL_COLOR);
        // 将自定义的 myLocationStyle 对象添加到地图上
        mAMap.setMyLocationStyle(myLocationStyle);
    }

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_location_query, container, false);
        mMapView = (MapView) inflate.findViewById(R.id.map);
        mTvLoction = (TextView) inflate.findViewById(R.id.tv_location);
        mMapView.onCreate(savedInstanceState);
        return inflate;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        mLocationClient.stopLocation();
        mLocationClient.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mLocationClient.stopLocation();
    }
}
