package com.equifax.poc.services.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.equifax.poc.pojo.CityQuery;
import com.equifax.poc.pojo.GeoReference;
import com.equifax.poc.pojo.input.LocalizationReport;
import com.equifax.poc.services.TBDService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class TBDServiceImpl implements TBDService {

	@Override
	public List<GeoReference> getReferenced(CityQuery cityQuery) {
		List<GeoReference> result = new ArrayList<GeoReference>();
		
		/*GeoReference geo = new GeoReference();
		
		geo.setCity("Atlanta");
		geo.setStreet("805 Peachtree St NE");
		geo.setZip( "30308" );
		geo.setState( "GA" );
		
		result.add(geo);*/
		
		try {
			result = readFromFile(cityQuery);
		} catch (FileNotFoundException e) {
			result = new ArrayList<GeoReference>();
		} catch (IOException e) {
			result = new ArrayList<GeoReference>();
		}
		
		return result;
	}

	
	private List<GeoReference> readFromFile(CityQuery cityQuery) throws IOException, FileNotFoundException{
		List<GeoReference> geos = new ArrayList<GeoReference>();
		
		String line = "";
		String fileName="MOCK_DATA_MM.json";
		if("H".equals(cityQuery.getWageCategory()) && "H".equals(cityQuery.getPatrimony())){
			fileName="MOCK_DATA_HH.json";
		}else if ("L".equals(cityQuery.getWageCategory()) && "H".equals(cityQuery.getPatrimony())){
			fileName="MOCK_DATA_LH.json";
		}
		System.out.println(cityQuery.getWageCategory());
		System.out.println(cityQuery.getPatrimony());
		
		BufferedReader bread = new BufferedReader(new FileReader(fileName));
		
		StringBuilder builder = new StringBuilder();
		
		while( (line = bread.readLine()) != null ){
			builder.append(line);
		}
		
		bread.close();
		geos = convertFromJSON(builder.toString());		
		return geos;
	}
	
	private List<GeoReference> convertFromJSON(String jsonContent){
		List<GeoReference> res = new ArrayList<GeoReference>();
		
		Gson gson = new GsonBuilder().create();
		List infos = gson.fromJson(jsonContent, List.class);
		
		int i = 1;
		
		for (Object geoInfo : infos) {
			System.out.println(i++);
			
			GeoReference geo = new GeoReference();
			Map<?, ?> info = (Map<?, ?>)geoInfo;
			
			String lat = String.valueOf( info.get("lat") );
			String longitude = String.valueOf( info.get("longitude") );
			String city = String.valueOf( info.get("city") );
			String income = String.valueOf( info.get("Income") );
			String patrimony = String.valueOf( info.get("Patrimony") );
			
			geo.setLatitude( Double.parseDouble( lat ) );
			geo.setLongitude( Double.parseDouble( longitude ) );
			geo.setCity( city );
			geo.setState( "" );
			geo.setStreet( "" );
			geo.setIncome( Double.parseDouble(income) );
			geo.setPatrimony( Double.parseDouble(patrimony));
			
			res.add(geo);
		}
		
		return res;
	}


	@SuppressWarnings("resource")
	@Override
	public void addAtlantaLocationsOnLocalizationReport(
			List<LocalizationReport> localizationReports) {
		try 
		{
			InputStreamReader read = new InputStreamReader(
					new FileInputStream("files/RandomPointsAtlanta.csv"), "UTF-8");
			
			BufferedReader reader = new BufferedReader(read);
			
			String line;
			
			int i = 0;
			
			while ((line = reader.readLine()) != null) 
			{
				if(localizationReports.get(i) == null)
				{
					return;
				}
				
				String[] xyz = line.split(",");			
				localizationReports.get(i).setLat(Double.parseDouble(xyz[1]));
				localizationReports.get(i).setLongitude(Double.parseDouble(xyz[3]));
				
				i++;
			}
			
			reader.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
}
