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.Outer_list;
import com.tomas.ckd_system.ClickRowItemOuter;
import com.tomas.ckd_system.R;

import java.util.List;

public class Outer_Adapter extends RecyclerView.Adapter<Outer_Adapter.ViewHolder> {
    private ClickRowItemOuter onClickRowItem;
    private List<Outer_list> data;
    public Context context;
    String pc_no, control_no;

    public Outer_Adapter(List<Outer_list> dataset, Context context, ClickRowItemOuter onClickRowItem) {
        data = dataset;
        this.context = context;
        this.onClickRowItem = onClickRowItem;
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout layout;
        public TextView txt_date, txt_pc_no, txt_model, txt_dest, txt_control, txt_cont, txt_vanning,
                txt_start_time, txt_finish_time, txt_receive, txt_set_part, txt_inner, txt_outer, txt_sub_leader,
                txt_leader, txt_result;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = (LinearLayout) itemView.findViewById(R.id.row_outer);
            txt_date = (TextView) itemView.findViewById(R.id.txt_date);
            txt_pc_no = (TextView) itemView.findViewById(R.id.txt_pc_no);
            txt_model = (TextView) itemView.findViewById(R.id.txt_model);
            txt_dest = (TextView) itemView.findViewById(R.id.txt_dest);
            txt_control = (TextView) itemView.findViewById(R.id.txt_control_no);
            txt_cont = (TextView) itemView.findViewById(R.id.txt_cont);
            txt_date = (TextView) itemView.findViewById(R.id.txt_date);
            txt_vanning = (TextView) itemView.findViewById(R.id.txt_vanning);
            txt_start_time = (TextView) itemView.findViewById(R.id.txt_start_time);
            txt_finish_time = (TextView) itemView.findViewById(R.id.txt_finish_time);
            txt_receive = (TextView) itemView.findViewById(R.id.txt_receive);
            txt_set_part = (TextView) itemView.findViewById(R.id.txt_set_part);
            txt_inner = (TextView) itemView.findViewById(R.id.txt_inner);
            txt_outer = (TextView) itemView.findViewById(R.id.txt_outer);
            txt_sub_leader = (TextView) itemView.findViewById(R.id.txt_sub_leader);
            txt_leader = (TextView) itemView.findViewById(R.id.txt_leader);
            txt_result = (TextView) itemView.findViewById(R.id.txt_result);

        }
    }

    @Override
    public void onBindViewHolder(@NonNull Outer_Adapter.ViewHolder holder, int position) {

        final  Outer_list list = data.get(position);

        pc_no = list.getPc_no();
        pc_no = pc_no.substring(6);
        control_no = list.getControl_no();

        holder.txt_date.setText(list.getOuter_plan_date());
        holder.txt_pc_no.setText(pc_no);
        holder.txt_model.setText(list.getModel_no());
        holder.txt_dest.setText(list.getDist());
        holder.txt_control.setText(control_no+"/"+list.getCase_no());
        holder.txt_cont.setText(list.getCont_no());
        holder.txt_vanning.setText(list.getVanning_plan_date());
        holder.txt_start_time.setText(list.getOuter_start_dt());
        holder.txt_finish_time.setText(list.getOuter_act_dt());
        holder.txt_receive.setText(list.getReceive_user_id());
        holder.txt_set_part.setText(list.getStaging_user_id());
        holder.txt_inner.setText(list.getInner_user_id());
        holder.txt_outer.setText(list.getOuter_user_id());
        holder.txt_sub_leader.setText(list.getOuter_subl_cf_user_id());
        holder.txt_leader.setText(list.getOuter_lead_cf_user_id());

        String result = list.getResult();
        if (result.trim().equals("")){
            holder.txt_result.setText("");
        }else {
            holder.txt_result.setText("1 / "+result);
        }

       int receive_act, receive_plan, staging_act, staging_plan, inner_plan, inner_act, outer_plan, outer_ack;
       receive_act = Integer.parseInt(list.getReceive_act_qty());
       receive_plan = Integer.parseInt(list.getReceive_plan_qty());
       staging_plan = Integer.parseInt(list.getStaging_plan_qty());
       staging_act = Integer.parseInt(list.getStaging_act_qty());
       inner_plan = Integer.parseInt(list.getInner_plan_qty());
       inner_act = Integer.parseInt(list.getInner_act_qty());
       outer_plan = Integer.parseInt(list.getOuter_plan_qty());
       outer_ack = Integer.parseInt(list.getOuter_act_qty());

       String status = list.getStatus();
       String stage = list.getStage();

        //Receive
        if (receive_act == 0){
            holder.txt_receive.setBackgroundResource(R.drawable.button_backgroup_layout);
            holder.txt_receive.setTextColor(Color.BLACK);
        }else if (receive_act == receive_plan){
            holder.txt_receive.setBackgroundResource(R.drawable.button_backgroup_line_green);
            holder.txt_receive.setTextColor(Color.BLACK);
        }else {
            holder.txt_receive.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
            holder.txt_receive.setTextColor(Color.BLACK);
        }

        //set part
        if (staging_act == 0){
            holder.txt_set_part.setBackgroundResource(R.drawable.button_backgroup_layout);
            holder.txt_set_part.setTextColor(Color.BLACK);
        }else if (staging_act == staging_plan){
            holder.txt_set_part.setBackgroundResource(R.drawable.button_backgroup_line_green);
            holder.txt_set_part.setTextColor(Color.BLACK);
        }else {
            holder.txt_set_part.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
            holder.txt_set_part.setTextColor(Color.BLACK);
        }

        //inner
        if (inner_act == 0){
            holder.txt_inner.setBackgroundResource(R.drawable.button_backgroup_layout);
            holder.txt_inner.setTextColor(Color.BLACK);
        }else if (inner_act == inner_plan){
            holder.txt_inner.setBackgroundResource(R.drawable.button_backgroup_line_green);
            holder.txt_inner.setTextColor(Color.BLACK);
        }else {
            holder.txt_inner.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
            holder.txt_inner.setTextColor(Color.BLACK);
        }


        //outer
        if (status.trim().equals("STOP_CASE") || status.trim().equals("NG") || status.trim().equals("PENDING")){
            holder.txt_outer.setBackgroundResource(R.drawable.button_backgroup_line_red);
            holder.txt_outer.setTextColor(Color.WHITE);
        }else if (status.trim().equals("WORKING") && stage.trim().equals("OUTER_PACKING")){
            holder.txt_outer.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
            holder.txt_outer.setTextColor(Color.BLACK);
//        }else if (status.trim().equals("OUTER_PACKED")||status.trim().equals("OUTER Complete")){
//            holder.txt_outer.setBackgroundResource(R.drawable.button_backgroup_line_green);
//            holder.txt_outer.setTextColor(Color.BLACK);
        }else if (status.trim().equals("CANCELED")){
            holder.txt_outer.setBackgroundResource(R.drawable.button_backgroup_line_gray);
            holder.txt_outer.setTextColor(Color.BLACK);
        } else if (outer_ack == 0){
            holder.txt_outer.setBackgroundResource(R.drawable.button_backgroup_layout);
            holder.txt_outer.setTextColor(Color.BLACK);
        }else if (outer_ack == outer_plan){
            holder.txt_outer.setBackgroundResource(R.drawable.button_backgroup_line_green);
            holder.txt_outer.setTextColor(Color.BLACK);
        }else {
            holder.txt_outer.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
            holder.txt_outer.setTextColor(Color.BLACK);
        }


        String sub_leader = list.getOuter_subl_cf_user_id();
        if (sub_leader.trim().equals("")){
            holder.txt_sub_leader.setBackgroundResource(R.drawable.button_backgroup_layout);
            holder.txt_sub_leader.setTextColor(Color.BLACK);
        }else {
            holder.txt_sub_leader.setBackgroundResource(R.drawable.button_backgroup_line_green);
            holder.txt_sub_leader.setTextColor(Color.BLACK);
        }

        String leader = list.getOuter_lead_cf_user_id();
        if (leader.trim().equals("")){
            holder.txt_leader.setBackgroundResource(R.drawable.button_backgroup_layout);
            holder.txt_leader.setTextColor(Color.BLACK);
        }else {
            holder.txt_leader.setBackgroundResource(R.drawable.button_backgroup_line_green);
            holder.txt_leader.setTextColor(Color.BLACK);
        }

        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickRowItem.SelectionItemRowOuter(
                        list.getPc_no(),
                        list.getControl_no(),
                        list.getCase_no(),
                        list.getOuter_lead_cf_user_id().trim(),
                        list.getOuter_subl_cf_user_id().trim(),
                        list.getOuter_act_qty(),
                        list.getOuter_plan_qty(),
                        list.getStatus());
            }
        });
    }

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


}
