package com.ahau.againstpest.model;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.ahau.againstpest.HttpProcessor.HttpCallback;
import com.ahau.againstpest.HttpProcessor.HttpHelper;
import com.ahau.againstpest.base.MyApplication;
import com.ahau.againstpest.bean.PestBean;
import com.ahau.againstpest.utils.LoginUtils;
import com.ahau.againstpest.utils.URLUtils;
import com.amap.api.maps2d.CoordinateConverter;
import com.amap.api.maps2d.model.LatLng;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static android.support.v4.content.ContextCompat.getSystemService;


/**
 * 注释:
 *
 * @author fanchunchun
 * @date 2018/4/3
 */

public class PestModelImpl implements IPestModel {


    /**
     * 害虫的名字
     *
     * @param pestGetListener
     */
    @Override
    public void getPest(final PestGetListener pestGetListener) {

        Map<String, Object> params = new HashMap<>();

        HttpHelper.obtain().get(URLUtils.PestUrl, params, new HttpCallback<PestBean>() {
            @Override
            public void onSuccess(PestBean pestBean) {

                ArrayList<PestBean.Pest> mPestNameList;
                //String[] arr;

                mPestNameList = pestBean.pest;
                Log.i("TAG", pestBean.toString());

             /*   *
                 * 设置虫种
                arr = new String[mPestNameList.size() + 1];
                arr[0] = "请选择：";
                for (int i = 0; i < mPestNameList.size(); i++) {
                    arr[i + 1] = mPestNameList.get(i).pestname;
                }
                Log.i("tag", arr[0]);*/
                pestGetListener.onComplete(mPestNameList);
            }

            @Override
            public void onFailure(String e) {
                Log.e("tag", "网络请求失败");
            }

        });

    }

    /**
     * 当前日期
     *
     * @param dateListener
     */
    @Override
    public void getDate(DateListener dateListener) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// yyyy/MM/dd  HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        String sDate = simpleDateFormat.format(date);
        dateListener.onComplete(sDate);
    }

    /**
     * 经纬度数据
     *
     * @param latlngListener
     */
    @Override
    public void getLatlng(LatlngListener latlngListener) {
        String[] latlng = new String[4];
        String jingdu, weidu, gaodejingdu, gaodeweidu; //经纬度
//        LocationManager locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationManager locationmanager = (LocationManager) MyApplication.getInstance().getSystemService(Context.LOCATION_SERVICE);

        if (ActivityCompat.checkSelfPermission(MyApplication.getInstance(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MyApplication.getInstance(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = locationmanager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null) {
            double lat = location.getLatitude();// 经度
            double lng = location.getLongitude();// 纬度
            System.out.println(lat);
            jingdu = "" + lat;
            weidu = "" + lng;

        } else {
            location = locationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                double lat = location.getLatitude();// 经度
                double lng = location.getLongitude();// 纬度
                System.out.println(lat);
                jingdu = "" + lat;
                weidu = "" + lng;
            } else {
                jingdu = "";
                weidu = "";
            }
        }
        if (!jingdu.isEmpty() && !weidu.isEmpty()) {

            LatLng latLng = new LatLng(Double.valueOf(jingdu), Double.valueOf(weidu));
            CoordinateConverter converter = new CoordinateConverter();
            // CoordType.GPS 待转换坐标类型
            converter.from(CoordinateConverter.CoordType.GPS);
            // sourceLatLng待转换坐标点 LatLng类型
            converter.coord(latLng);
            // 执行转换操作
            LatLng desLatLng = converter.convert();
            gaodejingdu = "" + desLatLng.latitude;
            gaodeweidu = "" + desLatLng.longitude;
            latlng[2] = gaodejingdu;
            latlng[3] = gaodeweidu;
        }
        latlng[0] = jingdu;
        latlng[1] = weidu;


        latlngListener.onComplete(latlng);

    }


    /**
     * 获取调查人姓名
     *
     * @param researNameListener
     */
    @Override
    public void getResearName(ResearNameListener researNameListener) {
        Map<String, String> map = LoginUtils.getUtil(MyApplication.getGlobalContext());
        String ResearName = map.get("username");
        researNameListener.onComplete(ResearName);
    }

}

