package com.lc.myapplication.security.locks.draw;

import android.content.Context;
import android.util.AttributeSet;
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.lc.myapplication.security.locks.R;
import com.lc.myapplication.security.locks.adapter.BottomInputadapter;
import com.lc.myapplication.security.locks.adapter.TopInputadapter;
import com.lc.myapplication.security.locks.listener.BottomnumberClick;
import com.lc.myapplication.security.locks.listener.InputCodeFinishListener;


public class DrawInputView extends FrameLayout {
    private RecyclerView passcode_top;
    private RecyclerView passcode_bottom;
    private Context mcon;
    private StringBuilder my_code_top;
    private TopInputadapter topInputadapter;
    private BottomInputadapter bottomInputadapter;
    private InputCodeFinishListener inputCodeFinishListener;

    public DrawInputView(Context context) {
        super(context);
        initDrawView(context);
    }
   public void setInputFinish(InputCodeFinishListener codeFinish){
        this.inputCodeFinishListener = codeFinish;
   }
    private void initDrawView(Context context) {
        mcon = context;
        my_code_top = new StringBuilder();
        View view = LayoutInflater.from(context).inflate(R.layout.draw_input_view,null);
        passcode_top = view.findViewById(R.id.draw_top);
        passcode_bottom = view.findViewById(R.id.draw_bottom);
        addView(view);

        topInputadapter = new TopInputadapter();
        passcode_top.setAdapter(topInputadapter);
        passcode_top.setLayoutManager(new LinearLayoutManager(mcon,RecyclerView.HORIZONTAL,false));

        bottomInputadapter = new BottomInputadapter();
        passcode_bottom.setAdapter(bottomInputadapter);
        passcode_bottom.setLayoutManager(new GridLayoutManager(mcon,3));

        bottomInputadapter.setBottomClick(new BottomnumberClick() {
            @Override
            public void numberClick(String number) {
                if (my_code_top.length() >= 4){
                    my_code_top.delete(0,my_code_top.length());
                }else {
                    my_code_top.append(number);
                    topInputadapter.upDatakey(my_code_top.toString());
                    if (my_code_top.length() == 4){
                        inputCodeFinishListener.CodeFinish(my_code_top.toString());
                    }else {
                        inputCodeFinishListener.CodeNotFinish(my_code_top.toString());
                    }
                }
            }

            @Override
            public void delete() {
                if (my_code_top.length() >= 1){
                    my_code_top.deleteCharAt(my_code_top.length()-1);
                }
                topInputadapter.upDatakey(my_code_top.toString());
                if (my_code_top.length() < 4){
                    inputCodeFinishListener.CodeNotFinish(my_code_top.toString());
                }
            }
        });
    }

    public DrawInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDrawView(context);
    }
    public void ClearInputCode(){
        my_code_top.delete(0,my_code_top.length());
        topInputadapter.upDatakey(my_code_top.toString());
    }

}
