package com.tomas.ckd_system.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.Staging_list;
import com.tomas.ckd_system.R;
import com.tomas.ckd_system.Staging_Activity_PC;
import com.tomas.ckd_system.lib.Settings;

import java.util.List;

public class Staging_Adapter extends RecyclerView.Adapter<Staging_Adapter.ViewHolder> {
    private List<Staging_list>data;
    public Context context;
    String  status_position, line_no, outer_plan_dt;
    int Request = 2001;

    public Staging_Adapter(List<Staging_list> data, Context context, String status_position, String line_no,String outer_plan_dt) {
        this.data = data;
        this.context = context;
        this.status_position = status_position;
        this.line_no = line_no;
        this.outer_plan_dt = outer_plan_dt;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public LinearLayout layout;
        public TextView txt_no, txt_dest, txt_model, txt_type, txt_pc_no, txt_receive, txt_setpart;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = (LinearLayout) itemView.findViewById(R.id.row_staging);
            txt_no = (TextView) itemView.findViewById(R.id.txt_no);
            txt_dest = (TextView) itemView.findViewById(R.id.txt_dest);
            txt_model = (TextView) itemView.findViewById(R.id.txt_model);
            txt_type = (TextView) itemView.findViewById(R.id.txt_type);
            txt_pc_no = (TextView) itemView.findViewById(R.id.txt_pc_no);
            txt_receive = (TextView) itemView.findViewById(R.id.txt_receive);
            txt_setpart = (TextView) itemView.findViewById(R.id.txt_packing);

        }
    }


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

    @Override
    public void onBindViewHolder(@NonNull Staging_Adapter.ViewHolder holder, int position) {
        final Staging_list list = data.get(position);
        holder.txt_no.setText(list.getId());
        holder.txt_dest.setText(list.getDist());
        holder.txt_model.setText(list.getModel_no());
        holder.txt_type.setText(list.getModel_type());
        holder.txt_pc_no.setText(list.getPc_no());
        holder.txt_receive.setText(list.getReceive_act_qty());
        holder.txt_setpart.setText(list.getStaging_act_qty());

        int received_ack, received_plan, staging_ack, staging_plan;
        received_ack = Integer.parseInt(list.getReceive_act_qty());
        received_plan = Integer.parseInt(list.getReceive_plan_qty());
        staging_ack = Integer.parseInt(list.getStaging_act_qty());
        staging_plan = Integer.parseInt(list.getStaging_plan_qty());


        if (received_ack == 0){
            holder.txt_receive.setBackgroundResource(R.drawable.button_backgroup_line_red);
        }else if (received_ack == received_plan){
            holder.txt_receive.setBackgroundResource(R.drawable.button_backgroup_line_green);
        }else {
            holder.txt_receive.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
        }

        if (staging_ack == 0){
            holder.txt_setpart.setBackgroundResource(R.drawable.button_backgroup_line_red);
        }else if (staging_ack == staging_plan){
            holder.txt_setpart.setBackgroundResource(R.drawable.button_backgroup_line_green);
        }else {
            holder.txt_setpart.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
        }

        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(context.getApplicationContext(), Staging_Activity_PC.class);
                i.putExtra("pc_no", list.getPc_no().trim());
                i.putExtra("outer_plan_date", outer_plan_dt);
                i.putExtra("dist", list.getDist().trim());
                i.putExtra("model_no", list.getModel_no().trim());
                i.putExtra("model_type", list.getModel_type().trim());
                i.putExtra("status", status_position);
                i.putExtra("line_no",line_no);

//                context.startActivity(i);
                ((Activity) context).startActivityForResult(i,200);
            }
        });
    }

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