package com.joe.print.mvp.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.gingersoft.gsa.cloud.print.bean.OrderDetails;
import com.joe.print.R;

import java.math.BigDecimal;
import java.util.List;

import static com.qmuiteam.qmui.util.QMUIDisplayHelper.dp2px;

public class OtherOrderAdapter extends BaseQuickAdapter<OrderDetails.DataBean.PRODUCTNAMEBean, BaseViewHolder> {
    private Context context;
    private int indentation;
    private boolean isShowPrice;

    public OtherOrderAdapter(Context context, @Nullable List<OrderDetails.DataBean.PRODUCTNAMEBean> data, boolean isShowPrice) {
        super(R.layout.print_other_order_food_item, data);
        this.context = context;
        this.isShowPrice = isShowPrice;
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderDetails.DataBean.PRODUCTNAMEBean item) {
        helper.setText(R.id.tv_food_name, item.getPRODUCT_NAME());
        helper.setText(R.id.tv_food_number, item.getNum());
        helper.setText(R.id.tv_food_price, item.getPRICE());
        helper.getView(R.id.tv_food_price).setVisibility(isShowPrice ? View.VISIBLE : View.GONE);
        if (helper.getAdapterPosition() == 0) {
            indentation = new BigDecimal(((TextView) helper.getView(R.id.tv_food_name)).getPaint().measureText("哈")).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        }

        if (item.getChild() != null && item.getChild().size() > 0) {
            LinearLayout childLayout = helper.getView(R.id.layout_food_content);
            for (OrderDetails.DataBean.PRODUCTNAMEBean.ChildBeanX childBeanX : item.getChild()) {
                View secondView = getChild(childBeanX.getPRODUCT_NAME(), childBeanX.getNum(), childBeanX.getPRICE(), indentation);
                childLayout.addView(secondView);
                if (childBeanX.getChild() != null && childBeanX.getChild().size() > 0) {
                    LinearLayout secondLayout = secondView.findViewById(R.id.layout_food_content);
                    for (OrderDetails.DataBean.PRODUCTNAMEBean.ChildBeanX.ChildBean childBean : childBeanX.getChild()) {
                        secondLayout.addView(getChild(childBean.getPRODUCT_NAME(), childBean.getNum(), childBean.getPRICE(), indentation * 2));
                    }
                }
            }

        }
    }

    private View getChild(String foodName, String foodNum, String price, int leftMargin) {
        View view = LayoutInflater.from(context).inflate(R.layout.print_other_order_food_item, null, false);
        ImageView ivNext = view.findViewById(R.id.iv_food_item_next);
        ivNext.setPadding(dp2px(context, leftMargin), 0, 0, 0);
        ((TextView) view.findViewById(R.id.tv_food_name)).setText(foodName);
        ((TextView) view.findViewById(R.id.tv_food_number)).setText(foodNum);
//        ((TextView) view.findViewById(R.id.tv_food_price)).setText(price);
        return view;
    }


}
