package com.philisense.rdgo.view;

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

import com.philisense.rdgo.R;
import com.unnamed.b.atv.model.TreeNode;

/**
 * Created by shaop on 2018/1/12.
 */

public class OrderInViewHolder extends TreeNode.BaseNodeViewHolder<OrderInViewHolder.OrderInfo> {

    public OrderInViewHolder(Context context) {
        super(context);
    }

    private OrderInViewHolder.AddListenner listenner;
    private TextView titleTextView;
    private TextView maxNumTextView;
    private ImageButton addButton;
    private TextView numTextView;
    private ImageView stateImageView;
    private OrderInViewHolder.OrderInfo data;

    @Override
    public View createNodeView(TreeNode node, OrderInViewHolder.OrderInfo value) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final View view = inflater.inflate(R.layout.item_put_out_order_detail, null, false);

        data = value;

        titleTextView = view.findViewById(R.id.order_oil_type);
        maxNumTextView = view.findViewById(R.id.order_max_num);
        numTextView= view.findViewById(R.id.order_num);
        stateImageView = view.findViewById(R.id.order_state_image);
        addButton = view.findViewById(R.id.order_add_button);

        titleTextView.setText(value.getTypeName());
        maxNumTextView.setText(value.getMaxBoxNum()+"箱");
        numTextView.setText(value.getBoxNum()+"箱");

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listenner.add(data);
            }
        });

        return view;
    }

    public void updateData(OrderInViewHolder.OrderInfo info) {
        if (info.getBoxNum() == 0) {
            stateImageView.setSelected(true);
            numTextView.setText("满箱");
        }else {
            numTextView.setText(info.getBoxNum()+"箱");
        }
    }

    public void addBox(int number) {
        data.setBoxNum(data.getMaxBoxNum() - number);
        if (data.getBoxNum() <= 0) {
            stateImageView.setSelected(true);
            numTextView.setText("满箱");
        }else {
            numTextView.setText(data.getBoxNum()+"箱");
        }
    }

    public void removeBox() {
        data.setBoxNum(data.getBoxNum() + 1);
        stateImageView.setSelected(false);
        numTextView.setText(data.getBoxNum()+"箱");
    }

    public OrderInViewHolder.OrderInfo getData() {
        return data;
    }

    public void setListenner(OrderInViewHolder.AddListenner listenner) {
        this.listenner = listenner;
    }

    public interface AddListenner {
        public void add(OrderInViewHolder.OrderInfo typeName);
    }

    public static class OrderInfo {
        private String type;
        private String orderId;
        private String orderChildId;
        private String typeName;
        private String typeCode;
        private String sendChildId;
        private String sendId;
        private int maxBoxNum;
        private int BoxNum;

        public OrderInfo(String type,String orderChildId, String orderId, String sendChildId, String sendId, String typeName, String typeCode, int maxBoxNum, int boxNum) {
            this.type = type;
            this.sendId = sendId;
            this.sendChildId = sendChildId;
            this.orderChildId = orderChildId;
            this.orderId = orderId;
            this.typeName = typeName;
            this.typeCode = typeCode;
            this.maxBoxNum = maxBoxNum;
            BoxNum = boxNum;

        }

        public String getTypeName() {
            return typeName;
        }

        public void setTypeName(String typeName) {
            this.typeName = typeName;
        }

        public String getTypeCode() {
            return typeCode;
        }

        public void setTypeCode(String typeCode) {
            this.typeCode = typeCode;
        }

        public String getOrderChildId() {
            return orderChildId;
        }

        public void setOrderChildId(String orderChildId) {
            this.orderChildId = orderChildId;
        }

        public String getSendChildId() {
            return sendChildId;
        }

        public void setSendChildId(String sendChildId) {
            this.sendChildId = sendChildId;
        }

        public String getSendId() {
            return sendId;
        }

        public void setSendId(String sendId) {
            this.sendId = sendId;
        }

        public String getOrderId() {
            return orderId;
        }

        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public int getMaxBoxNum() {
            return maxBoxNum;
        }

        public void setMaxBoxNum(int maxBoxNum) {
            this.maxBoxNum = maxBoxNum;
        }

        public int getBoxNum() {
            return BoxNum;
        }

        public void setBoxNum(int boxNum) {
            BoxNum = boxNum;
        }
    }
}
