package com.bonait.bnframework.modules.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.db.res.ResAlertLog;
import com.bonait.bnframework.common.db.res.ResLog;

import java.util.List;

public class yjrz_adapter extends ArrayAdapter<ResLog> {
    private List<ResLog> datas;
    private Context context;

    private int resource;

    public yjrz_adapter(@NonNull Context context, int resource, @NonNull List<ResLog> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource=resource;
        datas=objects;
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ResLog log = (ResLog) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(this.resource, parent, false);
        //分别获取 image view 和 textview 的实例
        TextView name,type,time,message;
        //name = (TextView) view.findViewById(R.id.name);
        type = (TextView) view.findViewById(R.id.type);
        time = (TextView) view.findViewById(R.id.time);
        message = (TextView) view.findViewById(R.id.message);
        //name.setText(log.Name);
        switch (log.type)
        {
            case 1:
                type.setText("登录日志");
                break;
            case 2:
                type.setText("角色操作日志");
                break;
            case 3:
                type.setText("数据接收");
                break;
            case 4:
                type.setText("上传日志");
                break;
            case 5:
                type.setText("订单处理日志");
                break;
        }
        time.setText(log.createTime);
        message.setText(log.text);
        return view;
    }
    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }
    /**
     * 刷新
     * @param
     */
    public void refresh(){
        Activity activity= findActivity(context);
        if(activity!=null)
        {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        notifyDataSetChanged();
                    } catch (Exception e) {

                    }
                }
            });
        }
    }
}