package kr.gmtc.gw.dev.packetsend.file;

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.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;

public class TextReaderMergeAndSort {
    HashMap<String, String> splitMap;
	
	ArrayList<HashMap<String,String>>  splitMaps1 = new ArrayList<HashMap<String,String>>();
	ArrayList<HashMap<String,String>>  splitMaps2 = new ArrayList<HashMap<String,String>>();
	ArrayList<HashMap<String,String>>  mergeList = new ArrayList<HashMap<String,String>>();
	
	Queue<Map<String, String>> fileQ;
	
	private String sfilepath1;
	private String sfilepath2;
	
	public TextReaderMergeAndSort(Queue<Map<String, String>> fileQ, String sFilePath1, String sFilePath2) {
		super();
		
		this.fileQ = fileQ;
		this.sfilepath1 = sFilePath1;
		this.sfilepath2 = sFilePath2;
	}

	public void readFile() {
		try{
	        //파일 객체 생성
	        File file1 = new File(sfilepath1);
	        File file2 = new File(sfilepath2);
	       
            BufferedReader inFiles = new BufferedReader(new InputStreamReader(new FileInputStream(file1), StandardCharsets.UTF_8));
            
            String line = "";
            String lineSplit[] = null;
	        while((line = inFiles.readLine()) != null){	
	        	
	        	splitMap = new HashMap<String, String>();
	        	
	        	lineSplit = line.split("[|]");
	        	
	        	splitMap.put("time", lineSplit[0]);
	        	splitMap.put("data", lineSplit[1]);
	        	
	        	splitMaps1.add(splitMap);

	        }
	        inFiles.close();
	        
	        inFiles = new BufferedReader(new InputStreamReader(new FileInputStream(file2), StandardCharsets.UTF_8));
	        line = "";
	        while((line = inFiles.readLine()) != null){	
	        	splitMap = new HashMap<String, String>();
	        	
	        	lineSplit = line.split("[|]");
	        	
	        	splitMap.put("time", lineSplit[0]);
	        	splitMap.put("data", lineSplit[1]);
	        	
	        	splitMaps2.add(splitMap);
	        		
	        }
	        inFiles.close();
	        
	        mergeList.addAll(splitMaps1);
	        mergeList.addAll(splitMaps2);
	        
	       
	        
	        // queue에 순서대로 담기
	        int iSize = mergeList.size();
	        if(iSize > 0) {
		        for(int idx=0; idx < iSize; idx++) {
		        	
		        	// Unix Time -> KST Time으로 변환
		        	try {
		        		String sUnixTime = mergeList.get(idx).get("time");

		        		sUnixTime = sUnixTime.substring(0, sUnixTime.indexOf(".") );

		        		long ltimestamp = Long.parseLong(sUnixTime);
		        		
						Date localdate = new Date(ltimestamp * 1000L); 
						
						SimpleDateFormat sformat = new SimpleDateFormat("yyyyMMddHHmmss"); 
						
						sformat.setTimeZone(java.util.TimeZone.getTimeZone("GMT+9")); 
						
						String sCnvDate = sformat.format(localdate);
						
						mergeList.get(idx).put("time", sCnvDate);
						
		        	} catch (Exception e) {
						 System.out.println(e);
					}
		        }
	        }

	        // Unix Time 기준으로 오름차순 정렬
	        
	        int iTmp = 0;
	        
	        try {
	        	
	        	while(iTmp <= mergeList.size()) {
	        		
	        		iTmp++;
	        		
	        		Collections.sort(mergeList, new Comparator<HashMap<String, String >>() {
			        	@Override
			        	public int compare(HashMap<String, String> first, HashMap<String, String> second) {
			        		
			        		String sfirst = first.get("time");
			        		
			        		String ssecond = second.get("time");
			        		
			        		return sfirst.compareTo(ssecond);
			        	}
			        });
	        	}
	        	
			} catch (Exception e) {
				 System.out.println(e);
			}
	        
	     // 큐에 담기
	        // queue에 순서대로 담기
	        for(int idx=0; idx < iSize; idx++) { 
	        	fileQ.offer(mergeList.get(idx));
	        }
        	
	        
	    }catch (FileNotFoundException e) {
	        // TODO: handle exception
	    }catch(IOException e){
	        System.out.println(e);
	    }
	}
}
