package com.yono.messeripos.transactiontab;

import android.app.AlertDialog;
import android.content.Intent;
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.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.appbar.MaterialToolbar;
import com.yono.messeripos.CartActivity;
import com.yono.messeripos.HistoryActivity;
import com.yono.messeripos.R;
import com.yono.messeripos.adapter.TabAdapterTransaction;
import com.yono.messeripos.adapter.TransactionAdapter;
import com.yono.messeripos.models.MainViewModels;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.utils.Utils;

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

public class TransactionSuccess extends Fragment {
    TransactionAdapter transactionAdapter;
    PaymentsModels paymentsModels;
    MainViewModels mainViewModels;
    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_history, container, false);


//        MaterialToolbar toolbars = v.findViewById(R.id.cAppBar);
        recyclerView = v.findViewById(R.id.rvHistory);
        loading = v.findViewById(R.id.loading);

        loading.setVisibility(View.VISIBLE);
//        recyclerView.addItemDecoration(utils.dividerItemDecoration(this));
        transactionAdapter = new TransactionAdapter();
        mainViewModels = new ViewModelProvider(this).get(MainViewModels.class);

        mainViewModels.getAllTransactions().observe(getViewLifecycleOwner(), new Observer<DataResponse<List<PaymentsModels<String>>>>() {
            @Override
            public void onChanged(DataResponse<List<PaymentsModels<String>>> listDataResponse) {
                ArrayList<PaymentsModels> newData = new ArrayList<>();
                newData.addAll(listDataResponse.getData());
                transactionAdapter.setTransactionAdapter(v.getContext(), newData);

                loading.setVisibility(View.GONE);

                Log.d("TAG", "onChanged: "+newData);

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

        transactionAdapter.setListener(new TransactionAdapter.TransactionListener() {
            @Override
            public void onShowDetail(PaymentsModels paymentsModels) {

            }
        });

        return v;

    }
}
