package br.com.codetec.saudesabor.ui.login.ui.notifications;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import br.com.codetec.saudesabor.R;
import br.com.codetec.saudesabor.model.ProdutoResponse;
import br.com.codetec.saudesabor.service.CarrinhoComprasGlobal;
import br.com.codetec.saudesabor.service.ServiceConector;
import br.com.codetec.saudesabor.ui.login.ListaProdutosActivity;
import br.com.codetec.saudesabor.ui.login.LoginActivity;
import br.com.codetec.saudesabor.ui.login.ui.adapter.ProdutoAdapter;
import br.com.codetec.saudesabor.utils.UtilsActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class HistoricoComprasFragment extends Fragment {

    private RecyclerView recyclerView;
    private TextView textView;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_painel_anuncios, container, false);
        textView = root.findViewById(R.id.text_notifications);
        recyclerView = root.findViewById(R.id.recyclerView);

        return root;
    }

    @Override
    public void onResume() {
        super.onResume();

        Call<ProdutoResponse> call = ServiceConector
                .getService(HistoricoComprasFragment.this.getContext())
                .listarHistoricoCompras(getString(R.string.locale), LoginActivity.responseLogin.getToken());

        call.enqueue(new Callback<ProdutoResponse>() {
            @Override
            public void onResponse(Call<ProdutoResponse> call, Response<ProdutoResponse> response) {
                if(response.body()!=null && response.body().getProdutos()!=null){
                    recyclerView.setVisibility(View.VISIBLE);
                    ProdutoAdapter adapter = new ProdutoAdapter(response.body().getProdutos(), HistoricoComprasFragment.this.getContext(), false);
                    UtilsActivity.getInstance().initRecyclerView(HistoricoComprasFragment.this.getContext(), recyclerView);
                    recyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    textView.setText("");
                    textView.setVisibility(View.GONE);
                }else{
                    semRegistros();
                }
            }

            @Override
            public void onFailure(Call<ProdutoResponse> call, Throwable t) {
                semRegistros();
            }
        });
    }

    private void semRegistros() {
        recyclerView.setVisibility(View.GONE);
        textView.setText("Sem histórico de compras");
        textView.setVisibility(View.VISIBLE);
    }

}