package com.tomas.ckd_system.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tomas.ckd_system.ClickRowItemStaging;
import com.tomas.ckd_system.List_Class.Staging_detail_list;
import com.tomas.ckd_system.ProductionSchedule_Activity;
import com.tomas.ckd_system.R;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class Staging_Adapter_Detail extends RecyclerView.Adapter<Staging_Adapter_Detail.ViewHolder> {
    private List<Staging_detail_list>data;
    private ClickRowItemStaging clickRowItemStaging;
    public Context context;

    public Staging_Adapter_Detail(List<Staging_detail_list> data, Context context, ClickRowItemStaging clickRowItemStaging) {
        this.data = data;
        this.context = context;
        this.clickRowItemStaging = clickRowItemStaging;
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        public LinearLayout layout;
        public TextView txt_id, txt_part_no, txt_part_name, txt_color, txt_maker_code, txt_maker_name, txt_control, txt_qty,
                        txt_set_part_time, txt_status, txt_packing_seq, txt_case ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = (LinearLayout) itemView.findViewById(R.id.row_staging_detail);
            txt_id = (TextView) itemView.findViewById(R.id.txt_id);
            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_maker_code = (TextView) itemView.findViewById(R.id.txt_maker_code);
            txt_maker_name = (TextView) itemView.findViewById(R.id.txt_maker_name);
            txt_control = (TextView) itemView.findViewById(R.id.txt_control);
            txt_qty = (TextView) itemView.findViewById(R.id.txt_qty);
            txt_set_part_time = (TextView) itemView.findViewById(R.id.txt_set_part_time);
            txt_status = (TextView) itemView.findViewById(R.id.txt_status);
            txt_packing_seq = (TextView) itemView.findViewById(R.id.txt_packing_seq);
            txt_case = (TextView)itemView.findViewById(R.id.txt_case);
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull Staging_Adapter_Detail.ViewHolder holder, int position) {
        final Staging_detail_list list = data.get(position);
        holder.txt_id.setText(list.getCase_packing_seq());
        holder.txt_part_no.setText(list.getPart_no());
        holder.txt_part_name.setText(list.getPart_name());
        holder.txt_packing_seq.setText(list.getItem_no());
        holder.txt_color.setText(list.getPart_color());
        holder.txt_maker_code.setText(list.getMaker_code());
        holder.txt_maker_name.setText(list.getMaker_name());
        holder.txt_control.setText(list.getControl_no());
        holder.txt_set_part_time.setText(list.getStaging_act_dt());
        holder.txt_status.setText(list.getStaging_user_id());
        holder.txt_case.setText(list.getCase_no());

        int staging_act, staging_plan, receive_act, receive_plan;
        staging_act = Integer.parseInt(list.getStaging_act_qty());
        staging_plan = Integer.parseInt(list.getStaging_plan_qty());
        receive_act = Integer.parseInt(list.getReceive_act_qty());
        receive_plan = Integer.parseInt(list.getReceive_plan_qty());

        holder.txt_qty.setText(String.valueOf(staging_act+"/"+staging_plan));

//        String bg_color = list.getBg_color();
//        if (bg_color.trim().equals("white")){
//            holder.layout.setBackgroundResource(R.color.color_white);
//        }else if (bg_color.trim().equals("blue")){
//            holder.layout.setBackgroundResource(R.color.status_confirmed);
//        }

        if (receive_act != receive_plan ){
            holder.txt_qty.setBackgroundResource(R.drawable.button_backgroup_line_red);
        }else if (staging_act == staging_plan){
            holder.txt_qty.setBackgroundResource(R.drawable.button_backgroup_line_green);
        }else if (receive_act == receive_plan){
            holder.txt_qty.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
        }

        if (staging_act == 0){
            holder.txt_status.setBackgroundResource(R.drawable.button_backgroup_line_red);
        }else if (staging_act >0 && staging_act < staging_plan){
            holder.txt_status.setBackgroundResource(R.drawable.button_backgroup_line_yellow);
        }else {//
            holder.txt_status.setBackgroundResource(R.drawable.button_backgroup_line_green);
        }

        holder.layout.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               String url_meiji_card = list.getUrl_meiji_card().trim();
               String url_packing = list.getUrl_packing_method().trim();

               if (url_meiji_card.trim().equals("")&&url_packing.trim().equals("")){
                   new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
                           .setTitleText("WARNING")
                           .setContentText("Not found Meiji Card and packing method")
                           .show();

               }else {
                   clickRowItemStaging.onClickRow(list.getPc_no(),
                           list.getControl_no(),
                           list.getPart_no(),
                           list.getPart_color(),
                           url_meiji_card,
                           url_packing,
                           list.getCase_no(),
                           list.getItem_no());

               }
           }
       });
    }

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