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

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

import com.capton.colorfulprogressbar.ColorfulProgressbar;
import com.example.bpa.R;
import com.example.bpa.config.ConfigName;
import com.example.bpa.config.DataBus;
import com.example.bpa.helper.MyImageView;
import com.example.bpa.helper.T;
import com.example.bpa.view.mode.MakeStatus;
import com.example.bpa.view.mode.ResGoodsMake;
import com.example.bpa.view.mode.ResGoodsRecipe;

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


/**
 * 配方流程
 */
public class pflc_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<ResGoodsRecipe> Modes = new ArrayList<>()  ;

    public pflc_adapter(Context context, ArrayList<ResGoodsRecipe> recipes) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        Modes=recipes;
    }

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

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof pflc_adapter.MyViewHolder) {
                pflc_adapter.MyViewHolder myViewHolder = (pflc_adapter.MyViewHolder) holder;
                ResGoodsRecipe mode= Modes.get(position);
                myViewHolder.title.setText(SortStr(mode.sort)+" "+mode.materialName +"("+mode.value+")");
                myViewHolder.progress_bar.setProgress(mode.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;
        }
    }

    public String SortStr(int i)
    {
        if(i==1)
        {
            return "①";
        }else if(i==2)
        {
            return "②";
        }else if(i==3)
        {
            return "③";
        }else if(i==4)
        {
            return "④";
        }else if(i==5)
        {
            return "⑤";
        }else if(i==6)
        {
            return "⑥";
        }else if(i==7)
        {
            return "⑦";
        }else if(i==8)
        {
            return "⑧";
        }else if(i==9)
        {
            return "⑨";
        }else if(i==10)
        {
            return "⑩";
        }else if(i==11)
        {
            return "⑪";
        }else if(i==12)
        {
            return "⑫";
        }else if(i==13)
        {
            return "⑬";
        }else if(i==14)
        {
            return "⑭";
        }else if(i==15)
        {
            return "⑮";
        }else if(i==16)
        {
            return "⑯";
        }else if(i==17)
        {
            return "⑰";
        }else if(i==18)
        {
            return "⑱";
        }else if(i==19)
        {
            return "⑲";
        }else if(i==20)
        {
            return "⑳";
        }else
        {
            return "①";
        }
    }

    /**
     * 刷新
     * @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 Modes.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        /**
         * 标题
         */
        TextView title;
        /**
         * 制作进度
         */
        ColorfulProgressbar progress_bar;
        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            progress_bar=(ColorfulProgressbar) view.findViewById(R.id.progress_bar);
        }
    }
}