package com.tomas.ht_mast.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.tomas.ht_mast.ListData.ScheduleOutbound_Item;
import com.tomas.ht_mast.OutboundDetail_Activity;
import com.tomas.ht_mast.R;

import java.text.DecimalFormat;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class ScheduleOutbound_Adapter extends RecyclerView.Adapter<ScheduleOutbound_Adapter.ViewHolder> {
    private List<ScheduleOutbound_Item> data;
    Context context;
    DecimalFormat decimalFormat;

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        ConstraintLayout layout;
        TextView txt_order_no, txt_part_no, txt_part_name, txt_rank, txt_factory_code, txt_qty, txt_customer,txt_product_no1;
        public ViewHolder(View itemview){
            super(itemview);
            layout = (ConstraintLayout) itemview.findViewById(R.id.item_row);
            txt_order_no = (TextView)itemView.findViewById(R.id.txt_order_no);
            txt_customer = (TextView)itemView.findViewById(R.id.txt_customer);
            txt_part_no = (TextView)itemView.findViewById(R.id.txt_part_no);
            txt_part_name = (TextView)itemView.findViewById(R.id.txt_part_name);
            txt_rank = (TextView)itemView.findViewById(R.id.txt_rank);
            txt_factory_code = (TextView)itemView.findViewById(R.id.txt_location_code);
            txt_qty = (TextView)itemView.findViewById(R.id.txt_qty);
            txt_product_no1 = (TextView) itemview.findViewById(R.id.txt_product_no1);

        }
    }


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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        decimalFormat = new DecimalFormat("#,#00.00");
        final ScheduleOutbound_Item item = data.get(position);
        holder.txt_order_no.setText(item.getOrder_no());
        holder.txt_customer.setText(item.getSupplier_name());
        holder.txt_part_no.setText(item.getPart_no());
        holder.txt_part_name.setText(item.getPart_name());
        holder.txt_rank.setText(item.getRank());
        holder.txt_factory_code.setText(item.getLocation());
        holder.txt_qty.setText(decimalFormat.format(Double.parseDouble(item.getAct_qty()))+"/"+decimalFormat.format(Double.parseDouble(item.getPlan_qty()))+" "+item.getUnit());
        holder.txt_product_no1.setText(item.getProduct_no());

        String status = item.getStatus();
        if (status.toString().trim().equals("COMPLETED")){
            holder.layout.setBackgroundResource(R.color.status_completed);
        }else if (status.toString().trim().equals("WORKING")){
            holder.layout.setBackgroundResource(R.color.status_working);
        }else{//IMPORTED
            holder.layout.setBackgroundResource(R.color.status_imported);
        }


        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!status.trim().equals("COMPLETED")) {
                    Intent i = new Intent(context.getApplicationContext(), OutboundDetail_Activity.class);
                    i.putExtra("scan", false);
                    i.putExtra("schedule_id", item.getSchedule_id());
                    i.putExtra("order_no", item.getOrder_no());
                    i.putExtra("part_no", item.getPart_no());
                    i.putExtra("part_name", item.getPart_name());
                    i.putExtra("rank", item.getRank());
                    i.putExtra("location", item.getLocation());
                    i.putExtra("plan_qty", item.getPlan_qty());
                    i.putExtra("act_qty", item.getAct_qty());
                    i.putExtra("unit", item.getUnit());
                    i.putExtra("inspection_flg", item.getInspection_flg());
                    context.startActivity(i);
                }else{
                    new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)

                            .setTitleText("ERROR")
                            .setContentText("This order is completed")
                            .show();
                }
            }
        });
    }

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