package jp.gihyo.mitoma.android.p031memberlist;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import org.jspecify.annotations.NonNull;

import java.util.ArrayList;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
    //**リスナーインターフェース********************************
    public interface OnMyClickListener {
        void onItemClick(View view, int position);
    }
    //********************************************************
    private int selectedPosition = RecyclerView.NO_POSITION;
    private ArrayList<MemberDTO> memberList;
    private OnMyClickListener myListener;

    // コンストラクタで表示データを受け取る
    public CustomAdapter(ArrayList<MemberDTO> memberList) {

        this.memberList = memberList;
    }
    // リスナーのセッター
    public void setOnMyClickListener(OnMyClickListener myListener){

        this.myListener = myListener;
    }
    //**以下必須メソッド ****************************************************************
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 1行分のレイアウトを生成
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    public int getItemCount() {
        return memberList.size();
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        // 指定位置のデータをTextViewに設定
        MemberDTO dto = memberList.get(position);
        holder.tvID.setText(dto.memberNo);
        holder.tvName.setText(dto.memberName);
        holder.tvPhone.setText(dto.phone);
        holder.tvEmail.setText(dto.eMail);
        // 行の背景色の変更
        if (position == selectedPosition) {
            holder.itemView.setBackgroundColor(Color.parseColor("#99CC00"));  // 選択時の色
        } else {
            holder.itemView.setBackgroundColor(Color.TRANSPARENT); // 通常時
        }
        //**クリック時の処理*************************************
        holder.itemView.setOnClickListener(v -> {
            int positionEx =holder.getBindingAdapterPosition();
            int previousPosition = selectedPosition;
            selectedPosition = positionEx;
            myListener.onItemClick(v,positionEx);
            // 再描画
            notifyItemChanged(previousPosition);
            notifyItemChanged(selectedPosition);
        });

    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvID;
        TextView tvName;
        TextView tvPhone;
        TextView tvEmail;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvID = itemView.findViewById(R.id.tvMemberNo);
            tvName = itemView.findViewById(R.id.tvMemberName);
            tvPhone = itemView.findViewById(R.id.tvPhone);
            tvEmail = itemView.findViewById(R.id.tvEmail);
        }
    }
}