package com.siteshot.siteshot;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseUser;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class ProfileActivity extends Activity {

    public TextView mUsernameView;
    public ImageView mUserIcon;
    String mCurrentPhotoPath;
    private final String TAG = ProfileActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_profile);

        mUsernameView = (TextView) findViewById(R.id.profile_username);
        mUserIcon = (ImageView) findViewById(R.id.profile_user_icon);
        mUsernameView.setText(ParseUser.getCurrentUser().getUsername());
        GridView photoGrid = (GridView) findViewById(R.id.profile_photo_grid);
        photoGrid.setAdapter(new ImageAdapter(this));

        mUserIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dispatchTakePictureIntent();
            }
        });


        photoGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Placeholder for activity photo view
                Toast.makeText(ProfileActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {

            Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, new BitmapFactory.Options());
            Bitmap rotatedBitmap = rotate(bitmap);
            uploadImageToParse(rotatedBitmap);
            mUserIcon.setImageBitmap(rotatedBitmap);
        }
    }

    private void uploadImageToParse(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] data = stream.toByteArray();
        ParseFile file = new ParseFile("userIcon.jpg",data);
        ParseUser user = ParseUser.getCurrentUser();
        user.put("icon", file);
        user.saveInBackground();
    }

    private Bitmap rotate(Bitmap bitmap) {
        int iconOrientation = 1;

        try {
            ExifInterface exif = new ExifInterface(mCurrentPhotoPath);
            iconOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
        }

        Matrix matrix = new Matrix();

        switch (iconOrientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
        }

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.profile, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {

            // finishes profile activity and returns user to tab activity
            case R.id.action_return:
                finish();
                return true;

            // logout the user and redirect to login activity. finishes profile activity
            // so the user will not have previous user's profile info displayed
            case R.id.action_logout:
                ParseUser.logOut();
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);


        }
    }

    static final int REQUEST_TAKE_PHOTO = 1;

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Log.e(TAG, ex.getMessage());
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }


    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }
}

@@ -606,24 +606,57 @@
 get.Toast;%0A%0A
+import com.parse.ParseException;%0A
 import com.p
@@ -1803,25 +1803,24 @@
        %7D);%0A%0A
-%0A
         phot
@@ -2153,24 +2153,534 @@
        %7D);%0A%0A
+        setUserIcon();%0A    %7D%0A%0A    private void setUserIcon() %7B%0A        ParseUser user = ParseUser.getCurrentUser();%0A        ParseFile file = (ParseFile) user.get(%22icon%22);%0A%0A        if (file != null) %7B%0A            try %7B%0A                byte%5B%5D data = file.getData();%0A                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);%0A                mUserIcon.setImageBitmap(bitmap);%0A            %7D catch (ParseException e) %7B%0A                Log.e(TAG, e.getMessage());%0A            %7D%0A%0A%0A        %7D%0A
     %7D%0A%0A    p

Set user icon if user has one