package com.tool.applockpro.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.tool.applockpro.R;
import com.tool.applockpro.tool.Staticvalue;

public class NumberAdapter extends RecyclerView.Adapter<NumberAdapter.NumberVH> {
    private String[] numberlist = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "-0", "0", "-1"};
    private StringBuilder currentNumber = new StringBuilder();
    private PinAdapter mpinAdapter;
    private Context mcontext;

    public NumberAdapter(Context context, PinAdapter pinAdapter) {
        mcontext = context;
        mpinAdapter = pinAdapter;
    }

    @NonNull
    @Override
    public NumberVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.number_item, null);
        return new NumberVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NumberVH holder, @SuppressLint("RecyclerView") int position) {

        if (position == 9) {
            holder.getImage().setVisibility(View.GONE);
            holder.getNumbertext().setVisibility(View.GONE);
        } else if (position == 11) {
            holder.getImage().setVisibility(View.VISIBLE);
//            holder.getNumbertext().setVisibility(View.GONE);
        } else {
            holder.getImage().setVisibility(View.GONE);
            holder.getNumbertext().setText(numberlist[position]);
        }
        holder.getNumbertext().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == 11) {
                    if (currentNumber.length() > 0) {
                        currentNumber.deleteCharAt(currentNumber.length() - 1);
                        mpinAdapter.updateItem(currentNumber.toString());
                    }
                } else {
                    if (currentNumber.length() < 4) {
                        currentNumber.append(numberlist[position]);
                    }
                }
                mpinAdapter.updateItem(currentNumber.toString());
                if (currentNumber.length() == 4) {
                    Staticvalue.KEY_NUMBER = currentNumber.toString();
                    currentNumber.setLength(0);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return numberlist.length;
    }

    public class NumberVH extends RecyclerView.ViewHolder {
        private TextView numbertext;
        private ConstraintLayout image;

        public ConstraintLayout getImage() {
            return image;
        }

        public NumberVH(@NonNull View itemView) {
            super(itemView);
            numbertext = itemView.findViewById(R.id.text_number);
            image = itemView.findViewById(R.id.pin_cons);
        }

        public TextView getNumbertext() {
            return numbertext;
        }
    }
}
