package com.example.applock.DrawView;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.applock.R;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.click.ButtomKeyClick;
import com.example.applock.listener.InputKeyListener;
import com.example.applock.lockadapter.ButtomAdapter;
import com.example.applock.lockadapter.TopAdapter;

public class InPutKeyView extends FrameLayout {
    private RecyclerView recyclerView_bottom;
    private RecyclerView recyclerView_top;
    private StringBuilder my_top_key;
    private int keylength = 4;
    private Context mycontext;
    private InputKeyListener inputKeyListener;
    private ButtomAdapter buttomAdapter;
    private TopAdapter topAdapter;

    public InPutKeyView(Context context) {
        super(context);
        initKeyBoard(context);
    }

    public InPutKeyView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initKeyBoard(context);
    }
    public void setInputKeyListener(InputKeyListener inputKeyListener) {
        this.inputKeyListener = inputKeyListener;
    }


    private void initKeyBoard(Context context) {
        my_top_key = new StringBuilder();
        mycontext = context;
        View inflate = LayoutInflater.from(context).inflate(R.layout.key_input_view, null);
        recyclerView_bottom = inflate.findViewById(R.id.recyclerView_bottom);
        recyclerView_top = inflate.findViewById(R.id.recyclerView_top);
        addView(inflate);

        topAdapter = new TopAdapter();
        recyclerView_top.setAdapter(topAdapter);
        recyclerView_top.setLayoutManager(new LinearLayoutManager(mycontext,RecyclerView.HORIZONTAL,false));

        buttomAdapter = new ButtomAdapter();
        recyclerView_bottom.setAdapter(buttomAdapter);
        recyclerView_bottom.setLayoutManager(new GridLayoutManager(mycontext,3));

        buttomAdapter.setBottomKeyClick(new ButtomKeyClick() {
            @Override
            public void OnKeyClick(String bottomkey) {
                if (my_top_key.length() >= keylength) {
                    my_top_key.delete(0, my_top_key.length());
                    Log.d(SoftWareLockApplication.TAG,"-------fin"+my_top_key.length());
                }
                my_top_key.append(bottomkey);
                Log.d(SoftWareLockApplication.TAG,"-------fin"+my_top_key.length());
                topAdapter.updateItem(my_top_key.toString());
                if (my_top_key.length() == keylength) {
                    inputKeyListener.onInputComplete(my_top_key.toString());
                }else {
                    inputKeyListener.onInputUnComplete(my_top_key.toString());
                }
            }

            @Override
            public void OnDelete() {
                if (my_top_key.length() >= 1) {
                    my_top_key.deleteCharAt(my_top_key.length() - 1);
                }
                topAdapter.updateItem(my_top_key.toString());
                if (my_top_key.length() < keylength) {
                    inputKeyListener.onInputUnComplete(my_top_key.toString());
                }

            }

        });
    }
    public void clearTopKey(){
        my_top_key.delete(0, my_top_key.length());
        topAdapter.updateItem(my_top_key.toString());
    }
}
