package com.cwbuyer.lib;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.annotation.SuppressLint;
import android.os.Environment;

@SuppressWarnings("unused")
public class ReadFileUtilx {
	// 用於記錄目錄下圖片名稱的字符串數組
	public static String[] FolderPath = null;
	public static String Curriculum = null;
	public static String CurriculumPath = null;
	/**
	 *	圖片List
	 */
	public static List<String> getImagesFromSDx(String folder) {
		List<String> imageList = new ArrayList<String>();
		List<String> imageListSort = new ArrayList<String>();

		String sdcard = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();

		File f = new File(folder);
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
		{

		}
		else
		{
			return imageList;
		}
		Curriculum = folder;
		File[] files = f.listFiles();

		if(files == null || files.length == 0)
			return imageList;

		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			if (isImageFile(file.getPath())){
				if(file.getPath().indexOf("cover") < 0){
					imageListSort.add(file.getPath());
				}
			}
		}
		
		String [] Listimage = new String[imageListSort.size()];
		for (int i = 0; i < imageListSort.size(); i++){
			Listimage[i] = imageListSort.get(i);
		}
		
		Arrays.sort(Listimage);
		FolderPath = Listimage;
		/**
		 * 將所有圖像檔的路徑存入ArrayList列表
		 */		
		for (int i = 0; i < Listimage.length; i++){
			imageList.add(Listimage[i]);
		}		
		return imageList;
	}


	/**
	 * 	判斷是否為圖像檔
	 */
	@SuppressLint("DefaultLocale")
	public static boolean isImageFile(String fName) {
		boolean re;
		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();
		/**
		 * 依據檔副檔名判斷是否為圖像檔
		 */
		if (end.equals("jpg") || end.equals("gif") || end.equals("png")
				|| end.equals("jpeg") || end.equals("bmp")) {
			re = true;
		} else {
			re = false;
		}
		return re;
	}
	
	// 獲得資料夾路徑
	public String[] getFolderPath() {
		return FolderPath;
	}
	//獲得書名資料夾
	public String getCurriculum() {
		return Curriculum;
	}
}
