package com.chinalwb.are.styles;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.Editable;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AlignmentSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chinalwb.are.AREditText;
import com.chinalwb.are.Constants;
import com.chinalwb.are.Util;
import com.chinalwb.are.spans.AreImageSpan;
import com.chinalwb.are.styles.toolbar.ARE_Toolbar;
import com.rainliu.glidesupport.GlideApp;
import com.rainliu.glidesupport.GlideRequests;

public class ARE_Image implements IARE_Style {

	private ImageView mInsertImageView;

	private AREditText mEditText;

	private Context mContext;

    private static GlideRequests sGlideRequests;

    private static int sWidth = 0;

    /**
	 *
	 * @param emojiImageView the emoji image view
	 */
	public ARE_Image(ImageView emojiImageView) {
		this.mInsertImageView = emojiImageView;
		this.mContext = emojiImageView.getContext();
		sGlideRequests = GlideApp.with(mContext);
        sWidth = Util.getScreenWidthAndHeight(mContext)[0];
		setListenerForImageView(this.mInsertImageView);
	}

	public void setEditText(AREditText editText) {
		this.mEditText = editText;
	}

	@Override
	public void setListenerForImageView(ImageView imageView) {
		imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				openImageChooser();
			}
		});
	} // #End of setListenerForImageView(..)

	public enum ImageType {
		URI,
		RES,
	}

	/**
	 * Open system image chooser page.
	 */
	private void openImageChooser() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		((Activity) this.mContext).startActivityForResult(intent, ARE_Toolbar.REQ_IMAGE);
	}


	/**
	 *
	 */
	public void insertImage(final Object src, final ImageType type) {
	    this.mEditText.useSoftwareLayerOnAndroid8();
		SimpleTarget myTarget = new SimpleTarget<Bitmap>() {
			@Override
			public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
				if (bitmap == null) { return; }

                bitmap = Util.scaleBitmapToFitWidth(bitmap, sWidth);
                ImageSpan imageSpan = null;
                if (type == ImageType.URI) {
                    imageSpan = new AreImageSpan(mContext, bitmap, ((Uri) src));
				}
				if (imageSpan == null) { return; }
				insertSpan(imageSpan);
			}
		};

        if (type == ImageType.URI) {
            sGlideRequests.asBitmap().load((Uri) src).centerCrop().into(myTarget);
        } else if (type == ImageType.RES) {
            ImageSpan imageSpan = new AreImageSpan(mContext, ((int) src));
            insertSpan(imageSpan);
        }
	}

	private void insertSpan(ImageSpan imageSpan) {
		Editable editable = this.mEditText.getEditableText();
		int start = this.mEditText.getSelectionStart();
		int end = this.mEditText.getSelectionEnd();

		AlignmentSpan centerSpan = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
		SpannableStringBuilder ssb = new SpannableStringBuilder();
		ssb.append(Constants.CHAR_NEW_LINE);
		ssb.append(Constants.ZERO_WIDTH_SPACE_STR);
		ssb.append(Constants.CHAR_NEW_LINE);
		ssb.append(Constants.ZERO_WIDTH_SPACE_STR);
		ssb.setSpan(imageSpan, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		ssb.setSpan(centerSpan, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		AlignmentSpan leftSpan = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL);
		ssb.setSpan(leftSpan, 3,4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

		editable.replace(start, end, ssb);
	}

	@Override
	public void applyStyle(Editable editable, int start, int end) {
		// Do nothing
	}

	@Override
	public ImageView getImageView() {
		return this.mInsertImageView;
	}

	@Override
	public void setChecked(boolean isChecked) {
		// Do nothing
	}

	@Override
	public boolean getIsChecked() {
		return false;
	}

	@Override
	public EditText getEditText() {
		return this.mEditText;
	}
}

@@ -2275,56 +2275,967 @@
  %7B%0A%09
-    this.mEditText.useSoftwareLayerOnAndroid8();
+%09// Note for a possible bug:%0A%09%09// There may be a possible bug here, it is related to:%0A%09%09//   https://issuetracker.google.com/issues/67102093%0A%09%09// But I forget what the real use case is, just remember that%0A%09%09// When working on the feature, there was a crash bug%0A%09%09//%0A%09%09// That's why I introduce the method below:%0A%09    // this.mEditText.useSoftwareLayerOnAndroid8();%0A%09%09//%0A%09%09// However, with this setting software layer, there is another%0A%09%09// bug which is when inserting a few (2~3) images, there will%0A%09%09// be a warning:%0A%09%09//%0A%09%09// AREditText not displayed because it is too large to fit into a software layer (or drawing cache), needs 17940960 bytes, only 8294400 available%0A%09%09//%0A%09%09// And then the EditText becomes an entire white nothing displayed%0A%09%09//%0A%09%09// So in temporary, I commented out this method invoke to prevent this known issue%0A%09%09// When someone run into the crash bug caused by this on Android 8%0A%09%09// I can then find out a solution to cover both cases
 %0A%09%09S

Comment out special handling for Android 8 when inserting an image