/*
 * Copyright (C) 2009 University of Washington
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.odk.collect.android.widgets;

import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.StringData;
import org.javarosa.form.api.FormEntryPrompt;
import org.odk.collect.android.R;
import org.odk.collect.android.activities.FormEntryActivity;
import org.odk.collect.android.activities.PermissionActivity;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.utilities.FileUtils;
import org.odk.collect.android.utilities.MediaUtils;
import org.odk.collect.android.views.ButtonBuilder;

import java.io.File;
import java.util.HashMap;

/**
 * Widget that allows user to take pictures, sounds or video and add them to the form.
 *
 * @author Carl Hartung (carlhartung@gmail.com)
 * @author Yaw Anokwa (yanokwa@gmail.com)
 */
public class ImageWidget extends QuestionWidget implements IBinaryWidget, PermissionActivity.PermissionRequired {
  private final static String t = "MediaWidget";
  public static final int CAMERA_REQUEST_CODE = 1;
  public static final int STORAGE_REQUEST_CODE = 2;

  private Button mCaptureButton;
  private Button mChooseButton;
  private ImageView mImageView;

  private String mBinaryName;

  private String mInstanceFolder;

  private TextView mErrorTextView;


  public ImageWidget(Context context, final FormEntryPrompt prompt) {
    super(context, prompt);

    mInstanceFolder =
        Collect.getInstance().getFormController().getInstancePath().getParent();

    setOrientation(LinearLayout.VERTICAL);

    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    mErrorTextView = new TextView(context);
    mErrorTextView.setId(QuestionWidget.newUniqueId());
    mErrorTextView.setText("Selected file is not a valid image");

    final @StringRes int viewImageTextResID = R.string.view_image;

    // setup capture button
    final ButtonBuilder captureButtonBuilder = new ButtonBuilder(context);
    captureButtonBuilder.withParent(this);
    final int captureButtonTextResID = prompt.isReadOnly() ? viewImageTextResID : R.string.capture_image;
    captureButtonBuilder.withTextResource(captureButtonTextResID);
    mCaptureButton = captureButtonBuilder.build();
    mCaptureButton.setLayoutParams(params);

    // launch capture intent on click
    mCaptureButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (prompt.isReadOnly()) {
          launchImageViewer();
        } else {
          showCamera();
        }
      }
    });

    // setup chooser button
    final ButtonBuilder chooseButtonBuilder = new ButtonBuilder(context);
    chooseButtonBuilder.withParent(this);
    final int chooseButtonTextResID = prompt.isReadOnly() ? viewImageTextResID : R.string.choose_image;
    chooseButtonBuilder.withTextResource(chooseButtonTextResID);
    mChooseButton = chooseButtonBuilder.build();
    mChooseButton.setLayoutParams(params);

    // launch capture intent on click
    mChooseButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (prompt.isReadOnly()) {
          launchImageViewer();
        } else {
          showFilePicker();
        }
      }
    });

    // finish complex layout
    addView(mCaptureButton);
    addView(mChooseButton);
    addView(mErrorTextView);

    // and hide the capture and choose button if read-only and no image to display.
    final boolean hasImage = mBinaryName == null || mBinaryName.trim().isEmpty();
    if (prompt.isReadOnly()) {
      if (!hasImage) {
        mCaptureButton.setVisibility(View.GONE);
      }
      mChooseButton.setVisibility(View.GONE);
    }
    mErrorTextView.setVisibility(View.GONE);

    // retrieve answer from data model and update ui
    mBinaryName = prompt.getAnswerText();

    // Only add the imageView if the user has taken a picture
    if (mBinaryName != null) {
      mImageView = new ImageView(getContext());
      mImageView.setScaleType(ImageView.ScaleType.CENTER);
      mImageView.setId(QuestionWidget.newUniqueId());
      ViewCompat.setTransitionName(mImageView, context.getString(R.string.view_image_transition_name));

      File f = new File(mInstanceFolder + File.separator + mBinaryName);

      if (f.exists()) {
        Bitmap bmp = FileUtils.getBitmapScaledToDisplay(context, f);
        if (bmp == null) {
          mErrorTextView.setVisibility(View.VISIBLE);
        }
        mImageView.setImageBitmap(bmp);
      } else {
        mImageView.setImageBitmap(null);
      }

      mImageView.setPadding(10, 10, 10, 10);
      mImageView.setAdjustViewBounds(true);
      mImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          launchImageViewer();
        }
      });
      mImageView.setLayoutParams(params);

      addView(mImageView);
    }
  }

  private void launchImageViewer() {
    File f = new File(mInstanceFolder + File.separator + mBinaryName);
    if (f.exists()) {
      Collect.getInstance().getViewImageActivityDelagate().launchViewImageActivity(getContext(), f, mImageView);
    }
  }

  private void showFilePicker() {
    final Context context = getContext();
    final boolean hasStorage = ContextCompat.checkSelfPermission(context, Manifest.permission
        .WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    if (hasStorage) {
      mErrorTextView.setVisibility(View.GONE);
      Intent i = new Intent(Intent.ACTION_GET_CONTENT);
      i.setType("image/*");

      try {
        Collect.getInstance().getFormController()
            .setIndexWaitingForData(mPrompt.getIndex());
        ((Activity) context).startActivityForResult(i, FormEntryActivity.IMAGE_CHOOSER);
      } catch (ActivityNotFoundException e) {
        Toast.makeText(context,
            context.getString(R.string.activity_not_found, "choose image"),
            Toast.LENGTH_SHORT).show();
        Collect.getInstance().getFormController().setIndexWaitingForData(null);
      }
    } else {
      final Intent intent = PermissionActivity.newIntent(context, new String[]{Manifest.permission
          .WRITE_EXTERNAL_STORAGE}, STORAGE_REQUEST_CODE);
      Collect.getInstance().getFormController().setIndexWaitingForData(mPrompt.getIndex());
      ((Activity) context).startActivityForResult(intent, FormEntryActivity.PERMISSION_UPDATE);
    }
  }

  private void showCamera() {
    final Context context = getContext();
    final boolean hasCameraPermission =
        ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
    final boolean hasStoragePermission =
        ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager
            .PERMISSION_GRANTED;
    if (hasCameraPermission && hasStoragePermission) {
      mErrorTextView.setVisibility(View.GONE);
      Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
      // We give the camera an absolute filename/path where to put the
      // picture because of bug:
      // http://code.google.com/p/android/issues/detail?id=1480
      // The bug appears to be fixed in Android 2.0+, but as of feb 2,
      // 2010, G1 phones only run 1.6. Without specifying the path the
      // images returned by the camera in 1.6 (and earlier) are ~1/4
      // the size. boo.

      // if this gets modified, the onActivityResult in
      // FormEntyActivity will also need to be updated.
      i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
          Uri.fromFile(new File(Collect.TMP_CAMERA_FILE_PATH)));
      try {
        Collect.getInstance().getFormController().setIndexWaitingForData(mPrompt.getIndex());
        ((Activity) context).startActivityForResult(i,
            FormEntryActivity.IMAGE_CAPTURE);
      } catch (ActivityNotFoundException e) {
        Toast.makeText(context,
            context.getString(R.string.activity_not_found, "image capture"),
            Toast.LENGTH_SHORT).show();
        Collect.getInstance().getFormController().setIndexWaitingForData(null);
      }
    } else {
      final String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
      Collect.getInstance().getFormController().setIndexWaitingForData(mPrompt.getIndex());
      final Intent permissionIntent = PermissionActivity.newIntent(context, permissions, CAMERA_REQUEST_CODE);
      ((Activity) context).startActivityForResult(permissionIntent, FormEntryActivity.PERMISSION_UPDATE);
    }
  }


  private void deleteMedia() {
    // get the file path and delete the file
    String name = mBinaryName;
    // clean up variables
    mBinaryName = null;
    // delete from media provider
    int del = MediaUtils.deleteImageFileFromMediaProvider(mInstanceFolder + File.separator + name);
    Log.i(t, "Deleted " + del + " rows from media content provider");
  }


  @Override
  public void clearAnswer() {
    // remove the file
    deleteMedia();
    mImageView.setImageBitmap(null);
    mErrorTextView.setVisibility(View.GONE);

    // reset buttons
    mCaptureButton.setText(getContext().getString(R.string.capture_image));
  }


  @Override
  public IAnswerData getAnswer() {
    if (mBinaryName != null) {
      return new StringData(mBinaryName.toString());
    } else {
      return null;
    }
  }


  @Override
  public void setBinaryData(Object newImageObj) {
    // you are replacing an answer. delete the previous image using the
    // content provider.
    if (mBinaryName != null) {
      deleteMedia();
    }

    File newImage = (File) newImageObj;
    if (newImage.exists()) {
      // Add the new image to the Media content provider so that the
      // viewing is fast in Android 2.0+
      ContentValues values = new ContentValues(6);
      values.put(Images.Media.TITLE, newImage.getName());
      values.put(Images.Media.DISPLAY_NAME, newImage.getName());
      values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
      values.put(Images.Media.MIME_TYPE, "image/jpeg");
      values.put(Images.Media.DATA, newImage.getAbsolutePath());

      Uri imageURI = getContext().getContentResolver().insert(
          Images.Media.EXTERNAL_CONTENT_URI, values);
      Log.i(t, "Inserting image returned uri = " + imageURI.toString());

      mBinaryName = newImage.getName();
      Log.i(t, "Setting current answer to " + newImage.getName());
    } else {
      Log.e(t, "NO IMAGE EXISTS at: " + newImage.getAbsolutePath());
    }

    Collect.getInstance().getFormController().setIndexWaitingForData(null);
  }

  @Override
  public void setFocus(Context context) {
    // Hide the soft keyboard if it's showing.
    InputMethodManager inputManager =
        (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(this.getWindowToken(), 0);
  }


  @Override
  public boolean isWaitingForBinaryData() {
    return mPrompt.getIndex().equals(Collect.getInstance().getFormController().getIndexWaitingForData());
  }


  @Override
  public void cancelWaitingForBinaryData() {
    Collect.getInstance().getFormController().setIndexWaitingForData(null);
  }


  @Override
  public void setOnLongClickListener(OnLongClickListener l) {
    mCaptureButton.setOnLongClickListener(l);
    mChooseButton.setOnLongClickListener(l);
    if (mImageView != null) {
      mImageView.setOnLongClickListener(l);
    }
  }


  @Override
  public void cancelLongPress() {
    super.cancelLongPress();
    mCaptureButton.cancelLongPress();
    mChooseButton.cancelLongPress();
    if (mImageView != null) {
      mImageView.cancelLongPress();
    }
  }

  @Override
  public void onPermissionsResult(final int requestCode, final HashMap<String, Boolean> results) {
    final Context context = getContext();
    switch (requestCode) {
      case CAMERA_REQUEST_CODE:
        final boolean hasCamera =
            ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
        final boolean hasStoragePermission =
            ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager
                .PERMISSION_GRANTED;
        if (hasCamera && hasStoragePermission) {
          showCamera();
        } else {
          Toast.makeText(context, R.string.error_photo_permissions, Toast.LENGTH_SHORT).show();
        }
        Collect.getInstance().getFormController().setIndexWaitingForData(null);
        break;
      case STORAGE_REQUEST_CODE:
        final boolean hasStorage =
            ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager
                .PERMISSION_GRANTED;
        if (hasStorage) {
          showFilePicker();
        } else {
          Toast.makeText(context, R.string.error_storage_permissions, Toast.LENGTH_SHORT).show();
        }
        Collect.getInstance().getFormController().setIndexWaitingForData(null);
        break;
    }
  }

  @Override
  public void cancelWaitingForPermission() {
    Collect.getInstance().getFormController().setIndexWaitingForData(null);
  }

  @Override
  public boolean isWaitingForPermission() {
    return mPrompt.getIndex().equals(Collect.getInstance().getFormController().getIndexWaitingForData());
  }

  public interface ViewImageActivityDelagate {

    /**
     * Launch an Activity to view an image.
     *
     * @param context
     *     the current Context, used to launch the Activity.
     * @param file
     *     the file containing the image we want to display.
     * @param imageView
     *     the ImageView showing the preview of the image.
     */
    void launchViewImageActivity(final Context context, final File file, final ImageView imageView);
  }
}
