package info.knacki.pass.services;

import android.content.Context;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import info.knacki.pass.R;
import info.knacki.pass.io.PathUtils;
import info.knacki.pass.ui.passwordList.PasswordClickListener;
import info.knacki.pass.ui.passwordList.PasswordListView;

public class AccessibilityView extends LinearLayoutCompat {
    public interface AccessibilityViewListener extends PasswordClickListener {
        void cancel();
    }

    private PasswordListView fPasswordListView;
    private AccessibilityViewListener fAccessibilityViewListener;

    public AccessibilityView Init(Context ctx, final AccessibilityViewListener listener) {
        fPasswordListView = findViewById(R.id.password_list);
        fPasswordListView.Init(listener, PathUtils.GetPassDir(ctx));
        fAccessibilityViewListener = listener;
        findViewById(R.id.cancel).setOnClickListener(v -> {
            if (fAccessibilityViewListener != null)
                fAccessibilityViewListener.cancel();
        });
        return this;
    }

    public AccessibilityView AddCancelButton() {
        findViewById(R.id.cancel).setVisibility(VISIBLE);
        return this;
    }

    public AccessibilityView(Context ctx) {
        super(ctx);
        setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        LayoutInflater.from(ctx).inflate(R.layout.activity_accessibility, this);
        findViewById(R.id.cancel).setVisibility(INVISIBLE);
    }

    public AccessibilityView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        LayoutInflater.from(ctx).inflate(R.layout.activity_accessibility, this);
        findViewById(R.id.cancel).setVisibility(INVISIBLE);
    }

    public AccessibilityView(Context ctx, AttributeSet attrs, int defStyle) {
        super(ctx, attrs, defStyle);
        setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        LayoutInflater.from(ctx).inflate(R.layout.activity_accessibility, this);
        findViewById(R.id.cancel).setVisibility(INVISIBLE);
    }

    public void reset() {
        fPasswordListView.reset();
    }

    public void refresh() {
        fPasswordListView.refresh();
    }

    public String GetCurrentDir() {
        return fPasswordListView.fCurrentDir;
    }
}
