package com.cpm.lorealbaautomation.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
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.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.CommonFunctions;
import com.cpm.lorealbaautomation.tbaleavemodel.TBAMELeaveHistory;

import java.util.List;

public class LeaveDetailAdapter extends RecyclerView.Adapter<LeaveDetailAdapter.MyViewHolder> {
    LayoutInflater inflator;
    List<TBAMELeaveHistory> data;

    public LeaveDetailAdapter(Context context, List<TBAMELeaveHistory> data) {
        inflator = LayoutInflater.from(context);
        this.data = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = inflator.inflate(R.layout.item_leave_details, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint({"NotifyDataSetChanged", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        final TBAMELeaveHistory current = data.get(position);
        if (position % 2 == 0) {
            holder.rlLeaveDetail.setBackgroundColor(ContextCompat.getColor(inflator.getContext(), R.color.white));
        } else {
            holder.rlLeaveDetail.setBackgroundColor(ContextCompat.getColor(inflator.getContext(), R.color.gray));
        }
        if (CommonFunctions.isTablet(inflator.getContext())) {
            holder.tvDatefromTo.setText(current.getFromDate() + "  -  " + current.getToDate());
        } else {
            holder.tvDatefromTo.setText(current.getFromDate() + "\n" + current.getToDate());
        }

        holder.tvDatefromTo.setId(position);
        holder.tvLeaveType.setText(current.getLeaveType());
        holder.tvLeaveType.setId(position);
        holder.tvNoOfDays.setText(current.getNoOfDays() != null ? current.getNoOfDays().toString() : "0");
        holder.tvNoOfDays.setId(position);
        holder.tvStatus.setText(current.getApprovedStatus());
        holder.tvStatus.setId(position);
        holder.tvRemark.setText(current.getRejectRemark());
        holder.tvRemark.setId(position);
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rlLeaveDetail;
        TextView tvDatefromTo, tvLeaveType, tvNoOfDays, tvStatus, tvRemark;

        public MyViewHolder(View itemView) {
            super(itemView);
            rlLeaveDetail = (LinearLayout) itemView.findViewById(R.id.rlLeaveDetail);
            tvDatefromTo = (TextView) itemView.findViewById(R.id.tvDatefromTo);
            tvLeaveType = (TextView) itemView.findViewById(R.id.tvLeaveType);
            tvNoOfDays = (TextView) itemView.findViewById(R.id.tvNoOfDays);
            tvStatus = (TextView) itemView.findViewById(R.id.tvStatus);
            tvRemark = (TextView) itemView.findViewById(R.id.tvRemark);
        }
    }
}
