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.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.res.StatusMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 制作商品
 */
public class devstatus_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

    public ArrayList<StatusMode> statusModes = DataBus.getInstance().statusModes;
    public Activity activity;

    public devstatus_adapter(Context context, Activity _activity) {
        this.context = context;
        activity=_activity;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.devstatus, parent, false);
        return new MyViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof MyViewHolder) {
                MyViewHolder myViewHolder = (MyViewHolder) holder;
                StatusMode statusMode= statusModes.get(position);
                myViewHolder.title.setText(statusMode.Name);
                if(statusMode.Status)
                {
                    myViewHolder.image.setImageResource(statusMode.Url_open);
                    myViewHolder.text.setText("打开");
                    myViewHolder.text.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.app_color_blue));
                }else
                {
                    myViewHolder.image.setImageResource(statusMode.Url_close);
                    myViewHolder.text.setText("关闭");
                    myViewHolder.text.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.black_50p));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 刷新
     * @param
     */
    public void refresh(){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    notifyDataSetChanged();
                } catch (Exception e) {

                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return statusModes.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        /**
         * 标题
         */
        TextView title;
        /**
         * 图片
         */
        ImageView image;
        /**
         * 描述
         */
        TextView text;
        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            image = (ImageView) view.findViewById(R.id.image);
            text=(TextView) view.findViewById(R.id.text);
        }
    }
}

