package jp.gihyo.mitoma.android.p031memberlist;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MemberDS memberDS = MemberDS.getInstance();
        ArrayList<MemberDTO> memberList = memberDS.select();

        RecyclerView rvMember = findViewById(R.id.rvMember);
        DividerItemDecoration dividerItemDecoration =
                new DividerItemDecoration(rvMember.getContext(),
                        DividerItemDecoration.VERTICAL); // 縦方向
        rvMember.addItemDecoration(dividerItemDecoration);

        CustomAdapter adapter = new CustomAdapter(memberList);
        adapter.setOnMyClickListener(new MyClickLister());
        rvMember.setAdapter(adapter);
    }
    private class MyClickLister implements CustomAdapter.OnMyClickListener {
        public void onItemClick(View view, int position) {
            String message = getMessage(view);
            Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show();
        }
        private String getMessage(View view) {
            String name = "";
            if(view.getId() == -1) {
                TextView tvName = view.findViewById(R.id.tvMemberName);
                name = tvName.getText().toString();
            }else{
                TextView tvName = view.findViewById(view.getId());
                name = tvName.getText().toString();
            }
            return name + "さんが選択されました";
        }
    }
}