package com.yutou.jianr_mg.Fragments.GameDataFragments.WarLogFragments.Adapters;

import android.annotation.SuppressLint;
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 com.yutou.jianr_mg.Fragments.GameDataFragments.WarLogFragments.WarData;
import com.yutou.jianr_mg.R;

import java.util.List;

public class DaytimeListViewAdapter extends BaseAdapter {
    private Context context;
    private List<WarData> list;

    public DaytimeListViewAdapter(Context context, List<WarData> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_war, null);
            holder = new ViewHolder();
            holder.user = view.findViewById(R.id.user);
            holder.arrow = view.findViewById(R.id.arrow);
            holder.enemy = view.findViewById(R.id.enemy);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        WarData warData = list.get(i);
        if(warData.getAttackModel().equals("夜战")&&warData.getUserName()==null){
            holder.user.setText("");
            holder.arrow.setText("----进入夜战----");
            holder.enemy.setText("");
        }else {
            holder.user.setText(warData.getUserName());
            holder.arrow.setText("--" + warData.getAttackModel() + "-->");
            String[] enemyName = warData.getEnemy();
            String name = "";
            for (int j = 0; j < enemyName.length; j++) {
                name += enemyName[j] + "(-" + warData.getDamage()[j] + "HP)\n";
            }
            name=name.substring(0,name.length()-1);
            holder.enemy.setText(name);
        }
        return view;
    }

    public void setData(List<WarData> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void clear() {
        if (list != null)
            list.clear();
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView user, arrow, enemy;
    }
}
