package com.dyhpoon.fodex.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dyhpoon.fodex.R;

import fr.tvbarthel.lib.blurdialogfragment.SupportBlurDialogFragment;

/**
 * Created by darrenpoon on 20/2/15.
 */
public class InsertTagDialog extends SupportBlurDialogFragment {

    private EditText mEditText;
    private OnClickListener mListener;
    private int mNumberOfPhotos = 0;

    public interface OnClickListener {
        public void onClick(DialogInterface dialog, String[] tags, int which);
    }

    private InsertTagDialog(int numberOfPhotos) {
        this.mNumberOfPhotos = numberOfPhotos;
    }

    public static InsertTagDialog newInstance(int numberOfPhotos) {
        InsertTagDialog dialog = new InsertTagDialog(numberOfPhotos);
        return dialog;
    }

    public void setOnClickListener(OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_insert_tag, null);
        builder.setView(view);
        final AlertDialog dialog = builder.create();

        // setup title
        TextView tv = (TextView) view.findViewById(R.id.text_view);
        tv.setText(getActivity().getResources().getQuantityString(R.plurals.dialog_index_photo, mNumberOfPhotos));

        // setup editText
        mEditText = (EditText) view.findViewById(R.id.edit_text);

        // setup animation
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

        // setup buttons' onClickListener
        Button cancelButton = (Button) view.findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(new InsertTagOnClickListener(dialog, mEditText, DialogInterface.BUTTON_NEGATIVE, mListener));
        Button addButton = (Button) view.findViewById(R.id.button_add);
        addButton.setOnClickListener(new InsertTagOnClickListener(dialog, mEditText, DialogInterface.BUTTON_POSITIVE, mListener));
        return dialog;
    }

    private class InsertTagOnClickListener implements View.OnClickListener {
        private Dialog mDialog;
        private EditText mEditText;
        private int mDialogButtonType;
        private OnClickListener mListener;

        public InsertTagOnClickListener(Dialog dialog, EditText editText, int dialogButtonType, OnClickListener listener) {
            mDialog = dialog;
            mEditText = editText;
            mDialogButtonType = dialogButtonType;
            mListener = listener;
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                String[] tags = mEditText.getText().toString().split("\\s+");
                if (tags.length == 0 || tags[0].length() == 0) tags = null;
                mListener.onClick(mDialog, tags, mDialogButtonType);
            }
        }
    }

    @Override
    protected boolean isDebugEnable() {
        return false;
    }

    @Override
    protected boolean isDimmingEnable() {
        return false;
    }

    @Override
    protected boolean isActionBarBlurred() {
        return true;
    }

    @Override
    protected float getDownScaleFactor() {
        return 8f;
    }

    @Override
    protected int getBlurRadius() {
        return 8;
    }

}

@@ -301,16 +301,94 @@
 dex.R;%0A%0A
+import java.util.Arrays;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0A%0A
 import f
@@ -3107,116 +3107,417 @@
     
-if (tags.length == 0 %7C%7C tags%5B0%5D.length() == 0) tags = null;%0A                mListener.onClick(mDialog, tags,
+List%3CString%3E filteredTags = new LinkedList%3C%3E(Arrays.asList(tags));%0A                filteredTags.removeAll(Arrays.asList(%22%22, null));%0A                if (filteredTags.size() == 0) %7B%0A                    filteredTags = null;%0A                %7D%0A                mListener.onClick(%0A                        mDialog,%0A                        filteredTags.toArray(new String%5BfilteredTags.size()%5D),%0A                       
  mDi

Fix strings with leading whitespace fail to tokenize properly in InsertTagDialog.