package jp.gihyo.mitoma.android.s1601recycerview;

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

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ********************RecyclerViewを使う**************************


        // 表示するデータを準備
        FriendsDS friendsDS = FriendsDS.getInstance();
        ArrayList<FriendsDTO> friendsList = friendsDS.select();
        //イメージデータ作成
        ArrayList<Integer> imageList = new ArrayList<>();
        int i = 0;
        for (FriendsDTO dto : friendsList) {
            if (dto.id.charAt(0) == 'F') imageList.add(i, R.drawable.droid1);
            if (dto.id.charAt(0) == 'S') imageList.add(i, R.drawable.droid2);
            if (dto.id.charAt(0) == 'W') imageList.add(i, R.drawable.droid3);
            i++;
        }

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

        MyAdapter adapter = new MyAdapter(friendsList, imageList);
        adapter.setOnMyClickListener(new MyClickLister());
        recyclerView.setAdapter(adapter);

    }
    private class MyClickLister implements MyAdapter.OnMyClickListener {
        public void onItemClick(View view, int position) {
            String msg = getMessage(view);
            Toast.makeText(getApplicationContext(),
                    "Click: " + msg, Toast.LENGTH_LONG).show();
        }
        public void onItemLongClick(View view, int position) {
            String msg = getMessage(view);
            Toast.makeText(getApplicationContext(),
                    "Long Click: " + msg, Toast.LENGTH_LONG).show();
        }
         private String getText(View view,int resource) {
             TextView textView = view.findViewById(resource);
             return textView.getText().toString();
         }
        private String getMessage(View view) {
            String id = "";
            String name = "";
            if(view.getId() == -1) {
                TextView tvID = view.findViewById(R.id.tvID);
                id = tvID.getText().toString();
                TextView tvName = view.findViewById(R.id.tvName);
                name = tvName.getText().toString();
            }else{
                TextView tvName = view.findViewById(view.getId());
                name = tvName.getText().toString();
            }
            return id + " " + name + "が選択されました";
        }
     }
}
