package com.example.bpa.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

import com.example.bpa.R;
import com.example.bpa.app.Main;
import com.example.bpa.config.ConfigName;
import com.example.bpa.config.MessageName;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_ORDER;
import com.example.bpa.db.mode.BPA_SUBORDER;
import com.example.bpa.message.MessageManager;
import com.example.bpa.service.BusinessServer;
import com.example.bpa.config.DataBus;
import com.example.bpa.helper.T;
import com.example.bpa.view.mode.mainGoods;

import java.util.List;
import java.util.Random;

public class maingoods_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

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

    public maingoods_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.maingoods, parent, false);
        return new maingoods_adapter.MyLCViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof maingoods_adapter.MyLCViewHolder) {
                maingoods_adapter.MyLCViewHolder myViewHolder = (maingoods_adapter.MyLCViewHolder) holder;
                myViewHolder.goodname.setText(mainGoods.get(position).name);
                myViewHolder.id = mainGoods.get(position).id;
                myViewHolder.url= mainGoods.get(position).url;
                int num = (int) new Random().nextInt(DataBus.getInstance().good_picMap.size());//随机商品图片
                myViewHolder.good_pic.setImageResource(DataBus.getInstance().good_picMap.get(num));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    public static class MyLCViewHolder extends RecyclerView.ViewHolder {
        TextView goodname;

        Button goodmake;
        RelativeLayout makerelative;
        ImageView good_pic;

        public String id;

        public String url;


        public MyLCViewHolder(View view) {
            super(view);
            goodname = (TextView) view.findViewById(R.id.goodname);
            goodmake = (Button) view.findViewById(R.id.goodmake);
            good_pic = (ImageView)view.findViewById(R.id.good_pic);
            makerelative= (RelativeLayout)view.findViewById(R.id.makerelative);

            makerelative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Make(view);
                }
            });
            goodmake.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Make(view);
                }
            });

        }

        /**
         * 制作商品
         * @param view
         */
        public void Make(View view)
        {
            if(!DataBus.getInstance().PlcIsConnect)
            {
              T.show(view.getContext(),"PLC未连接,不允许下单！");
              return;
            }

            if (BusinessServer.Get().Commoditys.size() > 0) {
                T.show(view.getContext(), "请等待前一饮品制作完成！");
                return;
            }

            BPA_ORDER order=new BPA_ORDER();
            order.thirdPartyID="手动下单";
            order.status=0;
            order.deviceID = ConfigName.getInstance().DeviceId;
            order.userID = ConfigName.getInstance().user.userID;
            QueryDB.AddOrder(order);

            BPA_SUBORDER suborder=new BPA_SUBORDER();
            suborder.deviceID = ConfigName.getInstance().DeviceId;
            suborder.userID = ConfigName.getInstance().user.userID;
            suborder.orderID = order.id;
            suborder.goodsID = id;
            suborder.number = 1;
            suborder.status=0;
            suborder.goodImg=url;
            QueryDB.AddSubOrder(suborder);

            MessageManager.getInstance().sendMessage(MessageName.ShowOrderMakeForm.name(),order.id);
            T.show(view.getContext(),"开始制作商品："+goodname.getText().toString());
        }
    }
}
