package com.hmsoft.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import com.alibaba.fastjson.JSON;
import com.hmsoft.common.BatchConst;
import com.hmsoft.common.FileConst;
import com.hmsoft.model.Batch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 * 
 * @author zq
 *
 */
public class BatchDataUtil {

	private BatchDataUtil() {
		
	}
	
	//load 
	public static ObservableList<Batch> loadBatchData() {
		final ObservableList<Batch> dataList = FXCollections.observableArrayList();
		final File file = new File(FileConst.Batch_DATA_PATH);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			final List<String> list = IOUtils.readLines(new FileInputStream(file), Charset.forName("UTF-8"));
			for (String str : list) {
				if (str == null || str.equals("")) {
					continue;
				}
				Batch batch = JSON.parseObject(str, Batch.class);
				dataList.add(batch);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dataList;
	}
	
	//新增
	public static void insertBatch(final Batch batch) {
		PrintWriter printwriter = null;
		File file = new File(FileConst.Batch_DATA_PATH);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			List<String> oldList = IOUtils.readLines(new FileInputStream(file), Charset.forName("UTF-8"));
			batch.setStatusDes(getStatusCaption(batch.getStatus().byteValue()));
			String jsonBatch = JSON.toJSONString(batch);
			oldList.add(jsonBatch);
			//清空文件,再添加
			printwriter = new PrintWriter(file, "UTF-8");
			for (String json : oldList) {
				if (json == null || json.equals("")) {
					continue;
				}
				printwriter.write(json + FileConst.FILE_NEWLINE);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (printwriter != null) {
				printwriter.flush();
				printwriter.close();
			}
		}
	}
	
	//删除
	public static void deleteBatch(final Long batchId) {
		PrintWriter writer = null;
		File file = new File(FileConst.Batch_DATA_PATH);
		try {
			List<String> oldList = IOUtils.readLines(new FileInputStream(file), Charset.forName("UTF-8"));
			List<String> newList = new ArrayList<String>();
			for (String bat : oldList) {
				Batch batch = JSON.parseObject(bat, Batch.class);
				if (batch.getBatchId().longValue() != batchId.longValue()) {
					newList.add(bat);
				}
			}
			//清空文件,再添加
			writer = new PrintWriter(file, "UTF-8");
			writer.write("");
			for (String json : newList) {
				if (json == null || json.equals("")) {
					continue;
				}
				writer.write(json + FileConst.FILE_NEWLINE);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.flush();
				writer.close();
			}
		}
	}
	
	//修改
	public static void updateBatchStatus(final Long batchId, final byte status) {
		PrintWriter writer = null;
		File file = new File(FileConst.Batch_DATA_PATH);
		try {
			final List<String> oldList = IOUtils.readLines(new FileInputStream(file), Charset.forName("UTF-8"));
			List<String> newList = new ArrayList<String>();
			for (String str : oldList) {
				Batch batch = JSON.parseObject(str, Batch.class);
				if (batch.getBatchId().longValue() == batchId.longValue()) {
					batch.setStatus(new Integer(status));
					batch.setStatusDes(getStatusCaption(status));
					if (status == BatchConst.STATUS_PUBLISHED) {
						batch.setPublishTime(DateUtil.format(LocalDateTime.now()));
					}
					str = JSON.toJSONString(batch);
				}
				newList.add(str);
			}
			//删除之前内容,重新生成新内容
			writer = new PrintWriter(file, "UTF-8");
			writer.write("");
			//new
			for (String json : newList) {
				writer.write(json + FileConst.FILE_NEWLINE);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.flush();
				writer.close();
			}
		}
	}
	
	public static String getStatusCaption(final byte status) {
		String desc = "";
		if (status == 0) {
			desc = "新建";
		} else if (status == 1) {
			desc = "已导入";
		} else if (status == 2) {
			desc = "征集中";
		} else if (status == 3) {
			desc = "已发布";
		}
		
		return desc;
	}
}
