package com.example.vdcscore.ui.livegames;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.vdcscore.R;
import com.example.vdcscore.databinding.FragmentLiveGameDetailBinding;

public class LiveGameDetailFragment extends Fragment {

    private FragmentLiveGameDetailBinding binding;
    private GameViewModel gameViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        gameViewModel = new ViewModelProvider(requireActivity()).get(GameViewModel.class);

        binding = FragmentLiveGameDetailBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        gameViewModel.getSelectedGame().observe(getViewLifecycleOwner(), game -> {
            if (game != null) {
                binding.detailGameTime.setText(game.getTime());
                binding.detailHomeTeam.setText(game.getHomeTeam());
                binding.detailAwayTeam.setText(game.getAwayTeam());
                binding.detailScore.setText(game.getHomeScore() + " - " + game.getAwayScore());

                binding.progressPossession.setProgress(game.getHomePossession());
                binding.textPossessionHome.setText(game.getHomePossession() + "%");
                binding.textPossessionAway.setText(game.getAwayPossession() + "%");

                binding.textShotsHome.setText(String.valueOf(game.getHomeShots()));
                binding.textShotsAway.setText(String.valueOf(game.getAwayShots()));
            }
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
