package com.ecom.park_jy;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

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

import static com.ecom.park_jy.Start.postion_box;

public class Skin_Road_list_fragment_1 extends Fragment {
    private ListView listView;
    ArrayList<HashMap<String, String>> list_1 = new ArrayList<HashMap<String, String>>();
    //ArrayList<HashMap<String,String>> list_2 = new ArrayList<HashMap<String, String>>();
    String findin, findout;
    String[] saveRoad;
    Intent intent = new Intent();
    private View view;

    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle save) {
        view = inflater.inflate(R.layout.skin_road_list_fragment_list, vg, false);
        return view;
    }

    public void onViewCreated(View v, Bundle save) {
        super.onViewCreated(v, save);
        listView = (ListView) getView().findViewById(R.id.road_list);
        File road_select = new File(Environment.getExternalStorageDirectory() + "/Park_JY/" + "A117" + ".temp");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(road_select)));
            while ((findin = reader.readLine()) != null) {
                findout = findin;
                saveRoad = findout.split("\n");
                for (int i = 0; i < saveRoad.length; i++) {
                    HashMap<String, String> road = new HashMap<String, String>();
                    road.put("data", saveRoad[i]);
                    list_1.add(road);
                }
            }
            reader.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

        int[] type = new int[saveRoad.length];
        for (int j = 0; j < saveRoad.length; j += 2) {
            type[j] = 0;
            if (j + 1 < saveRoad.length) {
                type[j + 1] = 1;
            }
        }

        Skin_Road_list_Fragment_Adapter sRlist = new Skin_Road_list_Fragment_Adapter(this.getActivity(), list_1, type);
        listView.setAdapter(sRlist);
        listView.setSelection(postion_box);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent.setClass(getActivity(), Start.class);
                intent.putExtra("選項", position);
                intent.putExtra("車格", list_1.get(position).toString().substring(6, list_1.get(position).toString().length() - 1));
                getActivity().setResult(Activity.RESULT_OK, intent);
                getActivity().finish();
            }
        });

    }

    public void onDestroyView() {
        super.onDestroyView();
    }

}
