package ITUannotation;
import java.awt.BorderLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.CharsetDecoder;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileDemonstration extends JFrame{
	private JTextArea outputArea; // used for output
	private JScrollPane scrollPane; // used to provide scrolling to output
	private String fileType;
	
	 // set up GUI
	 public FileDemonstration()
	 {
		 super( "Select input .txt File" );
		 outputArea = new JTextArea();
		 scrollPane = new JScrollPane(outputArea);
		 add( scrollPane, BorderLayout.CENTER ); // add scrollPane to GUI
		 setSize( 400, 400 ); // set GUI size 75
		 
	  } // end FileDemonstration constructor

	 
	  public FileDemonstration(String type) {
		  fileType=type;
		
	}


	// allow user to specify file name
	  public File getFile(String lastdirectory,String[] fileType) throws IOException
	  {
	  // display file dialog, so user can choose file to open
		  JFileChooser fileChooser = new JFileChooser(lastdirectory);
		  
		  fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		  String description="*."+fileType[0];		  
		  for(int d=1;d<fileType.length;d++){			  
			description=description+"; *."+fileType[d];
		  }
		  FileNameExtensionFilter filter = new FileNameExtensionFilter(description, fileType/*new String[]{"txt"}*/);
		  fileChooser.setFileFilter(filter);
		  
		  // fileChooser.setFileFilter(Fil)
		  
		  File fileToOpen=null;
		  fileChooser.setAcceptAllFileFilterUsed(false);
			 
		  int option = fileChooser.showOpenDialog(this);//.showSaveDialog(this);
		  
		  if(option == JFileChooser.APPROVE_OPTION)
		  {    
//			  if(fileChooser.getSelectedFile().getAbsolutePath().endsWith("."+fileType))
				  fileToOpen = new File(fileChooser.getSelectedFile().getAbsolutePath());
	//			else
		//			fileToOpen = new File(fileChooser.getSelectedFile().getAbsolutePath() + "."+fileType);
			 
			  if(!fileToOpen.exists())
			  {
				  fileToOpen= new File(fileToOpen.getAbsolutePath());
				
			  }
			  else
			  {
				  InputStreamReader r = new InputStreamReader(new FileInputStream(fileToOpen));
				  
				  
				 // r.
				//  CharsetDecoder cd = new CharsetDecoder();
				  System.out.println(r.getEncoding());
			  }
		  }
		  else if(option==fileChooser.CANCEL_OPTION)
		  { 
			  //System.exit( 1 );
			  }
		  return fileToOpen;
		  
	  }
		  
		  
		  
	/*	  int result = fileChooser.showOpenDialog( this );
	 File fileName=null;
	  // if user clicked Cancel button on dialog, return
	  if ( result == JFileChooser.CANCEL_OPTION )
	 // System.exit( 1 );
		  fileChooser.setVisible(false);
	  if ( result == JFileChooser.APPROVE_OPTION ){
	   fileName = fileChooser.getSelectedFile(); // get selected file
	  }
	 
	  // display error if invalid
	  if ( ( fileName == null ) || ( fileName.getName().equals( "" ) ) )
	  {
		 // System.exit( 1 );
	  } // end if
	  return fileName; 
	 
	 }*/
	  public File save(String cpath,String fileType)
	  {
		  File theFileToSave=null;
		  JFileChooser fileChooser = new JFileChooser(cpath);
		  FileNameExtensionFilter filter = new FileNameExtensionFilter("*."+fileType, fileType);
		  this.setTitle("Kaydet");
		  
		  fileChooser.setFileFilter(filter);		    
		  fileChooser.setAcceptAllFileFilterUsed(false);
		 
		  int option = fileChooser.showSaveDialog(this);
		  
		  if(option == JFileChooser.APPROVE_OPTION)
		  {    
//			  File f = new File("filename");
//			  myJFileChooser.setSelectedFile(f);
			  if(fileChooser.getSelectedFile().getAbsolutePath().endsWith("."+fileType))
					theFileToSave = new File(fileChooser.getSelectedFile().getAbsolutePath());
				else
					theFileToSave = new File(fileChooser.getSelectedFile().getAbsolutePath() + "."+fileType);
			 
			  if(!theFileToSave.exists())
			  {
				  theFileToSave= new File(theFileToSave.getAbsolutePath());
			  }
		  }
		  else if(option==fileChooser.CANCEL_OPTION)
		  { 
			  //System.exit( 1 );
			  }
		  return theFileToSave;
		  
	  }


	public File getFile(String absolutePath, String string) throws IOException {
		String[] ext=new String[1];
		ext[0]=string;
		return getFile(absolutePath, ext);
	}

}
