package com.njcn.pojo.abstractClass;


import com.njcn.pojo.file.FileProperty;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

import java.io.*;

/**
 * @Author: Sunwei 【sunW2016@163.com】
 * @Description:
 * @Date: Create in 19:59 2018/3/5
 * @Modified By:
 */
public abstract class AbstractFile<T> {
	/**
	 * @文件信息对象
	 */
	private FileProperty fileProperty;
	private File file;
	private SmbFile smbFile;
	private FileReader fileReader;
	private BufferedReader bufferedReader;
	private FileWriter fileWriter;
	private BufferedWriter bufferedWriter;
	private SmbFileInputStream in;
	private InputStreamReader read;
	private SmbFileOutputStream out;
	private OutputStreamWriter writer;

	/**
	 * 标识iFlag： 1、网络共享文件 2、本地文件
	 * 
	 * @param fileProperty
	 */
	public final void setFileProperty(FileProperty fileProperty) throws IOException {
		this.fileProperty = fileProperty;

		if (1 == fileProperty.getFileType().intValue()) {
			String fileName = fileProperty.getFilePath() + fileProperty.getFileName() + fileProperty.getFileSuffix();
			fileName = fileName.replace('\\', '/');
			smbFile = new SmbFile(fileName, fileProperty.getAuth());

			if (isExitFile()) {
				in = new SmbFileInputStream(smbFile);
				read = new InputStreamReader(in);
				bufferedReader = new BufferedReader(read);
			}
		} else {
			file = new File(fileProperty.getFilePath(), fileProperty.getFileName() + fileProperty.getFileSuffix());

			if (isExitFile()) {
				fileReader = new FileReader(file);
				bufferedReader = new BufferedReader(fileReader);
			}
		}
	}

	/**
	 * 判断文件是否存在
	 * 
	 * @return 文件存在返回TRUE，不存在返回FALSE
	 * @throws SmbException
	 */
	public final Boolean isExitFile() throws SmbException {
		if (1 == fileProperty.getFileType().intValue()) {
			return (smbFile.isFile() || smbFile.exists());
		} else {
			return (file.exists() || file.isFile());
		}
	}

	/**
	 * 读取文件内容
	 * 
	 * @return 返回string对象
	 */
	public final String readFile() throws IOException {
		StringBuilder stringBuilder = new StringBuilder("");
		String strReadLine = null;

		while ((strReadLine = bufferedReader.readLine()) != null) {
			stringBuilder.append(strReadLine);
		}

		if (1 == fileProperty.getFileType().intValue()) {
			in.close();
			read.close();
		} else {
			fileReader.close();
		}

		bufferedReader.close();

		return stringBuilder.toString();
	}

	/**
	 * 向文件写入内容 判断文件是否存在，不存在创建新的文件
	 * 
	 * @param strBuffer
	 */
	public final void writeFile(String strBuffer) throws IOException {
		if (1 == fileProperty.getFileType().intValue()) {
			if (isExitFile()) {
				in.close();
				read.close();
				bufferedReader.close();
				smbFile.delete();
			}
			
			smbFile.createNewFile();
			out = new SmbFileOutputStream(smbFile);
			writer = new OutputStreamWriter(out);
			bufferedWriter = new BufferedWriter(writer);
		} else {
			if (isExitFile()) {
				fileReader.close();
				bufferedReader.close();
				file.delete();
			}
			
			file.createNewFile();
			fileWriter = new FileWriter(file,false);
			bufferedWriter = new BufferedWriter(fileWriter);
		}

		bufferedWriter.write(strBuffer);
		bufferedWriter.flush();

		if (1 == fileProperty.getFileType().intValue()) {
			out.close();
			writer.close();
		} else {
			fileWriter.close();
		}

		bufferedWriter.close();
	}

	/**
	 * 获取的文件内容转为T类型的数据(json、xml...) 子类如不需要转换类型，直接返回string即可 是否必须重写该方法
	 *
	 * @param strBuffer
	 * @return
	 */
	public abstract T string2T(String strBuffer);

}
