package com.obsez.android.lib.filechooser;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;

import java.lang.ref.WeakReference;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

class keyListener implements DialogInterface.OnKeyListener {
    private WeakReference<ChooserDialog> _c;

    keyListener(ChooserDialog c) {
        this._c = new WeakReference<>(c);
    }

    /**
     * Called when a key is dispatched to a dialog. This allows listeners to
     * get a chance to respond before the dialog.
     *
     * @param dialog  the dialog the key has been dispatched to
     * @param keyCode the code for the physical key that was pressed
     * @param event   the KeyEvent object containing full information about
     *                the event
     * @return {@code true} if the listener has consumed the event,
     * {@code false} otherwise
     */
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (event.getAction() != KeyEvent.ACTION_DOWN) return false;

        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) {
            if (_c.get()._newFolderView != null && _c.get()._newFolderView.getVisibility() == VISIBLE) {
                _c.get()._newFolderView.setVisibility(GONE);
                return true;
            }
            _c.get()._onBackPressed.onBackPressed((AlertDialog) dialog);
            return true;
        }

        if (!_c.get()._enableDpad) return true;

        if (!_c.get()._list.hasFocus()) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_UP:
                    if (_c.get()._alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).hasFocus()
                        || _c.get()._alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).hasFocus()
                        || _c.get()._alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).hasFocus()) {
                        if (_c.get()._options != null && _c.get()._options.getVisibility() == VISIBLE) {
                            _c.get()._options.requestFocus(View.FOCUS_LEFT);
                            return true;
                        } else if (_c.get()._newFolderView != null && _c.get()._newFolderView.getVisibility() == VISIBLE){
                            _c.get()._newFolderView.requestFocus(View.FOCUS_LEFT);
                            return true;
                        } else {
                            _c.get()._list.requestFocus();
                            _c.get().lastSelected = true;
                            return true;
                        }
                    }
                    if (_c.get()._options != null && _c.get()._options.hasFocus()) {
                        _c.get()._list.requestFocus();
                        _c.get().lastSelected = true;
                        return true;
                    }
                    break;
                default:
                    return false;
            }
        }

        if (_c.get()._list.hasFocus()) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    _c.get()._onBackPressed.onBackPressed(_c.get()._alertDialog);
                    _c.get().lastSelected = false;
                    return true;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    _c.get()._list.performItemClick(_c.get()._list, _c.get()._list.getSelectedItemPosition(), _c.get()._list.getSelectedItemId());
                    _c.get().lastSelected = false;
                    return true;
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    if (_c.get().lastSelected) {
                        _c.get().lastSelected = false;
                        if (_c.get()._options != null && _c.get()._options.getVisibility() == VISIBLE) {
                            _c.get()._options.requestFocus();
                        } else {
                            _c.get()._alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).requestFocus();
                        }
                        return true;
                    }
                    break;
                default:
                    return false;
            }
        }
        return false;
    }
}

@@ -164,16 +164,46 @@
 ew.View;
+%0Aimport android.widget.Button;
 %0A%0Aimport
@@ -423,83 +423,518 @@
 _c;%0A
-%0A    keyListener(ChooserDialog c) %7B%0A        this._c = new WeakReference%3C%3E(c
+    private WeakReference%3CButton%3E _neutral;%0A    private WeakReference%3CButton%3E _negative;%0A    private WeakReference%3CButton%3E _positive;%0A%0A    keyListener(ChooserDialog c) %7B%0A        this._c = new WeakReference%3C%3E(c);%0A        this._neutral = new WeakReference%3C%3E(c._alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL));%0A        this._negative = new WeakReference%3C%3E(c._alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE));%0A        this._positive = new WeakReference%3C%3E(c._alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
 );%0A 
@@ -2195,263 +2195,80 @@
 f (_
-c.get()._alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).hasFocus()%0A                        %7C%7C _c.get()._alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).hasFocus()%0A                        %7C%7C _c.get()._alertDialog.getButton(AlertDialog.BUTTON_POSITIVE
+neutral.get().hasFocus() %7C%7C _negative.get().hasFocus() %7C%7C _positive.get(
 ).ha
@@ -2623,16 +2623,17 @@
 VISIBLE)
+ 
 %7B%0A      
@@ -4306,66 +4306,165 @@
     
-_c.get()._alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL
+if (_neutral.get().getVisibility() == VISIBLE)%0A                                _neutral.get().requestFocus();%0A                            else _negative.get(
 ).re
@@ -4699,11 +4699,343 @@
 e;%0A    %7D
+%0A%0A    @Override%0A    protected void finalize() throws Throwable %7B%0A        this._c.clear();%0A        this._neutral.clear();%0A        this._negative.clear();%0A        this._positive.clear();%0A        this._c = null;%0A        this._neutral = null;%0A        this._negative = null;%0A        this._positive = null;%0A        super.finalize();%0A    %7D
 %0A%7D%0A

Update keyListener.java