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.bizpanel.SchoolTab;
import com.hmsoft.common.FileConst;
import com.hmsoft.model.School;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

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

	private ShoolDateUtil() {
		
	}
	
	//load 
	public static ObservableList<School> loadSchoolData() {
		final ObservableList<School> dataList = FXCollections.observableArrayList();
		final File file = new File(FileConst.SCHOOL_DATA_PATH);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			final List<String> list = IOUtils.readLines(new FileInputStream(file), Charset.forName("UTF-8"));
			int j=0;
			for (String schoolStr : list) {
				School school = JSON.parseObject(schoolStr, School.class);
				int i = ++j;
			    school.setId(Long.valueOf(i));	
				dataList.add(school);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dataList;
	}

	public static void insertSchool(School school) {
		PrintWriter printwriter = null;
		File file = new File(FileConst.SCHOOL_DATA_PATH);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			List<String> oldList = IOUtils.readLines(new FileInputStream(file), Charset.forName("UTF-8"));
			String jsonSchool = JSON.toJSONString(school);
			oldList.add(jsonSchool);
			//清空文件,再添加
			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 deleteSchool(Long schoolId) {
		PrintWriter writer = null;
		File file = new File(FileConst.SCHOOL_DATA_PATH);
		try {
			List<String> oldList = IOUtils.readLines(new FileInputStream(file), Charset.forName("UTF-8"));
			List<String> newList = new ArrayList<String>();
			for (String sho : oldList) {
				School school = JSON.parseObject(sho, School.class);
				if (school.getSchoolId().longValue() != schoolId.longValue()) {
					newList.add(sho);
				}
			}
			//清空文件内容
			writer = new PrintWriter(file, "UTF-8");
			writer.write("");
			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 void resetPasswd(final Long schoolId, final String passwd) {
		PrintWriter writer = null;
		File file = new File(FileConst.SCHOOL_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) {
				School school = JSON.parseObject(str, School.class);
				if (school.getSchoolId().longValue() == schoolId.longValue()) {
					school.setPassWord(passwd);
					str = JSON.toJSONString(school);
				}
				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 List<School> getAllSchoolData() {
		return SchoolTab.DATALIST;
	}
}
