package com.yono.messeripos.transactiontab;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yono.messeripos.R;
import com.yono.messeripos.adapter.PendingAdapter;
import com.yono.messeripos.adapter.TransactionAdapter;
import com.yono.messeripos.models.MainViewModels;
import com.yono.messeripos.models.MainViewModelsCart;
import com.yono.messeripos.models.PaymentPending;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class TransactionPending extends Fragment {

    PendingAdapter pendingAdapter;
    PaymentPending paymentPending;
    MainViewModelsCart mainViewModelsCart;
    RecyclerView recyclerView;
    Utils utils = new Utils();
    ProgressBar loading;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(
                R.layout.activity_pending,
                container,
                false
        );

        mainViewModelsCart = new ViewModelProvider(this).get(MainViewModelsCart.class);
        recyclerView = v.findViewById(R.id.rvPending);
        loading = v.findViewById(R.id.loadingPending);
        pendingAdapter = new PendingAdapter();
        LifecycleOwner lifecycleOwner = this;

        loading.setVisibility(View.VISIBLE);

        mainViewModelsCart.getAllPaymentPending().observe(lifecycleOwner, new Observer<List<PaymentPending>>() {
            @Override
            public void onChanged(List<PaymentPending> paymentPendings) {
                Log.i("Get data", "onChanged: "+utils.convertGson(paymentPendings));
                loading.setVisibility(View.GONE);
                ArrayList<PaymentPending> pendingArrayList = new ArrayList<>();
                pendingArrayList.addAll(paymentPendings);

                pendingAdapter.setPendingAdapter(v.getContext(),pendingArrayList);
                recyclerView.setLayoutManager(new LinearLayoutManager(v.getContext(), RecyclerView.VERTICAL, false));
                recyclerView.setAdapter(pendingAdapter);
            }
        });


        return v;
    }
}
