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.FragmentWinTicketBinding;
import com.google.android.material.appbar.MaterialToolbar;

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

    FragmentWinTicketBinding binding;


    public WinTicket() {
        // Required empty public constructor
    }
    public static WinTicket newInstance() {
        WinTicket fragment = new WinTicket();
        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 = FragmentWinTicketBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.winTicketBtnBack.setOnClickListener(v -> {
            getActivity().onBackPressed();
        });
        binding.scanTicketBtn.setOnClickListener(v ->{
            ScannerDialog scannerDialog = new ScannerDialog();
            scannerDialog.setOnBarcodeScannedListener(code -> {
                binding.referenceTicket.setText(code);
            });
            scannerDialog.show(getParentFragmentManager(), "scanner");
        });
    }

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

    }
}