package org.odk.collect.android.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Switch;

import org.odk.collect.android.R;
import org.odk.collect.android.adapters.TextSizeAdapter;
import org.odk.collect.android.utilities.DrawPreferences;
import org.odk.collect.android.views.DynamicWidthSpinner;

public class RequestTextDialog extends DialogFragment {

  private OnRequestTextListener onRequestTextListener;

  // VARS
  private Bitmap mPreviewBitmap;
  private int textSize = 1;

  public RequestTextDialog() {
  }

  public static RequestTextDialog newInstance() {
    return new RequestTextDialog();
  }

  @NonNull
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Context context = getContext();
    final View view = LayoutInflater.from(context).inflate(R.layout.layout_request_text, null);
    final EditText textInputEditText = (EditText) view.findViewById(R.id.et_req_text);
    final DrawPreferences drawPreferences = new DrawPreferences(context);
    textSize = drawPreferences.getTextSize();
    final DynamicWidthSpinner sizeSpinner = (DynamicWidthSpinner) view.findViewById(R.id.request_text_size);
    final TextSizeAdapter textSizeAdapter = new TextSizeAdapter(context);
    sizeSpinner.setAdapter(textSizeAdapter);
    textSizeAdapter.setSelectedItem(textSize, sizeSpinner);
    sizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
        textSize = textSizeAdapter.getItem(position);
        textSizeAdapter.setSelectedPosition(position);
        drawPreferences.setTextSize(textSize);
      }

      @Override
      public void onNothingSelected(final AdapterView<?> parent) {

      }
    });
    final Switch boldSwitch = (Switch) view.findViewById(R.id.request_text_bold);
    final Switch italicsSwitch = (Switch) view.findViewById(R.id.request_text_italics);
    boldSwitch.setChecked(drawPreferences.isTextBold());
    italicsSwitch.setChecked(drawPreferences.isTextItalics());

    AlertDialog.Builder builder = new AlertDialog.Builder(context)
        .setView(view)
        .setPositiveButton(R.string.draw_text_confirm, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            if (onRequestTextListener != null) {
              final String text = textInputEditText.getText().toString();
              final boolean bold = boldSwitch.isChecked();
              final boolean italics = italicsSwitch.isChecked();
              drawPreferences.setTextBold(bold);
              drawPreferences.setTextItalics(italics);
              int typefaceStyle = Typeface.NORMAL;
              if (bold && italics) {
                typefaceStyle = Typeface.BOLD_ITALIC;
              } else if (bold) {
                typefaceStyle = Typeface.BOLD;
              } else if (italics) {
                typefaceStyle = Typeface.ITALIC;
              }
              final Typeface typeface = Typeface.create(Typeface.DEFAULT, typefaceStyle);
              onRequestTextListener.onRequestTextConfirmed(text, textSize, typeface);
            }
            dismiss();
          }
        })
        .setNegativeButton(R.string.draw_text_cancel, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            if (onRequestTextListener != null) {
              onRequestTextListener.onRequestTextCancelled();
            }
            dismiss();
          }
        });

    final AlertDialog alertDialog = builder.create();
    textInputEditText.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

      }

      @Override
      public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

      }

      @Override
      public void afterTextChanged(final Editable s) {
        alertDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(!s.toString().isEmpty());
      }
    });
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
      @Override
      public void onShow(final DialogInterface dialog) {
        ((AlertDialog) dialog).getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
      }
    });
    return alertDialog;
  }

  public void setOnRequestTextListener(OnRequestTextListener onRequestTextListener) {
    this.onRequestTextListener = onRequestTextListener;
  }

  public interface OnRequestTextListener {
    void onRequestTextConfirmed(String requestedText, final float textSize, final Typeface typeface);

    void onRequestTextCancelled();
  }
}
