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.FavoritoResponse;
import br.com.codetec.saudesabor.model.ProdutoResponse;
import br.com.codetec.saudesabor.service.ServiceConector;
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 FavoritosFragment extends Fragment implements ListaAtualizavelInterface {

    private RecyclerView recyclerView;
    private TextView textView;

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

        return root;
    }

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

        atualizarLista();
    }

    public void atualizarLista() {
        Call<FavoritoResponse> call = ServiceConector
                .getService(FavoritosFragment.this.getContext())
                .listarFavoritos(getString(R.string.locale), LoginActivity.responseLogin.getToken());

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

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

    private void semFavoritos() {
        recyclerView.setVisibility(View.GONE);
        textView.setText("Sem Favoritos");
        textView.setVisibility(View.VISIBLE);
    }
}