package com.epson.memcardacc;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;


public class PasswordDialogFragment extends DialogFragment implements DialogInterface.OnKeyListener {
    private static final String ARG_KEY_FIRST_AUTHENTICATION = "first_authentication";
    private TextView mMessage;
    private EditText mPasswordEdit;
    private EditText mUserNameEdit;

    public interface Callback {
        void onNegativeClicked();

        void onPositiveClicked(String str, String str2);
    }

    public static PasswordDialogFragment newInstance(boolean z) {
        PasswordDialogFragment passwordDialogFragment = new PasswordDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(ARG_KEY_FIRST_AUTHENTICATION, z);
        passwordDialogFragment.setArguments(bundle);
        return passwordDialogFragment;
    }

    public Dialog onCreateDialog(Bundle bundle) {
        Bundle arguments = getArguments();
        boolean z = true;
        if (arguments != null) {
            z = arguments.getBoolean(ARG_KEY_FIRST_AUTHENTICATION, true);
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View inflate = View.inflate(getActivity(), R.layout.fragment_password_dialog, (ViewGroup) null);
        mMessage = (TextView) inflate.findViewById(R.id.password_message);
        mMessage.setText(z ? R.string.memcard_password_input_message : R.string.memcard_password_incorrect_message);
        mUserNameEdit = (EditText) inflate.findViewById(R.id.memcardUsername);
        mPasswordEdit = (EditText) inflate.findViewById(R.id.memcardPassword);
        ((ImageButton) inflate.findViewById(R.id.imageButton1)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                PasswordDialogFragment.mUserNameEdit.getText().clear();
            }
        });
        ((ImageButton) inflate.findViewById(R.id.imageButton2)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                PasswordDialogFragment.mPasswordEdit.getText().clear();
            }
        });
        builder.setView(inflate).setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                String userName = PasswordDialogFragment.this.getUserName();
                String password = PasswordDialogFragment.this.getPassword();
                CifsAccess.setSmbAuthInfo(new SmbAuthInfo(userName, password));
                ((Callback) PasswordDialogFragment.this.getActivity()).onPositiveClicked(userName, password);
            }
        }).setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                PasswordDialogFragment.this.cancelAuthentication();
            }
        }).setTitle(R.string.str_enter_pass);
        AlertDialog create = builder.create();
        create.setCanceledOnTouchOutside(false);
        create.setOnKeyListener(this);
        return create;
    }

    public void cancelAuthentication() {
        ((Callback) getActivity()).onNegativeClicked();
    }

    public String getPassword() {
        return mPasswordEdit.getText().toString();
    }

    public String getUserName() {
        return mUserNameEdit.getText().toString();
    }

    public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
        if (i != 4) {
            return false;
        }
        cancelAuthentication();
        dismiss();
        return true;
    }
}
