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

import com.ygtx.emcs.R;
import com.ygtx.emcs.activity.model.MaterialStockModel;
import com.ygtx.emcs.util.Constant;
import com.ygtx.emcs.util.StringUtils;

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

public class StockChangeItemViewAdapter extends BaseAdapter {
    private ArrayList<MaterialStockModel> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public StockChangeItemViewAdapter(ArrayList<MaterialStockModel> 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 MaterialStockModel materialStockModel = mList.get(position);
        if (view == null) {
            view=mLayoutInflater.inflate(R.layout.ygtx_material_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.tv_lock_qty = view.findViewById(R.id.tv_lock_qty);
            mainHolder.tv_box_code = view.findViewById(R.id.tv_box_code);
            mainHolder.tv_area_code = view.findViewById(R.id.tv_area_code);
            mainHolder.tv_batch_code = view.findViewById(R.id.tv_batch_code);
            mainHolder.ll_item = view.findViewById(R.id.ll_item);
            view.setTag(mainHolder);
        }else{
            mainHolder= (MainHolder) view.getTag();
        }
        mainHolder.tv_item_spec.setVisibility(View.VISIBLE);
        mainHolder.tv_item_spec.setText(materialStockModel.getSpecification());
        mainHolder.tv_product_code.setText(materialStockModel.getItemCode());
        if(materialStockModel.getQuantityOnhand() == null){
            materialStockModel.setLockQuantity(BigDecimal.ZERO);
        }
        mainHolder.qty.setText(materialStockModel.getQuantityOnhand().toString());
        mainHolder.tv_product_name.setText(materialStockModel.getItemName());
        if(materialStockModel.getLockQuantity() == null){
            materialStockModel.setLockQuantity(BigDecimal.ZERO);
        }
        mainHolder.tv_lock_qty.setText(materialStockModel.getLockQuantity().toString());
        mainHolder.tv_stock_status.setVisibility(View.GONE);
        mainHolder.tv_box_code.setText(materialStockModel.getAttr1());
        mainHolder.tv_batch_code.setText(materialStockModel.getBatchCode());
        if(StringUtils.isEmpty(materialStockModel.getAreaCode())){
            mainHolder.tv_area_code.setText("不在库位上");
            mainHolder.tv_area_code.setTextColor(Color.RED);
        }else if(Constant.XBK.equals(materialStockModel.getAreaCode())){
            mainHolder.tv_area_code.setText("在线边库");
            mainHolder.tv_area_code.setTextColor(Color.GREEN);
        }else{
            mainHolder.tv_area_code.setText(materialStockModel.getAreaCode());
            mainHolder.tv_area_code.setTextColor(Color.GRAY);
        }
        if("1".equals(materialStockModel.getSel())){
            mainHolder.ll_item.setBackgroundColor(Color.LTGRAY);
        }else{
            mainHolder.ll_item.setBackgroundColor(Color.WHITE);
        }
        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 TextView tv_lock_qty;
        public TextView tv_box_code;
        public TextView tv_area_code;
        public TextView tv_batch_code;
        public LinearLayout ll_item;
    }

}
