package com.ygtx.emcs.activity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.ygtx.emcs.R;
import com.ygtx.emcs.activity.model.StockLineModel;
import com.ygtx.emcs.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;

public class StockOutListViewAdapter extends BaseAdapter {
    private ArrayList<StockLineModel> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public StockOutListViewAdapter(ArrayList<StockLineModel> list, Context context) {
        this.mList=list;
        this.mContext=context;
        this.mLayoutInflater=LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(int position, View view, ViewGroup parent) {
        MainHolder mainHolder = null;
        final StockLineModel stockLineModel = mList.get(position);
        if (view == null) {
            view=mLayoutInflater.inflate(R.layout.ygtx_stock_product_item,null);
            mainHolder = new MainHolder();
            mainHolder.tv_item_spec = view.findViewById(R.id.tv_item_spec);
            mainHolder.tv_product_code = view.findViewById(R.id.tv_product_code);
            mainHolder.qty = view.findViewById(R.id.tv_qty);
            mainHolder.tv_product_name = view.findViewById(R.id.tv_product_name);
            mainHolder.tv_stock_status = view.findViewById(R.id.tv_stock_status);
            mainHolder.et_per_bag = view.findViewById(R.id.et_per_bag);
            mainHolder.tv_automatic = view.findViewById(R.id.tv_automatic);
            mainHolder.tv_box_code = view.findViewById(R.id.tv_box_code);
            mainHolder.ll_item_spec = view.findViewById(R.id.ll_item_spec);
            mainHolder.ll_product_name = view.findViewById(R.id.ll_product_name);
            mainHolder.ll_product_code = view.findViewById(R.id.ll_product_code);
            mainHolder.tv_other_num = view.findViewById(R.id.tv_other_num);
            mainHolder.tv_other_num_tips = view.findViewById(R.id.tv_other_num_tips);
            mainHolder.ll_stock_type = view.findViewById(R.id.ll_stock_type);
            mainHolder.ll_other_num = view.findViewById(R.id.ll_other_num);
            view.setTag(mainHolder);
        }else{
            mainHolder= (MainHolder) view.getTag();
        }
        if("2".equals(stockLineModel.getStatus())){
            mainHolder.tv_stock_status.setTextColor(Color.BLUE);
            if("1".equals(stockLineModel.getAutomatic())){
                if("1".equals(stockLineModel.getOutCheck())){
                    mainHolder.tv_stock_status.setText("已出库");
                }else{
                    mainHolder.tv_stock_status.setText("待核对");
                }
            }else{
                mainHolder.tv_stock_status.setText("已出库");
            }
        }else if("1".equals(stockLineModel.getStatus())){
            mainHolder.tv_stock_status.setTextColor(Color.GREEN);
            mainHolder.tv_stock_status.setText("在出库");
        }else{
            mainHolder.tv_stock_status.setTextColor(Color.RED);
            mainHolder.tv_stock_status.setText("待出库");
        }
        if("1".equals(stockLineModel.getAutomatic())){
            mainHolder.tv_automatic.setText("自动库");
            mainHolder.tv_automatic.setTextColor(Color.GREEN);
        }else if("3".equals(stockLineModel.getAutomatic())){
            mainHolder.tv_automatic.setText("其他库");
            mainHolder.tv_automatic.setTextColor(Color.RED);
            mainHolder.tv_box_code.setText(stockLineModel.getBoxCode());
        }else if("2".equals(stockLineModel.getAutomatic())){
            mainHolder.tv_automatic.setText("没库存");
            mainHolder.tv_automatic.setTextColor(Color.YELLOW);
            mainHolder.tv_box_code.setText(stockLineModel.getBoxCode());
        }else{
            mainHolder.tv_automatic.setText("手动库");
            mainHolder.tv_automatic.setTextColor(Color.BLUE);
        }
        if("1".equals(stockLineModel.getSel())){
            mainHolder.ll_product_code.setBackgroundColor(Color.LTGRAY);
            mainHolder.ll_item_spec.setBackgroundColor(Color.LTGRAY);
            mainHolder.ll_product_name.setBackgroundColor(Color.LTGRAY);
            mainHolder.ll_stock_type.setBackgroundColor(Color.LTGRAY);
            mainHolder.ll_other_num.setBackgroundColor(Color.LTGRAY);
        }else{
            mainHolder.ll_product_code.setBackgroundColor(Color.WHITE);
            mainHolder.ll_item_spec.setBackgroundColor(Color.WHITE);
            mainHolder.ll_product_name.setBackgroundColor(Color.WHITE);
            mainHolder.ll_stock_type.setBackgroundColor(Color.WHITE);
            mainHolder.ll_other_num.setBackgroundColor(Color.WHITE);
        }
        mainHolder.tv_item_spec.setText(stockLineModel.getSpecification());
        mainHolder.tv_product_code.setText(stockLineModel.getItemCode());
        mainHolder.qty.setText(stockLineModel.getQuantity());
        mainHolder.tv_product_name.setText(stockLineModel.getItemName());
        mainHolder.tv_box_code.setText(stockLineModel.getBoxCode());
        mainHolder.tv_other_num_tips.setText("剩余：");
        BigDecimal boxQty = stockLineModel.getBoxItemQuantity();
        if(boxQty == null){
            stockLineModel.setBoxItemQuantity(BigDecimal.ZERO);
        }
        mainHolder.tv_other_num.setText(stockLineModel.getBoxItemQuantity().toString());
        return view;
    }

    static class MainHolder {
        public TextView tv_product_code;
        public TextView qty;
        public TextView tv_item_spec;
        public TextView tv_product_name;
        public TextView tv_stock_status;
        public EditText et_per_bag;
        public TextView tv_automatic;
        public TextView tv_box_code;
        public LinearLayout ll_item_spec;
        public LinearLayout ll_product_name;
        public LinearLayout ll_product_code;
        public LinearLayout ll_stock_type;
        public LinearLayout ll_other_num;
        public TextView tv_other_num;
        public TextView tv_other_num_tips;
    }

}
