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

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

import com.example.bpa.R;
import com.example.bpa.config.ConfigName;
import com.example.bpa.config.DataBus;
import com.example.bpa.view.mode.MakeStatus;
import com.example.bpa.view.mode.ResGoodsMake;
import com.example.bpa.view.mode.StatusMode;

import java.util.List;


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

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<StatusMode> statusModes = DataBus.getInstance().statusModes;

    public devstatus_adapter(Context context) {
        this.context = context;
        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 devstatus_adapter.MyViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof devstatus_adapter.MyViewHolder) {
                devstatus_adapter.MyViewHolder myViewHolder = (devstatus_adapter.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.lable_color));
                }else
                {
                    myViewHolder.image.setImageResource(statusMode.Url_close);
                    myViewHolder.text.setText("关闭");
                    myViewHolder.text.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.warm_red));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    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) {

                    }
                }
            });
        }
    }

    @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);
        }
    }
}

