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

public class SelectCategoryScript : MonoBehaviour {

    [SerializeField] Button doneButton;
    [SerializeField] Button closeButton;
    [SerializeField] GameObject CategoryToSelectPrefab;

    [SerializeField] GameObject SelectCategoryPanel;

    [SerializeField] GameObject ContentPanel;
    [SerializeField] Text TotalsText;
    // Start is called before the first frame update

    private List<CategoryPanel.Category> categories;
    private void Start() {
        doneButton.onClick.AddListener(CloseDialogAction);
        closeButton.onClick.AddListener(CloseDialogAction);

        GetCategories();

        updateTotalsText();

        int pos = 0;
        foreach (CategoryPanel.Category cat in categories) {
            GameObject go = Instantiate(CategoryToSelectPrefab, Vector3.zero, Quaternion.identity);
            CategorySelection c = go.GetComponent<CategorySelection>();
            c.SetSelected(true);
            c.QuestionCount = cat.questionCount;
            c.SetCategoryText(cat.name, cat.questionCount);
            c.transform.SetSiblingIndex(pos++);
            c.transform.localScale = new Vector3(1, 1, 1);
            c.transform.SetParent(ContentPanel.transform, false);
        }
    }

    private void Update() {
        updateTotalsText();
    }

    private void updateTotalsText() {
        string totalsText = String.Format(LocalizationManager.Instance.GetText(TotalsText.GetComponent<TextLocalization>().key),
            GetSelectedCategoriesQuestionCount().ToString());
        TotalsText.text = totalsText;
    }

    private void GetCategories() {
        if (categories == null || categories.Count == 0) {
            categories = new List<CategoryPanel.Category>();
            categories = OnlineDatabase.Instance.GetCategories(categories, -1);
        }
    }

    internal void CloseDialogAction() {
        SelectCategoryPanel.GetComponent<selectCategoriesPanelController>().UpdateCategoryText(GetSelectedCategoriesQuestionCount());
        gameObject.SetActive(false);
    }

    internal int GetSelectedCategoriesQuestionCount() {
        IEnumerable<CategorySelection> selectedCategories = ContentPanel.GetComponentsInChildren<CategorySelection>().Where(c => c.isSelected());
        return selectedCategories.Sum(c => c.QuestionCount);
    }

    internal string GetCategoriesSelectedCount() {
        string returnValue;
        int selectionCount = GetSelectedCategoriesAsList().Count;

        if (selectionCount == categories.Count) {
            returnValue = LocalizationManager.Instance.GetText("ALL");
        } else {
            returnValue = selectionCount.ToString();
        }
        return returnValue;
    }

    private List<CategorySelection> GetSelectedCategoriesAsList() {
        return ContentPanel.GetComponentsInChildren<CategorySelection>().Where(c => c.isSelected()).ToList();
    }

    public List<CategoryPanel.Category> GetSelectedCategories() {
        GetCategories();
        List<string> categoriesName = categories.Select(c1 => c1.name).ToList();
        List<string> selectedCategoyNames = GetSelectedCategoriesAsList().Select(c2 => c2.getCategoryName()).ToList();
        List<string> names = new List<string>();
        if (selectedCategoyNames.Count == 0) {
            names = categoriesName;
        } else {
            names = categoriesName.Intersect(selectedCategoyNames).ToList();
        }

        return categories.Where(c => names.Contains(c.name)).ToList();
    }

    internal void ShowDialog() {
        gameObject.SetActive(true);
    }
}
