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

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

import com.example.bpa.Model.SuOrderA;
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.helper.WrapContentLinearLayoutManager;
import com.example.bpa.service.BusinessServer;
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 order_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<ResGoodsMake> Modes = DataBus.getInstance().GoodsSMMake;

    public order_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.order_item, parent, false);
        return new order_adapter.MyViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof order_adapter.MyViewHolder) {
                order_adapter.MyViewHolder myViewHolder = (order_adapter.MyViewHolder) holder;
                ResGoodsMake statusMode = Modes.get(position);
                myViewHolder.title.setText(statusMode.good.name);
                myViewHolder.image.setImageURL(statusMode.subOrder.goodImg);
                myViewHolder.goodms.setText(statusMode.makeMs);
                myViewHolder.progress_bar.setProgress(statusMode.makeProcess);
                ;
                myViewHolder.goodStatus.setText(statusMode.makeStatus.toString());


                myViewHolder.pllc.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
                myViewHolder.adapter = new pflc_adapter(context, statusMode.recipes);
                myViewHolder.pllc.setAdapter(myViewHolder.adapter);
                if (statusMode.makeStatus == MakeStatus.等待制作) {
                    myViewHolder.goodStatus.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.dd));
                } else if (statusMode.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));
                }
                if (statusMode.makeStatus == MakeStatus.等待制作) {
                    myViewHolder.start_button.setVisibility(View.VISIBLE);
                    myViewHolder.goodStatus.setVisibility(View.GONE);
                } else {
                    myViewHolder.start_button.setVisibility(View.GONE);
                    myViewHolder.goodStatus.setVisibility(View.VISIBLE);
                }
                myViewHolder.start_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

//                        if (!DataBus.getInstance().PlcIsConnect) {
//                            T.show(view.getContext(), "PLC未连接,不允许下单！");
//                            return;
//                        }

                        if (BusinessServer.Get().Commoditys.size() > 0) {
                            T.show(view.getContext(), "请等待前一饮品制作完成！");
                            return;
                        }
                        BusinessServer.Get().AddCommodity(statusMode.subOrder);
                        T.show(view.getContext(), "开始制作商品：" + statusMode.good.name.toString());
                    }
                });
            }
        } 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 Modes.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        /**
         * 标题
         */
        TextView title;
        /**
         * 图片
         */
        MyImageView image;
        /**
         * 开始按钮
         */
        Button start_button;
        /**
         * 显示状态
         */
        TextView goodStatus;
        /**
         * 商品描述
         * 当前配置奶茶中..请稍后...
         */
        TextView goodms;
        /**
         * 制作进度
         */
        ProgressBar progress_bar;
        pflc_adapter adapter;
        RecyclerView pllc;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            goodms = (TextView) view.findViewById(R.id.goodms);
            progress_bar = (ProgressBar) view.findViewById(R.id.progress_bar);
            image = (MyImageView) view.findViewById(R.id.image);
            start_button = (Button) view.findViewById(R.id.start_button);
            goodStatus = (TextView) view.findViewById(R.id.start_text);
            pllc = view.findViewById(R.id.pllc);
        }
    }
}