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

public class InvitePanelScript : MonoBehaviour {
    public Button closeButton;
    public Button searchButton;
    public Button doneButton;

    public InputField searchField;

    private String lastSearchValue = "";

    public GameObject inviteSearchResultPrefab;

    public GameObject searchResultPanel;
    public GameObject OnlineGameSettingsPanel;

    [SerializeField] Button findRandomPlayerButton;

    // Start is called before the first frame update
    void Start() {
        closeButton.onClick.AddListener(HidePanel);
        doneButton.onClick.AddListener(HidePanel);
        findRandomPlayerButton.onClick.AddListener(FindRandomPlayer);
        searchButton.onClick.AddListener(CallInviteFunction);
    }

    // Update is called once per frame
    void Update() {
        if (searchField.text.Length < 3) {
            searchButton.interactable = false;
        } else {
            searchButton.interactable = true;
        }
    }

    internal int InvitedCount() {
        InviteSearchResult[] invites = this.GetComponentsInChildren<InviteSearchResult>();
        int count = 0;
        foreach (InviteSearchResult isr in invites) {
            if (isr.ShouldInvite()) {
                count++;
            }
        }

        return count;
    }

    void FindRandomPlayer() {
        searchField.text = "";
        lastSearchValue = "";
        ClearIvites();
        List<OnlineDatabase.UserName> users = Database.Instance.FindRandomPlayer(Database.Instance.GetSignedInUser().Key);
        if (users != null) {
            AddToInvitePanel(users);
        }
    }

    public List<InviteSearchResult> GetSelectedUsersForInvite() {
        List<InviteSearchResult> result = new List<InviteSearchResult>();
        foreach(InviteSearchResult isr in this.GetComponentsInChildren<InviteSearchResult>()) {
            if (isr.ShouldInvite()) {
                result.Add(isr);
            }
        }

        return result;
    }

    public bool AreThereInvites() {
        int count = InvitedCount();
        return count > 0?true:false;
    }

    private void ClearIvites() {
        InviteSearchResult[] invites = this.GetComponentsInChildren<InviteSearchResult>();
        foreach (InviteSearchResult isr in invites) {
            if (!isr.ShouldInvite()) {
                Destroy(isr.gameObject);
                Destroy(isr);
            }
        }
    }

    private void CallInviteFunction() {
        if ((lastSearchValue.Equals("") || !searchField.text.Equals("")) && !lastSearchValue.Equals(searchField.text)) {
            lastSearchValue = searchField.text;
            ClearIvites();

            List<OnlineDatabase.UserName> foundUsers = OnlineDatabase.Instance.GetUsersToInvite(searchField.text);
            if (foundUsers != null) {
                AddToInvitePanel(foundUsers);
            }
        }
    }

    private void AddToInvitePanel(List<OnlineDatabase.UserName> foundUsers) {
        InviteSearchResult[] added = searchResultPanel.GetComponentsInChildren<InviteSearchResult>();
        foreach (OnlineDatabase.UserName un in foundUsers) {
            if (Database.Instance.GetSignedInUser().Key.ToString().Equals(un.id)) {
                continue;
            }
            bool skip = false;
            foreach (InviteSearchResult addedRes in added) {
                if (addedRes.GetName().Equals(un.username)) {
                    skip = true;
                    break;
                }
            }
            int resCount = 0;
            if (!skip) {
                GameObject invSearchRes = Instantiate(inviteSearchResultPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
                InviteSearchResult res = invSearchRes.GetComponent<InviteSearchResult>();
                res.SetName(un.username);
                res.SetId(un.id);
                res.transform.SetParent(searchResultPanel.transform, false);
                res.transform.SetSiblingIndex(resCount++);
            }
        }
    }

    void HidePanel() {

        OnlineGameSettingsPanel.GetComponentInChildren<invitePanelController>().updateText(InvitedCount());
        gameObject.SetActive(false);
        OnlineGameSettingsPanel.SetActive(true);
    }
}
