package org.ntlab.acanthus_client.views.main_menu_ui.home;

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

import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.entities.AnimationJson;
import org.ntlab.acanthus_client.entities.Editor;

import java.util.ArrayList;
import java.util.Collection;

public class ListAnimationViewAdapter extends BaseAdapter {

    static class ViewHolder{
        TextView animationName;
        TextView editorsName;
        TextView createdDate;
    }

    private LayoutInflater inflater;
    private int itemLayoutId;
    private ArrayList<String> titles = new ArrayList<>();
    private ArrayList<ArrayList<String>> animationEditors =new ArrayList<>();
    private ArrayList<String> createdDates = new ArrayList<>();

    ListAnimationViewAdapter(Context context, int itemLayoutId,
                             Collection<AnimationJson> animationJsons) {
        super();
        this.inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.itemLayoutId = itemLayoutId;
        for(AnimationJson animationJson: animationJsons){
            this.titles.add(animationJson.getAnimationName());
            ArrayList<String> editors = new ArrayList<>();
            for(String editor: animationJson.getEditorNames()){
                editors.add(editor);
            }
            animationEditors.add(editors);
            this.createdDates.add(animationJson.getCreatedDate());
        }
    }

    ListAnimationViewAdapter(Context context, int itemLayoutId){
        super();
        int i = 0;
        this.inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.itemLayoutId = itemLayoutId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        // 最初だけ View を inflate して、それを再利用する
        if (convertView == null) {
            // activity_main.xml に list.xml を inflate して convertView とする
            convertView = inflater.inflate(itemLayoutId, parent, false);
            // ViewHolder を生成
            holder = new ViewHolder();
            holder.animationName = convertView.findViewById(R.id.animationName);
            holder.editorsName = convertView.findViewById(R.id.editorsName);
            holder.createdDate = convertView.findViewById(R.id.createdDate);
            convertView.setTag(holder);

            holder.animationName.setText(this.titles.get(position));
            for(String s: this.animationEditors.get(position))holder.editorsName.setText(s);
            holder.createdDate.setText(this.createdDates.get(position));

        }
        // holder を使って再利用
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        // holder の imageView にセット
        holder.animationName.setText(titles.get(position));
        // 現在の position にあるファイル名リストを holder の textView にセット
        for(String ed : animationEditors.get(position)){
        holder.editorsName.setText(ed);

        }

        return convertView;
    }

    @Override
    public int getCount() {
        return titles.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

}
