//
// Core Program for TETDM 
// FILEIO.java Version 1.00
// Copyright(C) 2011-2018 TETDM CORE MEMBER ALL RIGHTS RESERVED. 
// This program is provided under the license.
//


package source;

import java.io.*;
import java.text.*;

public class FILEIO
{
    //rename can change a name of file or folder
    public static void rename(String filename1, String filename2)
    {
        File oldFile = new File(filename1);
        File newFile = new File(filename2);
        
        if(oldFile.exists())
        {
            if(newFile.exists())
            {
                System.out.println("Folder already exists: "+filename2);
            }
            else
            {
                oldFile.renameTo(newFile);
                System.out.println("RENAME: "+filename1+" to "+filename2);
            }
        }
        else
        {
            System.out.println("File not found: "+filename1);
        }
    }
    
    //rename can change a name of file or folder
    public static void createFolder(String filename1)
    {
        File createFile = new File(filename1);
        
        if(createFile.exists())
        {
            System.out.println("Folder already exists: "+filename1);
        }
        else
        {
            createFile.mkdir();
            System.out.println("Create folder: "+filename1);
        }
    }
    
    public static void deleteFolder(String folderName, boolean contentsOnly)  //not recursively
    {
        File deleteFolder = new File(folderName);

        if(!deleteFolder.exists())
        {
            System.out.println("No Folder exists: "+folderName);
            return;
        }
        
        File files[] = deleteFolder.listFiles();
        
        for(int i=0;i<files.length;i++)
            files[i].delete();
        
        if(contentsOnly)
            return;
        else
            deleteFolder.delete();
    }
    
    public static boolean copyFolder(String folderName1, String folderName2)  //not recursively
    {
        File fromFolder = new File(folderName1);
        File toFolder = new File(folderName2);
        
        if(!fromFolder.exists())
        {
            System.out.println("No Folder exists: "+folderName1);
            return false;
        }
        
        if(toFolder.exists())
        {
            System.out.println("Folder exists: "+folderName2);
            return false;
        }
        
        createFolder(folderName2);
        
        File files[] = fromFolder.listFiles();

        String eachFilename;
        for(int i=0;i<files.length;i++)
        {
            eachFilename = files[i].getName();
            EXECUTE.filecopy(folderName1+File.separator+eachFilename, folderName2+File.separator+eachFilename);
        }
        return true;
    }
    
    
	public static int line_count(File filename)		//Count lines
	{
		String ss;
		int number = 0;

		try
		{
			BufferedReader br1 = new BufferedReader(new FileReader(filename));	//Count lines
			while((ss = br1.readLine()) != null)
				number++;
			br1.close();
		}
		catch(Exception e){
			System.out.println("File Read ERROR : Number count");
		}
		return number;
	}

	public static void TextFileRead(File filename, String cut_text[], int max_num)		//Textfile, read lines
	{
		try
		{
			BufferedReader br1 = new BufferedReader(new FileReader(filename));
			for(int i=0;i<max_num;i++)
				cut_text[i] = br1.readLine();
			br1.close();
		}
		catch(Exception e){
			System.out.println("File Reading ERROR Text data");
		}
	}

	public static void TextFileReadCode(File filename, String cut_text[], int max_num)		//Textfile, read lines
	{
		try
		{
			BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "JISAutoDetect"));
			for(int i=0;i<max_num;i++)
				cut_text[i] = br1.readLine();
			br1.close();
		}
		catch(Exception e){
			System.out.println("File Reading ERROR Text data");
		}
	}

	public static void TextFileLineRead(File filename, String cut_text)		//Textfile, read one line only
	{
		try
		{
			BufferedReader br1 = new BufferedReader(new FileReader(filename));
			cut_text = br1.readLine();
			br1.close();
		}
		catch(Exception e){
			System.out.println("File Reading ERROR Text data");
		}
	}

	public static String TextFileAllRead(File filename)		//Textfile, read all
	{
		String ss,text="";
		try
		{
			BufferedReader br1 = new BufferedReader(new FileReader(filename));

			StringWriter sw1 = new StringWriter();
			BufferedWriter bw1 = new BufferedWriter(sw1);
			while((ss = br1.readLine()) != null)
			{
				bw1.write(ss);
				bw1.newLine();
			}
			br1.close();
			bw1.flush();
			sw1.flush();
			text = sw1.toString();
			bw1.close();
			sw1.close();
		}
		catch(Exception e){
			System.out.println("File Reading ERROR Text data");
		}
		return text;
	}

	public static String TextFileAllReadCode(File filename)		//Textfile, read all(Recommend to use)
	{
		String ss,text="";
		
		if (!filename.exists())
		{
			System.out.println("File Not FOUND [ "+filename.getName()+" ] (to be created if needed)");
			return text;
		}
		
		try
		{
			BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "JISAutoDetect"));
			StringWriter sw1 = new StringWriter();
			BufferedWriter bw1 = new BufferedWriter(sw1);
			while((ss = br1.readLine()) != null)
			{
				bw1.write(ss);
				bw1.newLine();
			}
			br1.close();
			bw1.flush();
			sw1.flush();
			text = sw1.toString();
			bw1.close();
			sw1.close();
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR [ "+filename.getName()+" ]");
		}
		return text;
	}
    
    public static String TextFileAllReadUtf8(File filename)        //Textfile, read all(Recommend to use)
    {
        String ss,text="";
        
        if (!filename.exists())
        {
            System.out.println("File Not FOUND [ "+filename.getName()+" ] (to be created if needed)");
            return text;
        }
        
        try
        {
            BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
            StringWriter sw1 = new StringWriter();
            BufferedWriter bw1 = new BufferedWriter(sw1);
            while((ss = br1.readLine()) != null)
            {
                bw1.write(ss);
                bw1.newLine();
            }
            br1.close();
            bw1.flush();
            sw1.flush();
            text = sw1.toString();
            bw1.close();
            sw1.close();
        }
        catch(Exception e){
            System.out.println("Text Reading ERROR [ "+filename.getName()+" ]");
        }
        return text;
    }
	

	
	public static String[] TextFileAllReadCodeArray(File filename)		//Textfile, read all(Recommend to use)
	{
        String dataStrings[];
        
		String ss;		
		int number = 0;
        
        if(!filename.exists())
        {
            System.out.println("FILE NOT EXIST: "+filename.getName());
            dataStrings = new String[0];
            return dataStrings;
        }
        
		try
		{
			BufferedReader br1 = new BufferedReader(new FileReader(filename));	//Count lines
			while((ss = br1.readLine()) != null)
				number++;
			br1.close();
		}
		catch(Exception e){
			System.out.println("File Read ERROR : Number count");
		}	
		
		dataStrings = new String[number];
		
		number = 0;
		try
		{
			BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "JISAutoDetect"));

			while((ss = br1.readLine()) != null)
				dataStrings[number++] = ss;

			br1.close();
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR "+filename.getName()+" data");
		}
		return dataStrings;
	}
    
    public static String[] TextFileAllReadCodeArray(File filename, String encoding)        //Textfile, read all(Recommend to use)
    {
        String ss;
        int number = 0;
        
        try
        {
            BufferedReader br1 = new BufferedReader(new FileReader(filename));    //Count lines
            while((ss = br1.readLine()) != null)
                number++;
            br1.close();
        }
        catch(Exception e){
            System.out.println("File Read ERROR : Number count");
        }
        
        String dataStrings[] = new String[number];
        
        number = 0;
        try
        {
            BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding));
            
            while((ss = br1.readLine()) != null)
                dataStrings[number++] = ss;
            
            br1.close();
        }
        catch(Exception e){
            System.out.println("Text Reading ERROR "+filename.getName()+" data");
        }
        return dataStrings;
    }

    public static String[][] CsvFileRead(File filename)  //FOR CSV FILE
    {
        String ss;
        int number = 0;
        
        try
        {
            BufferedReader br1 = new BufferedReader(new FileReader(filename));    //Count lines
            
            while((ss = br1.readLine()) != null)
                number++;
            br1.close();
        }
        catch(Exception e){
            System.out.println("File Read ERROR : Number count");
        }
        
        String dataStrings[][] = new String[number][];
        
        number = 0;
        try
        {
            BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "JISAutoDetect"));
            
            while((ss = br1.readLine()) != null)
                dataStrings[number++] = ss.split(",");
            
            br1.close();
        }
        catch(Exception e){
            System.out.println("Text Reading ERROR "+filename.getName()+" data");
        }
        return dataStrings;
    }
    
    public static String[] CsvFileRead(File filename, int column)  //FOR CSV FILE
    {
        String ss;
        int number = 0;
        
        try
        {
            BufferedReader br1 = new BufferedReader(new FileReader(filename));    //Count lines
            
            while((ss = br1.readLine()) != null)
                number++;
            br1.close();
        }
        catch(Exception e){
            System.out.println("File Read ERROR : Number count");
        }
        
        String dataStrings[][] = new String[number][];
        int count = 0;
        
        number = 0;
        try
        {
            BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "JISAutoDetect"));
            
            while((ss = br1.readLine()) != null)
                dataStrings[number++] = ss.split(",");

            for(int i=0;i<number;i++)
                if(dataStrings[i].length > column)
                    count++;
            
            br1.close();
        }
        catch(Exception e){
            System.out.println("Text Reading ERROR "+filename.getName()+" data");
        }
        
        String dataStr[] = new String[count];
        count=0;
        for(int i=0;i<number;i++)
            if(dataStrings[i].length > column)
                dataStr[count++] = dataStrings[i][column];
        
        return dataStr;
    }
    
	public static void TextFileWrite(File filename, String cut_text[], int max_num)		//Textfile, write lines
	{
		try
		{
			BufferedWriter bw1 = new BufferedWriter(new FileWriter(filename));
			for(int i=0;i<max_num;i++)
			{
				bw1.write(cut_text[i]);
				bw1.newLine();
			}
			bw1.close();
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
	}

	public static void TextFileWrite2(File filename, String cut_text)		//Textfile, write all
	{
		try
		{
			BufferedWriter bw1 = new BufferedWriter(new FileWriter(filename));

			bw1.write(cut_text);
			bw1.close();
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
	}

	public static void TextFileWriteCode(File filename, String text, String encoding)		//Textfile, write all
	{
		try
		{
			BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), encoding));
			//Output byte stream

			bw1.write(text);
			bw1.close();
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
	}
	
	public static void TextFileWriteCode2(File filename, String text, String encoding)		//Textfile, write all, add mode
	{
		try
		{
			BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename, true), encoding));
			//Output byte stream
			
			bw1.write(text);
			bw1.close();
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
	}
    
    public static void TextFileWriteCode3(File filename, String cut_text[], int max_num, String encoding)		//Textfile, write all
    {
        try
        {
            BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), encoding));
            //Output byte stream
            
            for(int i=0;i<max_num;i++)
            {
                bw1.write(cut_text[i]);
                bw1.newLine();
            }
            bw1.close();
        }
        catch(Exception e){
            System.out.println("File Writing ERROR Text data");
        }
    }

	//// Followings are text processing libraries

	public static int CountTags(String text, String tag)		//Count number of lines including tags
	{
		int n=0;
		int index;
		
		String ss;
		try
		{
			BufferedReader br1 = new BufferedReader(new StringReader(text));

			while((ss = br1.readLine()) != null)
			{
				index = -1;
				while((index = ss.indexOf(tag,index+1)) >= 0)
				{
					n++;
				}
			}
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR "+text+" data");
		}
		return n+1;
	}
/*
	public static String CutTags(String text, String tag)		//delete lines including tags
	{
		String ss,text2="";
		try
		{
			BufferedReader br1 = new BufferedReader(new StringReader(text));
			StringWriter sw1 = new StringWriter();
			BufferedWriter bw1 = new BufferedWriter(sw1);
			while((ss = br1.readLine()) != null)
			{
				if(ss.indexOf(tag) == -1)
					bw1.write(ss);
				bw1.newLine();
			}
			br1.close();
			bw1.flush();
			sw1.flush();
			text2 = sw1.toString();
			bw1.close();
			sw1.close();
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR Text data");
		}
		return text2;
	}
*/
	public static int CutText(String text, String cut_text[], String tag, boolean leftTag)		//divide a text by tag(line including a tag)//return number of tags
	{
		BufferedReader br1 = new BufferedReader(new StringReader(text));
		StringWriter sw1 = new StringWriter();
		BufferedWriter bw1 = new BufferedWriter(sw1);		
		
		int n=0;
		int index;
		String ss="";
		String subString;
		boolean reading;
		
		try
		{
			reading = true;
			while(true)
			{
				if(reading)
					ss = br1.readLine();
				
				reading = true;
				
				if(ss == null)
					break;
				
				index = ss.indexOf(tag);
				if(index == -1)
				{
					bw1.write(ss);
					bw1.newLine();
				}
				else
				{
					subString = ss.substring(0,index);
					bw1.write(subString);
					bw1.flush();
					cut_text[n++] = sw1.toString();
					sw1.getBuffer().delete(0,sw1.getBuffer().length());
					
                    if(leftTag == true)
                        bw1.write(tag);

                    subString = ss.substring(index+tag.length(),ss.length());
					
//					System.out.println("substlength="+subString.length());
					
					if(subString.length() > 0)
					{
						ss = subString;
						reading = false;
					}
				}
				
				
			}
			bw1.flush();
			bw1.close();
			cut_text[n++] = sw1.toString();
			br1.close();
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR Text data");
		}
		return n;
	}

	public static String print_double(double num)
	{
		DecimalFormat df = new DecimalFormat();
		df.applyLocalizedPattern("#####.##");

		return df.format(num);
	}

	public static String print_pattern(double num, String pat)
	{
		DecimalFormat df = new DecimalFormat();
		df.applyLocalizedPattern(pat);

		return df.format(num);
	}
	
	/* COPYED FROM ModuleData except for filename matching */	
	static String[] check_dir(File dir)
	{
		File dir_list[] = dir.listFiles();
		String filename_list[];
		String filename;
		
		int count=0;
		for(int i=0;i<dir_list.length;i++)
			if(dir_list[i].getName().matches("^[A-Za-z0-9][A-Za-z0-9_\\.]+"))			
				count++;
		
		filename_list = new String[count];
		
		count=0;		
		for(int i=0;i<dir_list.length;i++)
		{
			filename = dir_list[i].getName();
			
			if(filename.matches("^[A-Za-z0-9][A-Za-z0-9_\\.]+"))
			{
				filename_list[count] = filename; //filename.substring(0,filename.length());
				count++;
			}
		}
		
		return filename_list;
	}	
	
	public static String makeOne(File fileDirectory, String absolutePath, String defaultCode, String tag, boolean utf8code)
	{
		String fileNames[];
		fileNames = check_dir(fileDirectory);
		
		StringWriter sw = new StringWriter();			//For words
		BufferedWriter bw = new BufferedWriter(sw);
		
		if(fileNames.length == 0)
		{
			System.out.println("No files in directory "+fileDirectory.getName());
			System.exit(0);			
		}
		
		try{		
			for(int i=0;i<fileNames.length;i++)
			{
                if(utf8code)
                    bw.write(TextFileAllReadUtf8(new File(fileDirectory + File.separator + fileNames[i])));
                else
                    bw.write(TextFileAllReadCode(new File(fileDirectory + File.separator + fileNames[i])));

				if(i != fileNames.length - 1)
				{
					bw.newLine();
					bw.newLine();
					bw.write(tag);
					bw.newLine();
					bw.newLine();
				}
			}
			bw.flush();
			bw.close();
		}
		catch(Exception e){
			System.out.println("writing ERROR in FILEIO.java");
		}
		
		TextFileWriteCode(new File(absolutePath + "text" + File.separator + fileDirectory.getName()+".txt"), sw.toString(), defaultCode);
		
		return absolutePath + "text" + File.separator + fileDirectory.getName()+".txt";
	}
	
	public static void deleteFile(String filename)	
	{
		File file = new File(filename);
		
		if(file.exists()){
			if(file.delete()){
				System.out.println("Delete File : "+filename);
			}else{
				System.out.println("Unable to delete File : "+filename);
			}
		}else{
			System.out.println("File not found : "+filename);
		}
	}
    
	public static void deleteFile(File file)
	{
		if(file.exists()){
			if(file.delete()){
				System.out.println("Delete File : "+file.getName());
			}else{
				System.out.println("Unable to delete File : "+file.getName());
			}
		}else{
			System.out.println("File not found : "+file.getName());
		}
	}
	
}
