package com.example.bpa.view.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.example.bpa.R;
import com.example.bpa.config.ConfigName;
import com.example.bpa.view.mode.ResAlertLog;
import com.example.bpa.view.mode.ResSubOrder;

import java.util.List;


public class ddgl_adapter extends ArrayAdapter<ResSubOrder> {
    private List<ResSubOrder> datas;
    private Context context;

    private int resource;

    public ddgl_adapter(@NonNull Context context, int resource, @NonNull List<ResSubOrder> 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) {
        ResSubOrder order = (ResSubOrder) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(this.resource, parent, false);
        //分别获取 image view 和 textview 的实例
        TextView bh,time,name,status,bz;
        bh = (TextView) view.findViewById(R.id.bh);
        time = (TextView) view.findViewById(R.id.time);
        name = (TextView) view.findViewById(R.id.name);
        status = (TextView) view.findViewById(R.id.status);
        bz = (TextView) view.findViewById(R.id.bz);
        bh.setText(order.id);
        time.setText(order.createTime);
        name.setText(order.goodsName);
        time.setText(order.createTime);
        status.setText(order.statusText);
        bz.setText(order.exp);
        switch (order.status)
        {
            case 0://未开始
                status.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.warm_gray));
                break;
            case 1://制作中
                status.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.color_purl));
                break;
            case 2://已完成
                status.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.foreground));
                break;
            case 3://已超时
                status.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.warm_red));
                break;

        }

        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) {

                    }
                }
            });
        }
    }
}
