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.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.quiz.data.adapter.TypeOfBetAdapter;
import com.example.quiz.data.model.TypeOfBet;
import com.example.quiz.databinding.FragmentListOfTypeOfBetsBinding;
import com.example.quiz.viewModel.SharedViewModel;
import com.google.android.material.appbar.MaterialToolbar;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import dagger.hilt.android.AndroidEntryPoint;

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

@AndroidEntryPoint
public class ListOfTypeOfBets extends Fragment {

    private FragmentListOfTypeOfBetsBinding binding;

    private SharedViewModel shared;

    private TypeOfBetAdapter adapter;

    public ListOfTypeOfBets() {
        // Required empty public constructor
    }
    public static ListOfTypeOfBets newInstance() {
        ListOfTypeOfBets fragment = new ListOfTypeOfBets();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.typeOfBetRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        // ViewModels
        //viewModel = new ViewModelProvider(this).get(BetViewModel.class);
        shared = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);

        // ⚡ Initialiser l’adapter UNE SEULE FOIS
        adapter = new TypeOfBetAdapter(new ArrayList<>());
        adapter.setOnItemClickListener(type -> shared.setTypeOfBet(type));
        binding.typeOfBetRecyclerView.setAdapter(adapter);
        List<TypeOfBet> types = List.of(
                new TypeOfBet(
                        "Couple placé",
                        "COUPLE_PLACE",
                        2
                ),
                new TypeOfBet(
                        "Couple gagnant",
                        "COUPLE_GAGNANT",
                        2
                ),
                new TypeOfBet(
                        "Tierce",
                        "TIERCE",
                        3
                ),
                new TypeOfBet(
                        "Quarte +",
                        "QUARTE",
                        4
                ),
                new TypeOfBet(
                        "Quinte +",
                        "QUINTE",
                        5
                )
        );
        String betType = shared.selectedCourse.getValue().getType();
        Log.d("BET_TYPE", betType);
        List<TypeOfBet>  useList = new ArrayList<>();
        if(betType.toLowerCase().startsWith("quinte")){
            useList = types.stream()
                            .filter(bet -> !bet.getName().toLowerCase().contains("quarte"))
                            .collect(Collectors.toList());
        }
        if(betType.toLowerCase().startsWith("quarte")){
            useList = types.subList(0, 4);
        }
        if(betType.toLowerCase().startsWith("tierce")){
            useList = types.subList(0, 3);
        }
        if(betType.toLowerCase().contains("couple")){
            useList = types.subList(0, 2);
        }
        adapter.setTypes(useList);
        adapter.setOnItemClickListener(type -> {
            shared.setTypeOfBet(type);
        });
        Button btnValidate = binding.btnValidate;
        btnValidate.setOnClickListener(v->{
            if(shared.typeOfBet.getValue() == null){
                Toast.makeText(getContext(),"Aucun Type de jeu choisi", Toast.LENGTH_SHORT).show();
                return;
            }
            FragmentManager fragmentManager = getParentFragmentManager();
            BetValidation betValidation = BetValidation.newInstance();
            fragmentManager.beginTransaction()
                    .replace(R.id.nav_host_fragment_content_main, betValidation)
                    .addToBackStack(null)
                    .commit();
        });
    }

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