package com.yono.messeripos.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemHistoryBinding;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.utils.Utils;

import java.text.ParseException;
import java.util.ArrayList;

public class TransactionAdapter extends RecyclerView.Adapter<TransactionAdapter.MyViewHolder> {

    private ArrayList<PaymentsModels> paymentsModels = new ArrayList<>();
    Context context;
    Utils utils = new Utils();


    public interface TransactionListener {
        void onShowDetail(PaymentsModels paymentsModels);
    }

    private TransactionListener listener;

    public void setListener(TransactionListener listener) {
        this.listener = listener;
    }

    public void setTransactionAdapter(Context context, ArrayList<PaymentsModels> paymentsModels) {
        this.paymentsModels = paymentsModels;
        this.context = context;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TransactionAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemHistoryBinding itemHistoryBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.item_history,
                parent,
                false
        );
        return new MyViewHolder(itemHistoryBinding);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(@NonNull TransactionAdapter.MyViewHolder holder, int position) {
        try {
            holder.binData(paymentsModels.get(position));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private String statusTransaction;
        ItemHistoryBinding itemHistoryBinding;

        public MyViewHolder(@NonNull ItemHistoryBinding itemView) {
            super(itemView.getRoot());
            this.itemHistoryBinding = itemView;
        }

        @SuppressLint("ResourceAsColor")
        @RequiresApi(api = Build.VERSION_CODES.O)
        public void binData(PaymentsModels paymentsModels) throws ParseException {
            itemHistoryBinding.setTransaction(paymentsModels);
            itemHistoryBinding.setDate(utils.convertDate(paymentsModels.getUpdatedAt()));
            itemHistoryBinding.cvHistory.setOnClickListener(view -> listener.onShowDetail(paymentsModels));
            itemHistoryBinding.setGrossAmount(utils.convertPrice("Rp. ", paymentsModels.getGrossAmount()));


            if (paymentsModels.getTransactionStatus().equals("Settlement".toLowerCase())) {
                statusTransaction = "Settlement";
                itemHistoryBinding.tvHistory.setBackgroundColor(0x550000FF);
            } else if (paymentsModels.getTransactionStatus().equals("Pending".toLowerCase())) {
                statusTransaction = "Pending";
                itemHistoryBinding.tvHistory.setBackgroundColor(0x55FF0000);
            }else if (paymentsModels.getTransactionStatus().equals("Success".toLowerCase())) {
                statusTransaction = "Success";
                itemHistoryBinding.tvHistory.setBackgroundColor(0xFF12FF45);
            }
        }
    }
}
