package w.soulofpluto.posstock;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

 /*
  Create by Ramesh Babu
 */

 public class StockReceivedAdapter extends RecyclerView.Adapter<StockReceivedAdapter.ViewHolder> {
    public ArrayList<BoxInfo> arrayList;
    public Context context;

    public StockReceivedAdapter(Context context, ArrayList<BoxInfo> arrayList){
        this.context   = context;
        this.arrayList = arrayList;
    }
    @NonNull
    @Override
    public StockReceivedAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem = layoutInflater.inflate(R.layout.stocktransfer_list_item, parent, false);
        StockReceivedAdapter.ViewHolder viewHolder = new StockReceivedAdapter.ViewHolder(listItem);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull StockReceivedAdapter.ViewHolder holder, int position){
        BoxInfo boxInfo = arrayList.get(position);
        holder.stockTransfer_barcode.setText(boxInfo.getBarcode());
        if(boxInfo.getReceive_qty().replace(".00","").equalsIgnoreCase(boxInfo.getTransfer_qty().replace(".00",""))) {
           holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
           holder.stockReceived_sent_receive.setText(boxInfo.getReceive_qty().replace(".00", "") + "/" + boxInfo.getTransfer_qty().replace(".00", ""));
        }else{
           if(boxInfo.getStatus()){
              holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
           }else{
              holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
           }
           holder.stockReceived_sent_receive.setText(boxInfo.getReceive_qty().replace(".00", "") + "/" + boxInfo.getTransfer_qty().replace(".00", ""));
        }
    }

    @Override
    public int getItemCount(){
       return arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
         TextView stockTransfer_barcode,stockReceived_sent_receive;
         LinearLayout background_layout;
         public ViewHolder(@NonNull View itemView){
           super(itemView);
           stockTransfer_barcode      =  itemView.findViewById(R.id.stockReceived_barcode);
           stockReceived_sent_receive =  itemView.findViewById(R.id.stockReceived_sent_receive);
           background_layout          =  itemView.findViewById(R.id.background_layout);
         }
    }
}
