package xn.xxp.home.notice;

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

import com.blankj.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import xn.xxp.R;
import xn.xxp.room.bean.NoticeSummary;

public class NoticeListAdapter extends BaseAdapter {

    private List<NoticeSummary> noticeSummaryList;
    private LayoutInflater layoutInflater;

    public NoticeListAdapter(Context context, List<NoticeSummary> NoticeSummaryList) {
        this.noticeSummaryList = NoticeSummaryList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return null == noticeSummaryList ? 0 : noticeSummaryList.size();
    }

    @Override
    public NoticeSummary getItem(int position) {
        return (null == noticeSummaryList || noticeSummaryList.isEmpty()) ? null : noticeSummaryList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_notice_list, null);
            holder = new Holder();
            holder.contextTV = convertView.findViewById(R.id.context_TV);
            holder.timeTV = convertView.findViewById(R.id.time_TV);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        NoticeSummary NoticeSummary = getItem(position);
        if (null != NoticeSummary) {
            holder.timeTV.setText(utc2Time(TextUtils.isEmpty(NoticeSummary.getCreateTime()) ? "" : NoticeSummary.getCreateTime()));
            // 系统消息
            if (0 == (NoticeSummary.getBusinessType())) {
                holder.contextTV.setText(TextUtils.isEmpty(NoticeSummary.getTitle()) ? "" : NoticeSummary.getTitle());
            } else {
                holder.contextTV.setText(TextUtils.isEmpty(NoticeSummary.getContent()) ? "" : NoticeSummary.getContent());
            }
        }
        return convertView;
    }

    private String utc2Time(String utc) {
        return TimeUtils.millis2String(TimeUtils.string2Millis(utc, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault())));
    }

    static class Holder {
        TextView contextTV, timeTV;
    }
}
