package com.example.interface01;


import android.os.Bundle;
import android.os.Environment;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
import android.util.Log;


import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_about:
			// Comportement du bouton "A Propos"
			return true;
		case R.id.menu_help:
			// Comportement du bouton "Aide"
			MediaPlayer mediaPlayer;
			mediaPlayer = new MediaPlayer();
			try {
				mediaPlayer.setDataSource("http://www.analog-design.net/ALARME/Videos/enigma.mp3");
				 mediaPlayer.prepare();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
           
            mediaPlayer.start();
            
			
		//Intent playerIntent = new Intent(this,player.class);
        //startActivity(playerIntent);
			return true;
		case R.id.menu_refresh:
			// Comportement du bouton "Rafraichir"
			return true;
		case R.id.menu_search:
			// Comportement du bouton "Recherche"
			return true;
		case R.id.menu_settings:
			// Comportement du bouton "Paramtres"
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

} 

//<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

//android.permission.WRITE_EXTERNAL_STORAGE
//android.permission.READ_EXTERNAL_STORAGE
//https://www.learn2crack.com/2014/04/android-read-write-file.html

//String newFolder = "/myFolder2";
//String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
//File myNewFolder = new File(extStorageDirectory + newFolder);
//myNewFolder.mkdir();

class FileOperations 
{
    public FileOperations() 
    {

    }
    
   // File file = new File(selectedFilePath);
   // boolean deleted = file.delete();
    
   // File dir = getFilesDir();
   // File file = new File(dir, "my_filename");
   // boolean deleted = file.delete();

    
    //File file = new File(myFilePath);
    //boolean deleted = file.delete();
    //Log.v("log_tag","deleted: " + deleted);
    
    public boolean DeleteFile(int filetype)
    {
        File f = new File("myfile.txt");
        boolean ans= f.delete();
         return ans;
    }
    
    
    public void delete_File(String path, String name )
    {
    	File sdCard = Environment.getExternalStorageDirectory();   
		String path_complet = sdCard.getAbsolutePath() + path ;
		File f = new File(path_complet +"myfile.txt");
		f.delete();
    	
    	
    }
    
    
    //deleteFiles("/sdcard/uploads/");
    public void deleteFiles(String path)
    {

        File file = new File(path);

        if (file.exists()) {
            String deleteCmd = "rm -r " + path;
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec(deleteCmd);
            } catch (IOException e) { }
        }
    }
    
    public Boolean create_folder(String name)
    {
    	
    	File directory = new File("/sdcard/" + name);
    	directory.mkdirs();
    	
    	File mydir = new File(Environment.getExternalStorageDirectory() + "/mydir/");
    	if(!mydir.exists())
    	    mydir.mkdirs();
    	else
    	    Log.d("error", "dir. already exists");
    	
    	
    	return false;
    }
    
    /* IF THE DIRECTORY IS NOT EMPTY . USE THIS FUNCTION */
    public boolean deleteNon_EmptyDir(File dir) 
    {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++)
            {
                boolean success = deleteNon_EmptyDir(new File(dir, children[i]));
                if (!success)
                {
                    return false;
                }
            }
        }
        return dir.delete();
    }
    
    public Boolean write(String fname, String fcontent)
    {
           try {

               String fpath = "/sdcard/"+fname+".txt";

               File file = new File(fpath);

               // If file does not exists, then create it
               if (!file.exists()) {
                   file.createNewFile();
               }

               FileWriter fw = new FileWriter(file.getAbsoluteFile());
               BufferedWriter bw = new BufferedWriter(fw);
               bw.write(fcontent);
               bw.close();

               Log.d("Suceess","Sucess");
               return true;

           } catch (IOException e) {
               e.printStackTrace();
               return false;
           }

    }

    public String read(String fname){

        BufferedReader br = null;
        String response = null;

           try {

               StringBuffer output = new StringBuffer();
               String fpath = "/sdcard/"+fname+".txt";

               br = new BufferedReader(new FileReader(fpath));
               String line = "";
               while ((line = br.readLine()) != null) {
                   output.append(line +"n");
               }
               response = output.toString();

           } catch (IOException e) {
               e.printStackTrace();
               return null;

           }
           return response;

    }
}