package com.swago.user.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.swago.lib_country_picker.Country;
import com.swago.lib_country_picker.LetterHolder;
import com.swago.lib_country_picker.PyAdapter;
import com.swago.lib_country_picker.PyEntity;
import com.swago.user.R;
import com.swago.user.widget.VH;

import java.util.List;

/**
 * @date 2021/11/20 16:37
 * description:
 */
public class CAdapter  extends PyAdapter<RecyclerView.ViewHolder> {

    public CAdapter(List<? extends PyEntity> entities) {
        super(entities, '#');
    }

    @Override
    public RecyclerView.ViewHolder onCreateLetterHolder(ViewGroup parent, int viewType) {
        return new LetterHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_letter, parent, false));
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_country_large_padding, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, PyEntity entity, int position) {
        VH vh = (VH)holder;
        final Country country = (Country)entity;
        vh.ivFlag.setImageResource(country.flag);
        vh.tvName.setText(country.name);
        vh.tvCode.setText("+" + country.code);
        holder.itemView.setOnClickListener(v -> {
            listener.onItemClick(country.toJson());
        });
    }

    @Override
    public void onBindLetterHolder(RecyclerView.ViewHolder holder, LetterEntity entity, int position) {
        ((LetterHolder)holder).textView.setText(entity.letter.toUpperCase());
    }


    private OnClickListener listener;
    public interface OnClickListener{
        void onItemClick(String json);
    }

    public void setOnClickListener(OnClickListener listener){
        this.listener = listener;
    }
}