package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.baidu.mapapi.bikenavi.BikeNavigateHelper;
import com.baidu.mapapi.walknavi.adapter.IWRoutePlanListener;
import com.baidu.mapapi.walknavi.model.WalkRoutePlanError;
import com.baidu.mapapi.walknavi.params.*;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.model.LatLng;
import java.util.ArrayList;
import java.util.List;

import android.widget.ImageButton;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.os.Bundle;
import com.baidu.baidunavis.BaiduNaviParams;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.walknavi.WalkNavigateHelper;
import com.baidu.mapapi.walknavi.adapter.IWEngineInitListener;
import com.baidu.mapapi.walknavi.params.WalkNaviLaunchParam;

public class itemfound extends AppCompatActivity {
    private WalkNavigateHelper helper = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //initPermissions();  // 初始化权限
        setContentView(R.layout.activity_itemfound); // 填充视图

        // 点击按钮开始导航
        Button imageButton = findViewById(R.id.button_item_keys);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 初始化步行导航
                startBikeNavi();
            }
        });
        Button imageButtonwallet = findViewById(R.id.button_item_wallet);
        imageButtonwallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 初始化步行导航
                startBikeNavi();
            }
        });
    }
    // 初始化权限
    /*
    private void initPermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            ArrayList<String> permissionsList = new ArrayList<>();
            String[] permissions = {
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.INTERNET,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
            };

            for (String perm : permissions) {
                if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(perm)) {
                    permissionsList.add(perm);
                    // 进入到这里代表没有权限.
                }
                if (!permissionsList.isEmpty()) {
                    String[] strings = new String[permissionsList.size()];
                    requestPermissions(permissionsList.toArray(strings), 0);
                }
            }

        }
    }*/
    private void startBikeNavi() {
        //Log.d("=========", "startBikeNavi");
        try {
            helper = WalkNavigateHelper.getInstance();
            helper.initNaviEngine(this, new IWEngineInitListener() {
                @Override
                public void engineInitSuccess() {
             //       Log.d("=========", "BikeNavi engineInitSuccess引擎初始化成功");
                    startWebNavi();
                }

                @Override
                public void engineInitFail() {
               //     Log.d("=========", "BikeNavi engineInitFail");
                    BikeNavigateHelper.getInstance().unInitNaviEngine();
                }
            });
        } catch (Exception e) {
          //  Log.d("=========", "startBikeNavi Exception");
            e.printStackTrace();
        }
    }
    private void startWebNavi() {
        //起终点位置
        LatLng walkStartNode = new LatLng(39.110132, 117.358512);
        LatLng walkEndNode = new LatLng(39.111348, 117.358940);

        WalkRouteNodeInfo startNode = new WalkRouteNodeInfo();
        startNode.setLocation(walkStartNode);
        WalkRouteNodeInfo endNode = new WalkRouteNodeInfo();
        endNode.setLocation(walkEndNode);
        // 官网
        //构造WalkNaviLaunchParam
        WalkNaviLaunchParam walkParam = new 	WalkNaviLaunchParam().startNodeInfo(startNode).endNodeInfo(endNode);
        //获取WalkNavigateHelper实例 //发起算路
        helper.routePlanWithRouteNode(walkParam, new IWRoutePlanListener() {
            @Override
            public void onRoutePlanStart() {
              //  Log.d("=======", "WalkNavi onRoutePlanStart开始步行导航");
            }

            @Override
            public void onRoutePlanSuccess() {
               // Log.d("========", "onRoutePlanSuccess算路成功");
                Intent intent = new Intent(itemfound.this,WNaviGuideActivity.class);
               // intent.setClass(getContext(), WNaviGuideActivity.class);
                startActivity(intent);
            }

            @Override
            public void onRoutePlanFail(WalkRoutePlanError error) {
//                Log.d("Walk", "WalkNavi onRoutePlanFail");
            }

        });
    }


/*
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_itemfound);
        Button find_wallet=findViewById(R.id.button_item_wallet);
        Button find_keys=findViewById(R.id.button_item_keys);
        find_wallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//Intent
                double locationX;
                double locationy;
                //BaiduMapNavigation


            }
        });
        find_keys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//Intent
            //NaviParaOption para=new Navi();

            }
        });
    }*/    private void startBikeNavi1() {
    //Log.d("=========", "startBikeNavi");
    try {
        helper = WalkNavigateHelper.getInstance();
        helper.initNaviEngine(this, new IWEngineInitListener() {
            @Override
            public void engineInitSuccess() {
                //       Log.d("=========", "BikeNavi engineInitSuccess引擎初始化成功");
                startWebNavi1();
            }

            @Override
            public void engineInitFail() {
                //     Log.d("=========", "BikeNavi engineInitFail");
                BikeNavigateHelper.getInstance().unInitNaviEngine();
            }
        });
    } catch (Exception e) {
        //  Log.d("=========", "startBikeNavi Exception");
        e.printStackTrace();
    }
}
    private void startWebNavi1() {
        //起终点位置
        LatLng walkStartNode = new LatLng(39.110132, 117.358512);
        LatLng walkEndNode = new LatLng(39.112286, 117.359619);

        WalkRouteNodeInfo startNode = new WalkRouteNodeInfo();
        startNode.setLocation(walkStartNode);
        WalkRouteNodeInfo endNode = new WalkRouteNodeInfo();
        endNode.setLocation(walkEndNode);
        // 官网
        //构造WalkNaviLaunchParam
        WalkNaviLaunchParam walkParam = new 	WalkNaviLaunchParam().startNodeInfo(startNode).endNodeInfo(endNode);
        //获取WalkNavigateHelper实例 //发起算路
        helper.routePlanWithRouteNode(walkParam, new IWRoutePlanListener() {
            @Override
            public void onRoutePlanStart() {
                //  Log.d("=======", "WalkNavi onRoutePlanStart开始步行导航");
            }

            @Override
            public void onRoutePlanSuccess() {
                // Log.d("========", "onRoutePlanSuccess算路成功");
                Intent intent = new Intent(itemfound.this,WNaviGuideActivity.class);
                // intent.setClass(getContext(), WNaviGuideActivity.class);
                startActivity(intent);
            }

            @Override
            public void onRoutePlanFail(WalkRoutePlanError error) {
//                Log.d("Walk", "WalkNavi onRoutePlanFail");
            }

        });
    }
}
