using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Linq;

public class NewOnlineGameScript : MonoBehaviour
{
    public Slider answerTimeSlider;
    public Slider daysToAnswerSlider;
    public Slider correctsToWinSlider;
    public GameObject invitePanel;

    [SerializeField] Button StartButton;
    [SerializeField] GameObject categoryPanel;

    private InvitePanelScript ips;
    private selectCategoriesPanelController selectedCategoriesController;
    // Start is called before the first frame update
    void Start()
    {
        StartButton.onClick.AddListener(StartNewOnlineGame);
        StartButton.interactable = false;
        ips = invitePanel.GetComponent<InvitePanelScript>();
        selectedCategoriesController = categoryPanel.GetComponent<selectCategoriesPanelController>();
    }

    // Update is called once per frame
    void Update()
    {
        if (ips.AreThereInvites() &&
            selectedCategoriesController.GetSelectedCategories().Sum(c => c.questionCount) > 0) { // Fungerar ej, stoppar inte vid 0
            StartButton.interactable = true;
        } 
        // TODO, make sure there are enough questions selected in categories
    }

    void StartNewOnlineGame() {
        List<InviteSearchResult> inviteUsers = ips.GetSelectedUsersForInvite();
        List<CategoryPanel.Category> selectedCategories = selectedCategoriesController.GetSelectedCategories();

        List<int> selectedCategoryIds = selectedCategories.Select(c => c.id).ToList();

        OnlineDatabase.Instance.SetupNewOnlineGame((int)answerTimeSlider.value, (int)daysToAnswerSlider.value, (int)correctsToWinSlider.value, inviteUsers, selectedCategoryIds);
        SceneManager.LoadScene("MainMenu");
    }
}
