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 java.util.List;

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

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<ResGoodsMake> mainGoods = DataBus.getInstance().GoodsMake;

    public makegood_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.makegoods, parent, false);
        return new makegood_adapter.MyGoodLCViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof makegood_adapter.MyGoodLCViewHolder) {
                makegood_adapter.MyGoodLCViewHolder myViewHolder = (makegood_adapter.MyGoodLCViewHolder) holder;
                ResGoodsMake goodsMake= mainGoods.get(position);
                myViewHolder.goodmakename.setText(goodsMake.good.name);
                myViewHolder.goodms.setText(goodsMake.makeMs);
                myViewHolder.goodStatus.setText(goodsMake.makeStatus.toString());
                if(goodsMake.makeStatus== MakeStatus.等待制作)
                {
                    myViewHolder.goodStatus.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.dd));
                }else if(goodsMake.makeStatus== MakeStatus.制作中)
                {
                    myViewHolder.goodStatus.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.lime));
                }else
                {
                    myViewHolder.goodStatus.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.white));
                }
                myViewHolder.progress_bar.setProgress(goodsMake.makeProcess);;
            }
        } 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 mainGoods.size();
    }

    public static class MyGoodLCViewHolder extends RecyclerView.ViewHolder {
        /**
         * 商品名称
         */
        TextView goodmakename;
        /**
         * 商品描述
         * 当前配置奶茶中..请稍后...
         */
        TextView goodms;
        /**
         * 商品制作状态
         */
        TextView goodStatus;
        /**
         * 制作进度
         */
        ProgressBar progress_bar;
        /**
         * 商品背景图片
         */
        ImageView good_Image;
        public MyGoodLCViewHolder(View view) {
            super(view);
            goodmakename = (TextView) view.findViewById(R.id.goodmakename);
            goodms = (TextView) view.findViewById(R.id.goodms);
            progress_bar=(ProgressBar) view.findViewById(R.id.progress_bar);
            good_Image = (ImageView)view.findViewById(R.id.good_Image);
            goodStatus=(TextView) view.findViewById(R.id.goodStatus);
        }
    }
}
