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.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import com.alibaba.fastjson.JSON;
import com.hmsoft.common.FileConst;
import com.hmsoft.model.CoursePlan;
import com.hmsoft.model.School;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

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

	private CoursePlanDataUtil() {
		
	}
	
	//从指定文件中加载征集计划.
	public static ObservableList<CoursePlan> loadCoursePlanData(final Long batchId) {
		final ObservableList<CoursePlan> dataList = FXCollections.observableArrayList();
		final File file = new File(FileConst.COURSEPLAN_DATA_PATH + "-" + batchId);
		final List<School> schoolList = ShoolDateUtil.loadSchoolData();
		try {
			if (file.exists()) {
				final List<String> list = IOUtils.readLines(new FileInputStream(file), Charset.forName("UTF-8"));
				for (String str : list) {
					CoursePlan courseplan = JSON.parseObject(str, CoursePlan.class);
					courseplan.getComboBox().getItems().clear();
					courseplan.getComboBox().getItems().addAll(schoolList);
					//设置下拉框之前的选中值，比如发布状态
					if (courseplan.getSchoolId() != null && courseplan.getSchoolId().longValue() > 0) {
						courseplan.getComboBox().getSelectionModel().select(new School(courseplan.getSchoolId(), courseplan.getSchoolName()));
					}
					courseplan.setConfirmDesc(courseplan.getConfirmStatus() == 1 ? "已确认" : "否");
					dataList.add(courseplan);
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dataList;
	}
	
	//导入写到文件中.
	public static void writeCoursePlan(final List<CoursePlan> coursePlanList, final Long batchId) {
		PrintWriter writer = null;
		File destFile = new File(FileConst.COURSEPLAN_DATA_PATH + "-" + batchId);
		try {
			writer = new PrintWriter(destFile, "UTF-8");
			for (CoursePlan obj : coursePlanList) {
				String json = JSON.toJSONString(obj);
				writer.write(json + FileConst.FILE_NEWLINE);
			}
		} catch (FileNotFoundException | UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.flush();
				writer.close();
			}
		}
	}
	
	//清空之前内容,用新列表内容修改课程计划文件
	public static void updateCoursePlan(final List<CoursePlan> coursePlanList, final Long batchId) {
		PrintWriter writer = null;
		File file = new File(FileConst.COURSEPLAN_DATA_PATH + "-" + batchId);
		try {
			List<String> newList = new ArrayList<String>();
			for (CoursePlan course : coursePlanList) {
				String json = JSON.toJSONString(course);
				newList.add(json);
			}
			//删除之前内容,重新生成新内容
			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();
			}
		}
	}
	
}
