package com.yonge.toolset.log.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yonge.toolset.log.dal.dao.HistoryMessageDao;
import com.yonge.toolset.log.dal.model.HistoryMessage;
import com.yonge.toolset.log.service.HistoryMessageService;
import com.yonge.toolset.mongodb.dao.BaseDaoWithMongo;
import com.yonge.toolset.mongodb.service.impl.BaseServiceImplWithMongo;

@Service
public class HistoryMessageServiceImpl extends BaseServiceImplWithMongo<String, HistoryMessage> implements HistoryMessageService {

	@Autowired
	private HistoryMessageDao historyMessageDao;

	@Override
	public BaseDaoWithMongo<String, HistoryMessage> getDAO() {
		return historyMessageDao;
	}

	@Override
	public void saveImHistoryMessage(File file) throws Exception {
//		long length = file.length();
//		if(length > 1048576){
			doNioReadFile(file);
//		}else {
//			doBufferReadFile(file);
//		}
	}

//	public static void main(String[] args) throws Exception {
//		doNioReadFile1(new File("/Users/chenxiaoyu/Documents/77fe9ce6-7d91-4568-afe3-9e8ac351e87f.zip"));
//	}

	public void doNioReadFile(File file) {
		ZipInputStream zin = null;
		try {
			zin = new ZipInputStream(new FileInputStream(file), StandardCharsets.UTF_8);
			ZipFile zf = new ZipFile(file);
			ZipEntry ze;
			Set<HistoryMessage> historyMessages = new HashSet<>();
			while ((ze = zin.getNextEntry()) != null) {
				BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
				String line;
				while ((line = br.readLine()) != null) {
					try {
						historyMessages.add(JSONObject.parseObject(line.substring(line.indexOf("{")), HistoryMessage.class));
//						if(historyMessages.size() >= 2000){
//							historyMessageDao.batchInsert(new ArrayList<>(historyMessages),HistoryMessage.class);
//							historyMessages.clear();
//						}
					}catch (Exception e){
						e.printStackTrace();
					}
				}
				br.close();
				break;
			}
			if(historyMessages.size() > 0){
				historyMessageDao.batchInsert(historyMessages.stream().sorted(Comparator.comparing(HistoryMessage::getDateTime)).collect(Collectors.toList()),HistoryMessage.class);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (zin != null) {
				try {
					zin.closeEntry();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

//	public void doBufferReadFile(File file) throws Exception
//	{
//		BufferedReader reader = null;
//		try {
//
////			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
//			reader = new BufferedReader(new FileReader(file));
//			String line = null;
//			int num = 0;
//			List<HistoryMessage> historyMessages = new ArrayList<>();
//			while ((line = reader.readLine()) != null){
//				try {
//					historyMessages.add(JSONObject.parseObject(line.substring(line.indexOf("{")), HistoryMessage.class));
//					if(historyMessages.size() >= 2000){
//						historyMessageDao.batchInsert(historyMessages,HistoryMessage.class);
//						historyMessages.clear();
//					}
//				}catch (Exception e){
//					e.printStackTrace();
//				}
//				num++;
//			}
//			if(historyMessages.size() > 0){
//				historyMessageDao.batchInsert(historyMessages,HistoryMessage.class);
//			}
//		} finally {
//			reader.close();
//		}
//	}


//	public static void doNioReadFile(File file) throws Exception
//	{
//		String enterStr = "\n";
//
//		FileChannel inChannel = new FileInputStream(file).getChannel();
//		ByteBuffer buffer = ByteBuffer.allocate(1048576);
//
//		StringBuilder newlinesBui = new StringBuilder();
//		while (inChannel.read(buffer) != -1)
//		{
//			buffer.flip();
//			//数据组合.
//			String content = new String(buffer.array());
//			newlinesBui.append(content).toString();
//
//			int fromIndex = 0;
//			int endIndex = -1;
//			//循环找到 \n
//			List<HistoryMessage> historyMessages = new ArrayList<>();
//			while ((endIndex = newlinesBui.indexOf(enterStr, fromIndex)) > -1)
//			{
//				//得到一行
//				String line = newlinesBui.substring(fromIndex, endIndex);
//				try {
//					historyMessages.add(JSONObject.parseObject(line.substring(line.indexOf("{")), HistoryMessage.class));
//					if(historyMessages.size() >= 2000){
////						historyMessageDao.batchInsert(historyMessages,HistoryMessage.class);
//						historyMessages.clear();
//					}
//				}catch (Exception e){
//					e.printStackTrace();
//				}
//				fromIndex = endIndex + 1;
//			}
//			if(historyMessages.size() > 0){
////				historyMessageDao.batchInsert(historyMessages,HistoryMessage.class);
//			}
//			newlinesBui.delete(0, fromIndex);
//			buffer.clear();
//		}
//	}
}
