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

public class GamesScrollController : MonoBehaviour, IPointerClickHandler {

    public Sprite upArrow;
    public Sprite downArrow;

    public GameObject localGamesPanel;
    public GameObject onlineGamesPanel;
    public GameObject finishedGamesPanel;
    public GameObject gamesPanel;

    public void expandThis(string objectName) {
        bool finishExpanded = finishedGamesPanel.GetComponent<RectTransform>().rect.height > 51f ? true : false;
        bool localExpanded = localGamesPanel.GetComponent<RectTransform>().rect.height > 51f ? true : false;
        bool onlineExpanded = onlineGamesPanel.GetComponent<RectTransform>().rect.height > 51f ? true : false;

        int numberExpanded = 0;
        if (finishExpanded) { numberExpanded++; };
        if (localExpanded) { numberExpanded++; };
        if (onlineExpanded) { numberExpanded++; };


        if (objectName.Equals("LocalGamesTitle")) {
            if (localExpanded) {
                Minimize(localGamesPanel);
            } else {
                Maximize(localGamesPanel);
                Minimize(onlineGamesPanel);
                Minimize(finishedGamesPanel);
            }
        } else if (objectName.Equals("OnlineGamesTitle")) {
            if (onlineExpanded) {
                Minimize(onlineGamesPanel);
            } else {
                Maximize(onlineGamesPanel);
                Minimize(localGamesPanel);
                Minimize(finishedGamesPanel);
            }
        } else if (objectName.Equals("FinishedGamesTitle")) {
            if (finishExpanded) {
                Minimize(finishedGamesPanel);
            } else {
                Maximize(finishedGamesPanel);
                Minimize(localGamesPanel);
                Minimize(onlineGamesPanel);
            }
        }
    }

    public void OnPointerClick(PointerEventData eventData) {
        GameObject clickedObject = GameObject.Find(eventData.pointerPress.name);
        expandThis(clickedObject.name);
        // Möjligt populera finished games när den expanderas
    }

    private void Maximize(GameObject activeObject) {
        float gamesPanelHeight = gamesPanel.GetComponent<RectTransform>().rect.height;

        activeObject.GetComponent<RectTransform>().sizeDelta = new Vector2(activeObject.GetComponent<RectTransform>().rect.width, gamesPanelHeight - 100f);
        for (int i = 0; i < activeObject.transform.childCount; i++) {
            if (activeObject.transform.GetChild(i).gameObject.tag.Equals("GamesPanelContent")) {
                activeObject.transform.GetChild(i).GetComponent<CanvasGroup>().alpha = 1f;
                break;
            }
        }
    }

    private void Minimize(GameObject activeObject) {
        activeObject.GetComponent<RectTransform>().sizeDelta = new Vector2(activeObject.GetComponent<RectTransform>().rect.width, 50f);
        for (int i = 0; i < activeObject.transform.childCount; i++) {
            if (activeObject.transform.GetChild(i).gameObject.tag.Equals("GamesPanelContent")) {
                activeObject.transform.GetChild(i).GetComponent<CanvasGroup>().alpha = 0f;
                break;
            }
        }
    }
}