package com.example.quiz;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

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

import com.example.quiz.databinding.FragmentCaisseBinding;
import com.example.quiz.utils.AuthNavigator;
import com.example.quiz.utils.SessionManager;
import com.example.quiz.viewModel.LoginViewModel;
import com.example.quiz.viewModel.LogsViewModel;
import com.google.android.material.appbar.MaterialToolbar;


import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

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

    FragmentCaisseBinding binding;

    AuthNavigator authNavigator;

    SessionManager sessionManager;

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

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sessionManager = SessionManager.newInstance(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentCaisseBinding.inflate(inflater, container, false);
        authNavigator = new AuthNavigator(getContext(), getParentFragmentManager(), sessionManager,new ViewModelProvider(this).get(LoginViewModel.class),new ViewModelProvider(this).get(LogsViewModel.class), this);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        binding.soldeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Sold sold = Sold.newInstance();
                authNavigator.navigate(sold);
            }
        });
        binding.winTicketBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WinTicket winTicket = WinTicket.newInstance();
                authNavigator.navigate(winTicket);
            }
        });
        binding.cancelTicketBtn.setOnClickListener(v -> {
            AnnulationTicket annulationTicket = AnnulationTicket.newInstance();
            authNavigator.navigate(annulationTicket);
        });

        binding.lastBetsBtn.setOnClickListener(v -> {
            authNavigator.navigate(DerniersParis.newInstance());
        });
        binding.raceReport.setOnClickListener(v -> {
            authNavigator.navigate(RaceReport.newInstance());
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (activity != null){
            MaterialToolbar toolbar = activity.findViewById(R.id.toolbar);
            activity.setSupportActionBar(toolbar);
            if(toolbar != null){
                if(activity.getSupportActionBar() != null){
                    activity.getSupportActionBar().setTitle("Caisse");
                    toolbar.setBackgroundColor(getResources().getColor(R.color.primary_green));
                    toolbar.setTitleTextColor(getResources().getColor(R.color.white));
                    toolbar.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}