package com.tomas.ckd_system.Adapter;

import android.content.Context;
import android.graphics.Color;
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 com.tomas.ckd_system.List_Class.Receiving_list;
import com.tomas.ckd_system.R;

import java.util.List;

public class Receiving_Adapter extends RecyclerView.Adapter<Receiving_Adapter.ViewHolder> {
    private List<Receiving_list> data;
    public Context context;


    public Receiving_Adapter(List<Receiving_list> dataset, Context context) {
        data = dataset;
        this.context = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout layout;
        public TextView txt_id, txt_code_maker, txt_maker_name, txt_part_no, txt_part_name, txt_color, txt_date,
                txt_time, txt_po_no, txt_qty, txt_receive, txt_balance, txt_rec_time, txt_status;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = (LinearLayout) itemView.findViewById(R.id.row_receiving);
            txt_id = (TextView) itemView.findViewById(R.id.txt_id);
            txt_code_maker = (TextView) itemView.findViewById(R.id.txt_code_maker);
            txt_maker_name = (TextView) itemView.findViewById(R.id.txt_maker_name);
            txt_part_no = (TextView) itemView.findViewById(R.id.txt_part_no);
            txt_part_name = (TextView) itemView.findViewById(R.id.txt_part_name);
            txt_color = (TextView) itemView.findViewById(R.id.txt_color);
            txt_date = (TextView) itemView.findViewById(R.id.txt_date);
            txt_time = (TextView) itemView.findViewById(R.id.txt_time);
            txt_po_no = (TextView) itemView.findViewById(R.id.txt_po_no);
            txt_qty = (TextView) itemView.findViewById(R.id.txt_qty);
            txt_receive = (TextView) itemView.findViewById(R.id.txt_receive);
            txt_balance = (TextView) itemView.findViewById(R.id.txt_balance);
            txt_rec_time = (TextView) itemView.findViewById(R.id.txt_rec_time);
            txt_status = (TextView) itemView.findViewById(R.id.txt_status);


        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View content_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_receiving,parent,false);
        context = parent.getContext();
        return new ViewHolder(content_view);
    }

    @Override
    public void onBindViewHolder(@NonNull Receiving_Adapter.ViewHolder holder, int position) {
        final Receiving_list list = data.get(position);
        holder.txt_id.setText(list.getId());
        holder.txt_code_maker.setText(list.getMaker_code());
        holder.txt_maker_name.setText(list.getMaker_name());
        holder.txt_part_no.setText(list.getPart_no());
        holder.txt_part_name.setText(list.getPart_name());
        holder.txt_color.setText(list.getPart_color());
        holder.txt_date.setText(list.getReceive_plan_date());
        holder.txt_time.setText(list.getReceive_plan_time());
        holder.txt_po_no.setText(list.getPo_no());
        holder.txt_qty.setText(list.getReceive_plan_qty());
        holder.txt_receive.setText(list.getReceive_act_qty());
        holder.txt_balance.setText(list.getBalance());
        holder.txt_rec_time.setText(list.getReceive_act_dt());
        holder.txt_status.setText(list.getReceive_user_id());

        //text color PO.NO / Q.TY
        String step1 = list.getReceive_step1();
        if (step1.trim().equals("")){
            holder.txt_qty.setTextColor(Color.RED);
            holder.txt_po_no.setTextColor(Color.RED);
        }else{
            holder.txt_po_no.setTextColor(Color.parseColor("#2CA531"));
            holder.txt_qty.setTextColor(Color.parseColor("#2CA531"));
        }

        //BG color balance
        int act_qty, plan_qty, balance;
        plan_qty = Integer.parseInt(list.getReceive_plan_qty());//100
        act_qty = Integer.parseInt(list.getReceive_act_qty());//50

        if (plan_qty == 0 && act_qty == 0){
            holder.txt_balance.setBackgroundResource(R.color.color_white);
            holder.txt_status.setBackgroundResource(R.color.color_white);

        }else if (plan_qty == act_qty){
            holder.txt_balance.setBackgroundResource(R.color.color_light_green);
            holder.txt_status.setBackgroundResource(R.color.color_light_green);

        }else if (plan_qty > act_qty && act_qty != 0){
            holder.txt_balance.setBackgroundResource(R.color.color_yel);
            holder.txt_status.setBackgroundResource(R.color.color_yel);

        }
        else {//plan_qty > act_qty && act_qty==0
            holder.txt_balance.setBackgroundResource(R.color.color_red);
            holder.txt_status.setBackgroundResource(R.color.color_white);
        }
    }

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