package com.example.mountaintravel.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.res.AssetManager;

public class AppJsonFileReader {
	public static String getJson(Context context, String fileName) {

		StringBuilder stringBuilder = new StringBuilder();
		try {
			AssetManager assetManager = context.getAssets();
			BufferedReader bf = new BufferedReader(new InputStreamReader(
					assetManager.open(fileName)));
			String line;
			while ((line = bf.readLine()) != null) {
				stringBuilder.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stringBuilder.toString();
	}

	public static List<List<Double>> setData(String str) {
		try {
			List<List<Double>> data = new ArrayList<List<Double>>();
			JSONObject obj = new JSONObject(str);
			JSONArray array = obj.getJSONArray("RECORDS");
			int len = array.length();
			List<Double> map;
			for (int i = 0; i < len; i++) {
				JSONObject object = array.getJSONObject(i);
				map = new ArrayList<Double>();
				map.add(object.getDouble("POINT_X"));
				map.add(object.getDouble("POINT_Y"));
				data.add(map);
			}
			return data;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}

	public static List<List<String>> getjson(String str) {
		try {
			List<List<String>> data = new ArrayList<List<String>>();
			JSONObject obj = new JSONObject(str);
			JSONArray array = obj.getJSONArray("RECORDS");
			List<String> map;
			for (int i = 0; i < array.length(); i++) {
				map = new ArrayList<String>();
				JSONObject object = array.getJSONObject(i);
				map.add(String.valueOf(object.getDouble("POINT_X")));
				map.add(String.valueOf(object.getDouble("POINT_Y")));
				map.add(object.getString("NAME"));
				map.add(object.getString("CODE"));
				data.add(map);
			}
			return data;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}

	public static List< Map<String, Object>> getGeometryPoint(String str) {
		try {
			List< Map<String, Object>> data = new ArrayList< Map<String, Object>>();
			JSONObject obj = new JSONObject(str);
			JSONArray array = obj.getJSONArray("features");
			int len = array.length();
			 Map<String, Object> map;
			for (int i = 0; i < len; i++) {
				map = new HashMap<String, Object>();
				JSONObject object = array.getJSONObject(i);
				JSONObject attributes = object.getJSONObject("attributes");
				String name = attributes.getString("name");
				JSONObject geometry = object.getJSONObject("geometry");
				Double x = geometry.getDouble("x");
				Double y = geometry.getDouble("y");
				map.put("name",name);
				map.put("x",x+"");
				map.put("y",y+"");
				data.add(map);
			}
			return data;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}

	public static List<Map<String, String>> bluetoothJson(String str) {
		try {
			List<Map<String, String>> data = new ArrayList<Map<String, String>>();
			// List<String > map = null;
			Map<String, String> arraymap;
			JSONObject obj = new JSONObject(str);
			JSONArray array = obj.getJSONArray("bluetoothAddress");
			for (int i = 0; i < array.length(); i++) {
				JSONObject obj2 = array.getJSONObject(i);
				// String NAME=obj2.getString("NAME");
				String ATTCODE = obj2.getString("ATTCODE");
				JSONArray array2 = obj2.getJSONArray("id");
				arraymap = new HashMap<String, String>();
				for (int j = 0; j < array2.length(); j++) {
					JSONObject obj3 = array2.getJSONObject(j);
					// String CODE=obj3.getString("CODE");
					String SN = obj3.getString("SN");
					arraymap.put(SN, ATTCODE);
				}
				// arraymap.put("name",NAME);
				data.add(arraymap);
			}
			return data;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}
}
