package com.cpm.lorealbaMabeline.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.cpm.lorealbaMabeline.R;
import com.cpm.lorealbaMabeline.constant.CommonFunctions;
import com.cpm.lorealbaMabeline.gsonGetterSetter.leaves.TLeaveRequestDetail;
import java.util.List;

public class ApprovedLeaveAdapter extends RecyclerView.Adapter<ApprovedLeaveAdapter.MyViewHolder> {
    LayoutInflater inflator;
    String type;
    List<TLeaveRequestDetail> data;

    public ApprovedLeaveAdapter(Context context, String type, List<TLeaveRequestDetail> data) {
        inflator = LayoutInflater.from(context);
        this.data = data;
        this.type = type;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = inflator.inflate(type.equals("GovtHolidays") ? R.layout.item_gov_holidays : R.layout.item_approved_leaves, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint({"NotifyDataSetChanged", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        final TLeaveRequestDetail current = data.get(position);
        if (type.equals("GovtHolidays")) {
            holder.titleTV.setText(current.getLeaveType());
            holder.descTV.setText(current.getReason());
        } else {
            String day = current.getDay().toString().length() == 1 ? "0" + current.getDay().toString() : current.getDay().toString();
            String month = current.getMonth().toString().length() == 1 ? "0" + current.getMonth().toString() : current.getMonth().toString();
            holder.dateTV.setText(CommonFunctions.formatDate(month + "/" + day + "/" + current.getYear().toString()));


            if (!current.getApprovedStatus().isEmpty() && current.getApprovedStatus().equalsIgnoreCase("Pending")) {
                holder.pendingCardView.setVisibility(View.VISIBLE);
                holder.rejectCardView.setVisibility(View.GONE);
                holder.approveCardView.setVisibility(View.GONE);
            } else if (!current.getApprovedStatus().isEmpty() && current.getApprovedStatus().equalsIgnoreCase("Reject")) {
                holder.pendingCardView.setVisibility(View.GONE);
                holder.rejectCardView.setVisibility(View.VISIBLE);
                holder.approveCardView.setVisibility(View.GONE);
            } else {
                holder.pendingCardView.setVisibility(View.GONE);
                holder.rejectCardView.setVisibility(View.GONE);
                holder.approveCardView.setVisibility(View.VISIBLE);
            }

            holder.titleTV.setText(current.getLeaveSubType() != null && !current.getLeaveSubType().isEmpty() ? current.getLeaveType() + " - " + current.getLeaveSubType() : current.getLeaveType());
            holder.descTV.setText(current.getReason());
        }
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayoutCompat rlParent;
        TextView dateTV, titleTV, descTV;
        CardView approveCardView, pendingCardView, rejectCardView;

        public MyViewHolder(View itemView) {
            super(itemView);
            rlParent = (LinearLayoutCompat) itemView.findViewById(R.id.rlParent);
            dateTV = (TextView) itemView.findViewById(R.id.dateTV);
            titleTV = (TextView) itemView.findViewById(R.id.titleTV);
            descTV = (TextView) itemView.findViewById(R.id.descTV);
            approveCardView = (CardView) itemView.findViewById(R.id.approveCardView);
            pendingCardView = (CardView) itemView.findViewById(R.id.pendingCardView);
            rejectCardView = (CardView) itemView.findViewById(R.id.rejectCardView);
        }
    }
}

