// 检查是否有目录，有就删除
async function removeExistFile(path) {
	return new Promise((resolve, reject) => {
		plus.io.resolveLocalFileSystemURL(path,
			function(checkEntry) {
				if (checkEntry.isDirectory) {
					checkEntry.removeRecursively()
				} else {
					checkEntry.remove()
				}
				resolve(true)
			},
			function(e) {
				console.log(e.message)
				resolve(false)
			})
	});
}

// 获取无后缀名filename
function getFilenameNoExt(fileName) {
	if (fileName.indexOf('.') === -1) {
		return false
	}
	return fileName.substring(0, fileName.lastIndexOf('.'))
}

// 获取filename
function getFilename(fileName) {
	if (fileName.indexOf('/') === -1) {
		return false
	}
	return fileName.substring(fileName.lastIndexOf('/') + 1)
}

// 获取后缀名
function getSuffix(fileName) {
	if (fileName.indexOf('.') === -1) {
		return false
	}
	return fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length)
}

// 获取后缀名
function getFileDir(fileName) {
	if (fileName.indexOf('/') === -1) {
		return false
	}
	return fileName.substring(0, fileName.lastIndexOf('/') + 1)
}

// 获取文件或目录
async function getFileEntry(path) {
	return new Promise((resolve, reject) => {
		plus.io.resolveLocalFileSystemURL(path, function(entry) {
			resolve(entry)
		}, function(e) {
			console.log(path + ',' + e.message)
			resolve(null)
		});
	})
}

// 获取文件系统
async function getFileSystem(path) {
	return new Promise((resolve, reject) => {
		plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, function(fs) {
			resolve(fs)
		}, function(e) {
			console.log(e.message)
			resolve(null)
		});
	})
}

// 写入文件
async function writeFile(fileName, fileDirEntry, data) {
	return new Promise((resolve, reject) => {
		fileDirEntry.getFile(fileName, {
				create: true
			},
			function(fileEntry) {
				fileEntry.file(function(file) {
					fileEntry.createWriter(function(writer) {
						writer.onwrite = function() {}
						writer.seek(writer.length - 1);
						writer.write(data);
						resolve(file)
					}, function(e) {
						console.log(e.message);
						reject(e.message)
					});
				}, function(e) {
					console.log(e.message);
					reject(e.message)
				});
			},
			function(e) {
				console.log(e.message)
				reject(e.message)
			});
	})
}

// 获取文件Entry
async function getDirFileEntry(path, file) {
	return await getFileEntry(path + '/' + file)
}

// 获取文件或目录
async function getParent(entry) {
	return new Promise((resolve, reject) => {
		entry.getParent(function(entry) {
			resolve(entry)
		}, function(e) {
			console.log(e.message)
			resolve(null)
		});
	})
}

// 创建目录
function createDir(url, dirName) {
	//url值可支持相对路径URL、本地路径URL
	return new Promise((resolve, reject) => {
		plus.io.resolveLocalFileSystemURL(url, function(entry) {
			entry.getDirectory(dirName, {
				create: true,
				exclusive: false
			}, function(dir) {
				resolve(dir)
			}, function(e) {
				console.log(e.message)
				resolve(null)
			});
		}, function(e) {
			console.log(e.message)
			resolve(null)
		});
	})
}

// 创建文件
function createFile(url, fileName) {
	//url值可支持相对路径URL、本地路径URL
	return new Promise((resolve, reject) => {
		plus.io.resolveLocalFileSystemURL(url, function(entry) {
			entry.getFile(fileName, {
				create: true
			}, function(file) {
				resolve(file)
			}, function(e) {
				console.log(e.message)
				resolve(null)
			});
		}, function(e) {
			console.log(e.message)
			resolve(null)
		});
	})
}

// 拷贝目录
async function copyFileDir(srcDirEntry, targetDirEntry, newName) {
	return new Promise((resolve, reject) => {
		let name = srcDirEntry.name
		if (newName) {
			name = newName
		}
		srcDirEntry.copyTo(targetDirEntry, name,
			function(entry) {
				resolve(entry)
			},
			function(e) {
				console.log(e.message)
				resolve(null)
			});
	})
}

// 拷贝文件
async function copyFile(srcFile, targetDirEntry, newName) {
	return new Promise((resolve, reject) => {
		let name = srcFile.name
		if (newName) {
			name = newName
		}
		srcFile.copyTo(targetDirEntry, name,
			function(entry) {
				resolve(entry)
			},
			function(e) {
				console.log(e.message)
				resolve(null)
			});
	})
}

// 移动文件
async function moveFile(srcFile, targetDirEntry, newName) {
	return new Promise((resolve, reject) => {
		let name = srcFile.name
		if (newName) {
			name = newName
		}
		srcFile.moveTo(targetDirEntry, name,
			function(entry) {
				resolve(entry)
			},
			function(e) {
				console.log(e.message)
				resolve(null)
			});
	})
}

// 获取目录所有文件
async function getFiles(dirEntry) {
	return new Promise((resolve, reject) => {
		let dirReader = dirEntry.createReader();
		dirReader.readEntries(
			function(entries) { //历遍子目录即可
				resolve(entries)
			},
			function(e) {
				console.log(e.message)
				resolve([])
			}
		);
	})
}

// 读取文件entry
async function readFile(fileEntry) {
	return new Promise((resolve, reject) => {
		fileEntry.file(function(file) {
			let fileReader = new plus.io.FileReader();
			fileReader.readAsText(file, "utf-8")
			fileReader.onloadend = function(res) {
				resolve(res.target.result)
			};
		}, function(e) {
			console.log(e.message)
			resolve(null)
		});
	})
}

// 解压zip
async function decompressZip(zipFilePath, targetDir) {
	return new Promise((resolve, reject) => {
		plus.zip.decompress(
			zipFilePath,
			targetDir,
			function(res) {
				resolve()
			},
			function(e) {
				console.log(e.message)
				resolve(e)
			}
		)
	})
}

// 压缩成zip
async function zip(targetPath, zipPath) {
	return new Promise((resolve, reject) => {
		plus.zip.compress(
			targetPath,
			zipPath,
			function() {
				resolve(true)
			},
			function(e) {
				console.log(e.message)
				resolve(null)
			}
		)
	})
}

// 获取物理路径
async function getAbsolutePath (url) {
	return new Promise((resolve, reject) => {
		plus.io.convertLocalFileSystemURL(url,
			function(entry) {
				console.log(entry)
				resolve(entry.fullPath)
			},
			function(e) {
				console.log(e.message)
				resolve(null)
			}
		)
	})
}

export default {
	removeExistFile,
	getFileSystem,
	getFilename,
	getFilenameNoExt,
	getSuffix,
	getFileDir,
	getFileEntry,
	getParent,
	createDir,
	createFile,
	writeFile,
	decompressZip,
	zip,
	copyFile,
	copyFileDir,
	moveFile,
	readFile,
	getFiles
};
