﻿using System.Collections.Generic;

/*
 * This file is part of lanterna (http://code.google.com/p/lanterna/).
 *
 * lanterna is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2010-2017 Martin Berglund
 */
namespace com.googlecode.lanterna.terminal
{
	using TextGraphics = com.googlecode.lanterna.graphics.TextGraphics;


	/// <summary>
	/// Containing a some very fundamental functionality that should be common (and usable) to all terminal implementations.
	/// All the Terminal implementers within Lanterna extends from this class.
	/// 
	/// @author Martin
	/// </summary>
	public abstract class AbstractTerminal : Terminal
	{

		private readonly IList<TerminalResizeListener> resizeListeners;
		private TerminalSize lastKnownSize;

		protected internal AbstractTerminal()
		{
			this.resizeListeners = new List<TerminalResizeListener>();
			this.lastKnownSize = null;
		}

		public override void addResizeListener(TerminalResizeListener listener)
		{
			if (listener != null)
			{
				resizeListeners.Add(listener);
			}
		}

		public override void removeResizeListener(TerminalResizeListener listener)
		{
			if (listener != null)
			{
				resizeListeners.Remove(listener);
			}
		}

		/// <summary>
		/// Call this method when the terminal has been resized or the initial size of the terminal has been discovered. It
		/// will trigger all resize listeners, but only if the size has changed from before.
		/// </summary>
		/// <param name="columns"> Number of columns in the new size </param>
		/// <param name="rows"> Number of rows in the new size </param>
		protected internal virtual void onResized(int columns, int rows)
		{
			lock (this)
			{
				onResized(new TerminalSize(columns, rows));
			}
		}

		/// <summary>
		/// Call this method when the terminal has been resized or the initial size of the terminal has been discovered. It
		/// will trigger all resize listeners, but only if the size has changed from before.
		/// </summary>
		/// <param name="newSize"> Last discovered terminal size </param>
		protected internal virtual void onResized(TerminalSize newSize)
		{
			lock (this)
			{
				if (lastKnownSize == null || !lastKnownSize.Equals(newSize))
				{
					lastKnownSize = newSize;
					foreach (TerminalResizeListener resizeListener in resizeListeners)
					{
						resizeListener.onResized(this, lastKnownSize);
					}
				}
			}
		}

//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public com.googlecode.lanterna.graphics.TextGraphics newTextGraphics() throws java.io.IOException
		public override TextGraphics newTextGraphics()
		{
			return new TerminalTextGraphics(this);
		}
	}

}