package com.example.TreballFiGrau;

import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class NoticiaCreadaAfegirFoto extends Activity implements OnClickListener{
	private static final int SELECT_VIDEO = 3;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent mediaChooser = new Intent(Intent.ACTION_PICK);
	      //comma-separated MIME types
	      //mediaChooser.setType("video/*, images/*"); AIXO FUNCONA
          mediaChooser.setType("image/*");
	      startActivityForResult(mediaChooser, SELECT_VIDEO);
			
		/*
        Log.d("aki","1111");
        Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Video.Media.DATA);


		Log.d("aki","2222");
        // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
        startActivityForResult(i, SELECT_VIDEO);
      	*/
         
    }
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
		Log.d("aki","despres thread");
	    if (resultCode == RESULT_OK) {
	
	        if (requestCode == SELECT_VIDEO) {
	        	/*
	        	Log.d("aki","select video");
	            Uri selectedVideoUri = data.getData();
	            String selectedPath = getPath(selectedVideoUri);
	            Log.d("aki","SELECT_VIDEO Path : " + selectedPath);
	            //uploadVideo(selectedPath);
	             * */
	        	
	        	Uri pickedImage = data.getData();
	            // Let's read picked image path using content resolver
	        	Log.d("aki","SELECT_VIDEO Path : ");
	            String[] filePath = { MediaStore.Images.Media.DATA};
	            Log.d("aki","SELECT_VIDEO Path : " );
	            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
	            cursor.moveToFirst();
	            
	            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
	            //String imagePath =getPath(pickedImage);
	        	Log.d("aki","despres imagePath");
	            // Now we need to set the GUI ImageView data with data read from the picked file.
	            // ----------AIXO FUNCIONA----- (linia d sota)
	            //image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
	            
	            //image.setImageBitmap(imatge.imatge);
	             
	            // AIXO ESTAVA
	        	//Foto.stringPathFoto=imagePath;
	        	//-------------------------
	        	NoticiaCreadaGAFotos.mThumbIds.add(imagePath);
	        	NoticiaCreada.fotos.add(imagePath);
	        	Log.d("aki","longitud adapter "+NoticiaCreadaGAFotos.mThumbIds.size());
	        	EscriureNoticiaMedia.adapterFotos.notifyDataSetChanged();
	            cursor.close();
	            finish();
	            
	        }      
	    }
	}
	
	@Override
    public void onClick(View v) {
		
        /*
		Intent intent = new Intent();
		intent.setType("video/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent,"Select a Video "), SELECT_VIDEO);*/
    }
	
	

	
}
