package com.example.quiz;

import android.os.Bundle;

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.quiz.data.adapter.LastBetsAdapter;
import com.example.quiz.data.model.Pari;
import com.example.quiz.databinding.FragmentDerniersParisBinding;
import com.example.quiz.utils.Result;
import com.example.quiz.utils.SharedPrefsHelper;
import com.example.quiz.viewModel.PariViewModel;
import com.google.android.material.appbar.MaterialToolbar;

import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DerniersParis#newInstance} factory method to
 * create an instance of this fragment.
 */

@AndroidEntryPoint
public class DerniersParis extends Fragment {

    FragmentDerniersParisBinding binding;
    SharedPrefsHelper prefsHelper;
    PariViewModel viewModel;

    LastBetsAdapter adapter;

    public DerniersParis() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static DerniersParis newInstance() {
        DerniersParis fragment = new DerniersParis();
        Bundle args = new Bundle();
          fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefsHelper = SharedPrefsHelper.getInstance(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding =  FragmentDerniersParisBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.lastBetsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        viewModel = new ViewModelProvider(this).get(PariViewModel.class);
        viewModel.getDerniersParis(prefsHelper.get("code")).observe(getViewLifecycleOwner(), new Observer<Result<List<Pari>>>() {
            @Override
            public void onChanged(Result<List<Pari>> listResult) {
                switch (listResult.status){
                    case LOADING:
                        Toast.makeText(getContext(), "En cours", Toast.LENGTH_SHORT).show();
                        break;
                    case ERROR:
                        Toast.makeText(getContext(), listResult.message, Toast.LENGTH_SHORT).show();
                        break;
                    case SUCCESS:
                        adapter = new LastBetsAdapter(listResult.data, pari -> {
                        });
                        binding.lastBetsRecyclerView.setAdapter(adapter);
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if(activity!=null){
            MaterialToolbar toolbar = activity.findViewById(R.id.toolbar);
            activity.setSupportActionBar(toolbar);
            toolbar.setTitle("Derniers Paris");
        }
    }
}