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;

    public GameObject inviteSearchResultPrefab;

    public GameObject searchResultPanel;

    private float autoSearchWait = 3f;
    private bool searchActive = false;
    private float autoSearchTime;
    private int searchChars = 0;
    private string searchedString = "";

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

    // Update is called once per frame
    void Update() {
        if (searchField.text.Length < 3) {
            ClearIvites();
            searchActive = false;
            searchButton.interactable = false;
        } else if (searchActive && searchChars != searchField.text.Length) {
            searchChars = searchField.text.Length;
            autoSearchTime = Time.time + autoSearchWait;
        } else if (searchActive && searchChars == searchField.text.Length && Time.time >= autoSearchTime) {
            CallInviteFunction();
        } else if (!searchActive && !searchField.text.Equals(searchedString)) {
            searchActive = true;
            autoSearchTime = Time.time + autoSearchWait;
            searchButton.interactable = true;
            searchChars = searchField.text.Length;
        }
    }

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

        return count;
    }

    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() {
        ClearIvites();
        searchedString = searchField.text;
        searchActive = false;

        List<OnlineDatabase.UserName> foundUsers = OnlineDatabase.Instance.GetUsersToInvite(searchField.text);
        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;
                }
            }
            if (!skip) {
                GameObject question = Instantiate(inviteSearchResultPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
                InviteSearchResult res = question.GetComponent<InviteSearchResult>();
                res.SetName(un.username);
                res.SetId(un.id);
                res.transform.SetParent(searchResultPanel.transform, false);
            }
        }
    }

    internal void ShowPanel() {
        CanvasGroup cg = this.GetComponent<CanvasGroup>();
        cg.alpha = 1f;
        cg.interactable = true;
        cg.blocksRaycasts = true;
    }

    internal void HidePanel() {
        CanvasGroup cg = this.GetComponent<CanvasGroup>();
        cg.alpha = 0f;
        cg.interactable = false;
        cg.blocksRaycasts = false;
    }
}
