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.ResponseCategoria;
import br.com.codetec.saudesabor.service.ServiceConector;
import br.com.codetec.saudesabor.ui.login.ui.adapter.CategoriaAdapter;
import br.com.codetec.saudesabor.utils.UtilsActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ComprasFragment extends Fragment {

    private ComprasModel viewModel;
    private RecyclerView listaCategoriasRecyclerView;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        viewModel =
                ViewModelProviders.of(this).get(ComprasModel.class);
        View root = inflater.inflate(R.layout.fragment_compras, container, false);
        final TextView textView = root.findViewById(R.id.text_notifications);
        viewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        listaCategoriasRecyclerView = root.findViewById(R.id.listaCategoriasRecyclerView);
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        Call<ResponseCategoria> call =  ServiceConector.getService(this.getActivity()).categorias(getString(R.string.locale));
        call.enqueue(new Callback<ResponseCategoria>() {
            @Override
            public void onResponse(Call<ResponseCategoria> call, Response<ResponseCategoria> response) {
                if(response.body()!=null){
                    CategoriaAdapter adapter = new CategoriaAdapter(response.body().getCategorias(), ComprasFragment.this.getActivity());
                    UtilsActivity.getInstance().initRecyclerView(ComprasFragment.this.getActivity(), listaCategoriasRecyclerView);
                    listaCategoriasRecyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }
            }
            @Override
            public void onFailure(Call<ResponseCategoria> call, Throwable t) {

            }
        });
    }
}