﻿using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net.Config;
using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using LogManager = ExitGames.Logging.LogManager;

namespace SuicideBombermanServer
{
	public class Application : ApplicationBase
	{
		protected static readonly ILogger Log = LogManager.GetCurrentClassLogger();
		protected Dictionary<Guid, ClientPeer> _clientPeers;
		protected readonly int ClientPort = ServerSettings.Default.ClientPort;

		protected override PeerBase CreatePeer(InitRequest initRequest)
		{
			Log.DebugFormat("Init request from {0}:{1} to {2}:{3}", initRequest.RemoteIP, initRequest.RemotePort, initRequest.LocalIP, initRequest.LocalPort);
			if (initRequest.LocalPort == ClientPort)
			{
				Log.DebugFormat("Client requests connection to the server");
				throw new NotImplementedException();
			}
			Log.WarnFormat("Unknown request to restricted port from {0}:{1}", initRequest.RemoteIP, initRequest.RemotePort);
			return null;
		}

		protected override void Setup()
		{
			log4net.GlobalContext.Properties["BinPath"] = base.BinaryPath;
			var configFileInfo = new FileInfo(Path.Combine(base.BinaryPath, "log4net.config"));
			if (configFileInfo.Exists)
			{
				LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
				XmlConfigurator.ConfigureAndWatch(configFileInfo);
			}

			Log.InfoFormat("Start");
		}

		protected override void TearDown()
		{
			Log.InfoFormat("TearDown");
		}
	}
}
