package com.tomas.ckd_system.Adapter;

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 com.tomas.ckd_system.ClickRowItemProductionSchedule;
import com.tomas.ckd_system.List_Class.ProductionSchedule_list;
import com.tomas.ckd_system.R;
import com.tomas.ckd_system.lib.ProductionSchedule;

import java.util.List;

public class SchedulePacking_Adapter extends RecyclerView.Adapter<SchedulePacking_Adapter.ViewHolder>{
    private List<ProductionSchedule_list> data;
    public Context context;
    private ClickRowItemProductionSchedule click_row_item;

    public SchedulePacking_Adapter(List<ProductionSchedule_list> dataset, Context context, ClickRowItemProductionSchedule click_row_item) {
        data = dataset;
        this.context = context;
        this.click_row_item = click_row_item;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public LinearLayout layout;
        public TextView txt_pc_no, txt_control_no, txt_dist, txt_po_no, txt_maker_code, txt_maker_name,
                txt_dc_part, txt_part_name, txt_part_color, txt_ttl_qty, txt_act_staging, txt_act_outer, txt_status;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = (LinearLayout) itemView.findViewById(R.id.row_schedule);
            txt_pc_no = (TextView) itemView.findViewById(R.id.txt_pc_no);
            txt_control_no = (TextView) itemView.findViewById(R.id.txt_control_no);
            txt_dist = (TextView) itemView.findViewById(R.id.txt_dist);
            txt_po_no = (TextView) itemView.findViewById(R.id.txt_po_no);
            txt_maker_code = (TextView)itemView.findViewById(R.id.txt_m_code);
            txt_maker_name = (TextView)itemView.findViewById(R.id.txt_m_name);
            txt_dc_part = (TextView) itemView.findViewById(R.id.txt_dc_part);
            txt_part_name = (TextView) itemView.findViewById(R.id.txt_part_name);
            txt_part_color = (TextView) itemView.findViewById(R.id.txt_part_color);
            txt_ttl_qty = (TextView) itemView.findViewById(R.id.txt_ttl_qty);
//            txt_act_receive = (TextView) itemView.findViewById(R.id.txt_act_receive);
            txt_act_staging = (TextView) itemView.findViewById(R.id.txt_act_staging);
            txt_act_outer = (TextView) itemView.findViewById(R.id.txt_act_outer);
            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_schedule_packing,parent,false);
        context = parent.getContext();
        return new ViewHolder(content_view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final ProductionSchedule_list list = data.get(position);
        holder.txt_pc_no.setText(list.getPc_no());
        holder.txt_control_no.setText(list.getControl_no());
        holder.txt_po_no.setText(list.getPo_no());
        holder.txt_part_name.setText(list.getPart_name());
        holder.txt_dist.setText(list.getDist());
        holder.txt_dc_part.setText(list.getDc_part_no());
        holder.txt_part_color.setText(list.getPart_color());
        holder.txt_ttl_qty.setText(list.getTtl_qty());
//        holder.txt_act_receive.setText(list.getReceive_act_qty());
        holder.txt_act_staging.setText(list.getStaging_act_qty());
        holder.txt_act_outer.setText(list.getOuter_act_qty());
        holder.txt_maker_code.setText(list.getMaker_code());
        holder.txt_maker_name.setText(list.getMaker_name());
        holder.txt_status.setText(list.getStatus());

        String status = list.getStatus();
        if (status.trim().equals(ProductionSchedule.STATUS.IMPORTED.toString())){
            holder.txt_status.setBackgroundResource(R.drawable.button_backgroup_line_white);
        }else if (list.getStatus().trim().equals("NG")){
            holder.txt_status.setBackgroundResource(R.drawable.button_backgroup_line_red);
        }else {
            holder.txt_status.setBackgroundResource(R.drawable.button_backgroup_line_green);
        }


        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click_row_item.SelectionItemRow(list.getReceive_pending(), list.getReceive_error(),
                        list.getStaging_pending(), list.getStaging_error(), list.getOuter_pending(),
                        list.getOuter_error(), list.getOuter_stopcase());
            }
        });
    }

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