package w.soulofpluto.posstock;

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

/*
 Create by Ramesh Babu
 */

public class PlanDetailsAdapter extends RecyclerView.Adapter<PlanDetailsAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Plans> arrayList;

    public PlanDetailsAdapter(Context context, ArrayList<Plans> storesArrayList) {
        this.context = context;
        this.arrayList = storesArrayList;
    }
    @NonNull
    @Override
    public PlanDetailsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem = layoutInflater.inflate(R.layout.plansdetails_item, parent, false);
        PlanDetailsAdapter.ViewHolder viewHolder = new PlanDetailsAdapter.ViewHolder(listItem);
        return viewHolder;
    }
    @SuppressLint("ResourceAsColor")
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(@NonNull PlanDetailsAdapter.ViewHolder holder, final int position) {
        final Plans items       =     arrayList.get(position);
        holder.planDetails_barcode.setText(items.getBarcode());
        holder.plandetails_qty    .setText(items.getQty());
        //Log.e("PlanDetails",items.getPlanId());
        if (items.getSetNumber() == 0) {
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
        } else if( items.getSetNumber() == 1){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.white));
        }else if( items.getSetNumber() == 2){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.darker_gray));
        }else if( items.getSetNumber() == 3){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_blue_bright));
        }else if( items.getSetNumber() == 4){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_blue_light));
        }else if( items.getSetNumber() == 5){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_orange_light));
        }else if( items.getSetNumber() == 6){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_purple));
        }else if( items.getSetNumber() == 7){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_red_light));
        }else if( items.getSetNumber() == 8){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.tab_indicator_text));
        }else if( items.getSetNumber() == 9){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_light));
        }else if( items.getSetNumber() == 10){
            holder.background_layout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
        }
        holder.plandetails_delete.setOnClickListener(view -> {
            try {
                if (Constants.isNetworkAvailable(context)) {
                    ((StockOutWordActivity) context).DeleteBarcode(items, position,arrayList);
                } else {
                    Constants.getToast(context, context.getResources().getString(R.string.connection));
                }
            } catch (NullPointerException np) {
                np.printStackTrace();
            }
        });
    }
    @Override
    public int getItemCount() {
        return arrayList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView planDetails_barcode, plandetails_qty;
        public ImageView plandetails_delete;
        public LinearLayout background_layout;
        public ViewHolder(View itemView) {
            super(itemView);
            planDetails_barcode = itemView.findViewById(R.id.plandetails_barcode);
            plandetails_qty     = itemView.findViewById(R.id.plandetails_qty);
            plandetails_delete  = itemView.findViewById(R.id.plandetails_delete);
            background_layout   = itemView.findViewById(R.id.background_layout);
        }
    }
}