package com.example.countries;

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

import androidx.annotation.NonNull;

import java.util.ArrayList;

public class CountryAdapter extends ArrayAdapter<CountryModel> {
    public CountryAdapter(Context context, ArrayList<CountryModel> countries) {
        super(context, 0, countries);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        CountryModel country = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        ImageView imageView = convertView.findViewById(R.id.flagImage);
        TextView nameView = convertView.findViewById(R.id.countryNameText);
        TextView cityView = convertView.findViewById(R.id.cityText);

        assert country != null;
        imageView.setImageResource(country.imageResId);
        nameView.setText(country.nameResId);
        cityView.setText(country.cityResId);

        return convertView;
    }
}
