package com.ecom.park_jy;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class GPS_Search extends AppCompatActivity {
    ListView listView;
    String[] car_GPS,gps2;
    String findin,findout;
    Intent intent = new Intent();
    Bundle bundle;
    int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gps__search);

        bundle = this.getIntent().getExtras();
        listView = (ListView)findViewById(R.id.GPS_list);
        final List list = new ArrayList();
        final List list2 = new ArrayList();
        final List list3 = new ArrayList();

        File car = new File(Environment.getExternalStorageDirectory() + "/Park_JY/All_GPS.txt");
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(car)));
            while((findin = reader.readLine()) != null){
                findout = findin;
                car_GPS = findout.split("\n");
                for(int i = 0; i < car_GPS.length; i++){
                    list.add(car_GPS[i]);
                    gps2 = car_GPS[i].split(" ");

                    if(gps2.length > 5 ){

                        if((bundle.getDouble("Lat") - Double.valueOf(gps2[4].substring(4)) >= -0.00002 && bundle.getDouble("Lat") - Double.valueOf(gps2[4].substring(4)) <= 0.00002) && (bundle.getDouble("Lon") - Double.valueOf(gps2[5].substring(4)) <= 0.00002 && bundle.getDouble("Lon") - Double.valueOf(gps2[5].substring(4)) >= -0.00002)){
                            if(count > 9){
                                break;
                            }
                            count ++;
                            list2.add(car_GPS[i]);
                            list3.add(gps2[0]);
                        }
                    }
                }
            }
            reader.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        ArrayAdapter adapter = new ArrayAdapter(this,R.layout.skin_user_list_text,R.id.textView43,list3);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //String name = (String)listView.getItemAtPosition(position);
                intent.putExtra("GPS車格",list2.get(position).toString());
                //intent.putExtra("dataCount",Integer.toString(list.size()));
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }
}
