package com.ygtx.emcs.activity.adapter;

import static com.ygtx.emcs.util.Constant.CACHE_BIND_CONFIG;

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

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

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

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

    public StockInListViewAdapter(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.tv_automatic = view.findViewById(R.id.tv_automatic);
            mainHolder.tv_box_code = view.findViewById(R.id.tv_box_code);
            mainHolder.tv_box_code_tip = view.findViewById(R.id.tv_box_code_tip);
            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_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);
            view.setTag(mainHolder);
        }else{
            mainHolder= (MainHolder) view.getTag();
        }
        String box_flag = CacheService.getInstance(mContext).getCache(CACHE_BIND_CONFIG, "box_flag");
        if("false".equals(box_flag)){
            mainHolder.tv_box_code.setVisibility(View.GONE);
            mainHolder.tv_box_code_tip.setVisibility(View.GONE);
        }
        mainHolder.tv_item_spec.setVisibility(View.VISIBLE);
        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());
        if("1".equals(stockLineModel.getAutomatic())){
            mainHolder.tv_automatic.setText("自动库");
            mainHolder.tv_automatic.setTextColor(Color.GREEN);
            mainHolder.tv_box_code.setText(stockLineModel.getBoxCode());
        }else if("3".equals(stockLineModel.getAutomatic())){
            mainHolder.tv_automatic.setText("其他库");
            mainHolder.tv_automatic.setTextColor(Color.RED);
            mainHolder.tv_box_code.setVisibility(View.GONE);
            mainHolder.tv_box_code_tip.setVisibility(View.GONE);
        }else if("2".equals(stockLineModel.getAutomatic())){
            mainHolder.tv_automatic.setText("没库存");
            mainHolder.tv_automatic.setTextColor(Color.YELLOW);
        }else{
            mainHolder.tv_automatic.setText("手动库");
            mainHolder.tv_automatic.setTextColor(Color.BLUE);
            mainHolder.tv_box_code.setVisibility(View.GONE);
            mainHolder.tv_box_code_tip.setVisibility(View.GONE);
        }
        if("1".equals(stockLineModel.getSel())){
            mainHolder.ll_item_spec.setBackgroundColor(Color.LTGRAY);
            mainHolder.ll_product_name.setBackgroundColor(Color.LTGRAY);
            mainHolder.ll_product_code.setBackgroundColor(Color.LTGRAY);
        }else{
            mainHolder.ll_item_spec.setBackgroundColor(Color.WHITE);
            mainHolder.ll_product_name.setBackgroundColor(Color.WHITE);
            mainHolder.ll_product_code.setBackgroundColor(Color.WHITE);
        }
        if("2".equals(stockLineModel.getStatus())){
            mainHolder.tv_stock_status.setTextColor(Color.BLUE);
            mainHolder.tv_stock_status.setText("已入库");
        }else{
            mainHolder.tv_stock_status.setTextColor(Color.RED);
            mainHolder.tv_stock_status.setText("待入库");
        }
        BigDecimal orderQty = stockLineModel.getOrderQuantity();
        if(orderQty == null){
            stockLineModel.setOrderQuantity(BigDecimal.ZERO);
        }
        mainHolder.tv_other_num_tips.setText("总量：");
        mainHolder.tv_other_num.setText(stockLineModel.getOrderQuantity().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 TextView tv_automatic;
        public TextView tv_box_code;
        public TextView tv_box_code_tip;
        public TextView tv_other_num;
        public TextView tv_other_num_tips;
        public LinearLayout ll_item_spec;
        public LinearLayout ll_product_name;
        public LinearLayout ll_product_code;
    }

}
