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

namespace GameAnvilSampleForTemplate {
    public class MessageContainer : MonoBehaviour {
        [SerializeField]
        Transform contentsRoot = null;
        [SerializeField]
        MessageElement dummyElement = null;
        [SerializeField]
        Scrollbar scrollbarVertical = null;
        [SerializeField]
        ScrollRect scrollRect = null;
        [SerializeField]
        VerticalLayoutGroup verticalLayoutGroup = null;
        [SerializeField]
        ContentSizeFitter contentSizeFitter = null;


        public MessageElement addElement(string name) {
            MessageElement newElement = GameObject.Instantiate<MessageElement>(dummyElement, contentsRoot);
            newElement.textMessage.text = name;
            newElement.gameObject.SetActive(true);

            setPositionZero();

            return newElement;
        }

        void setPositionZero() {
            scrollRect.velocity = Vector2.zero;
            scrollbarVertical.value = 0;
            scrollRect.CalculateLayoutInputVertical();
            scrollRect.SetLayoutVertical();
            Canvas.ForceUpdateCanvases();

            verticalLayoutGroup.CalculateLayoutInputVertical();
            contentSizeFitter.SetLayoutVertical();
            scrollRect.verticalNormalizedPosition = 0;

            scrollbarVertical.value = 0;
        }
    }
}