package com.example.quiz;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

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

import com.example.quiz.databinding.FragmentCaisseBinding;
import com.google.android.material.appbar.MaterialToolbar;

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

    FragmentCaisseBinding binding;

    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);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentCaisseBinding.inflate(inflater, container, false);
        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();
                getActivity().getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.nav_host_fragment_content_main, sold)
                        .addToBackStack(null)
                        .commit();
            }
        });
        binding.winTicketBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WinTicket winTicket = WinTicket.newInstance();
                getActivity().getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.nav_host_fragment_content_main, winTicket)
                        .addToBackStack(null)
                        .commit();
            }
        });
        binding.cancelTicketBtn.setOnClickListener(v -> {
            AnnulationTicket annulationTicket = AnnulationTicket.newInstance();
            getActivity().getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.nav_host_fragment_content_main, annulationTicket)
                    .addToBackStack(null)
                    .commit();
        });

        binding.lastBetsBtn.setOnClickListener(v -> {
            getActivity().getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.nav_host_fragment_content_main, DerniersParis.newInstance())
                    .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(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);
                }
            }
        }
    }
}