package info.knacki.pass.ui.passwordList;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MenuItem;
import android.widget.PopupMenu;

import java.io.File;

import info.knacki.pass.R;
import info.knacki.pass.io.PathUtils;

public class EditablePasswordListView extends PasswordListView implements PopupMenu.OnMenuItemClickListener  {
    private PasswordView fSelectedPassword;
    private boolean fDisplayHiddenFiles;

    public EditablePasswordListView(Context ctx) {
        super(ctx);
    }

    public EditablePasswordListView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
    }

    public EditablePasswordListView(Context ctx, AttributeSet attrs, int style) {
        super(ctx, attrs, style);
    }

    public EditablePasswordListView(Activity ctx, PasswordEditListener editListener, String rootPath, boolean displayHidden) {
        super(ctx);
        Init(editListener, rootPath, displayHidden);
    }

    public EditablePasswordListView Init(PasswordEditListener editListener, String rootPath, boolean displayHidden) {
        super.Init(editListener, rootPath);
        fDisplayHiddenFiles = displayHidden;
        return this;
    }

    public void SetDisplayHiddenFiles(boolean displayHidden) {
        fDisplayHiddenFiles = displayHidden;
        refresh();
    }

    @Override
    protected boolean IsHidden(File f) {
        return (PathUtils.IsHidden(f.getAbsolutePath()) != fDisplayHiddenFiles);
    }

    protected int GetMenuForFile(FileIdentity f) {
        if (f.fIsDir)
            return R.menu.context_dir_menu;
        return (PathUtils.IsHidden(f.fName)) ? R.menu.context_trashed_menu : R.menu.context_file_menu;
    }

    protected void AddContextMenu(final FileIdentity file, final PasswordView pv) {
        if (file.isParent())
            return;
        pv.setOnLongClickListener(v -> {
            fSelectedPassword = pv;
            PopupMenu menu = new PopupMenu(fContext, pv);
            menu.getMenuInflater().inflate(GetMenuForFile(file), menu.getMenu());
            menu.setOnMenuItemClickListener(EditablePasswordListView.this);
            menu.show();
            return true;
        });
    }

    @Override
    protected PasswordView CreateView(FileIdentity file) {
        final PasswordView pv = super.CreateView(file);
        AddContextMenu(file, pv);
        return pv;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        boolean found = true;

        switch (item.getItemId()) {
            case R.id.remove:
                if (fSelectedPassword.IsDir()) {
                    ((PasswordEditListener) fClickListener).OnRemoveDirectory(new File(fCurrentDir + "/" + fSelectedPassword.fFullName));
                } else {
                    ((PasswordEditListener) fClickListener).OnRemovePassword(new File(fCurrentDir + "/" + fSelectedPassword.fFullName));
                }
                break;

            case R.id.restore:
                ((PasswordEditListener) fClickListener).OnRestorePassword(new File(fCurrentDir + "/" + fSelectedPassword.fFullName));
                break;

            case R.id.encryption_information:
                ((PasswordEditListener) fClickListener).OnEncryptionInformation(new File(fCurrentDir +"/" +fSelectedPassword.fFullName));
                break;

            case R.id.clipboard:
                ((PasswordEditListener) fClickListener).OnCopyToClipboard(new File(fCurrentDir +"/" +fSelectedPassword.fFullName));
                break;

            default:
                found = false;
                break;
        }
        fSelectedPassword = null;
        return found;
    }
}
