using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParticleFramework.Data;
using ParticleFramework.Communication;
using ParticleFramework.Storage;

namespace ParticleFramework
{
    static class Particle
    {

        #region Particle
            public static Properties properties = new Properties();
            public static TcpListener Server = new TcpListener();
            public static ClientMessage packetClass;
        #endregion

        public static Boolean Initialize(string prop, ClientMessage myClass)
        {
            //output welcome message
            Console.WriteLine("");
            Console.WriteLine("###################################");
            Console.WriteLine("##   Particle Server Framework   ##");
            Console.WriteLine("##         Version 0.0.1         ##");
            Console.WriteLine("##      Copyright (C) 2010       ##");
            Console.WriteLine("##    Written by Sam Hellawell   ##");
            Console.WriteLine("###################################");
            Console.WriteLine("");

            //load the properties
            Console.WriteLine("Loading Particle properties file " + prop + "...");
            if (!properties.Load(prop))
            {
                return false;
            }

            packetClass = myClass;

            //start logging
            Console.WriteLine("Initiating logging system...");
            if (!Log.Initialize(properties.Get("logfile")))
            {
                Console.WriteLine("Unable to initialize the log file! (" + properties.Get("logfile") + ")");
                return false;
            }

            //start the database
            if (properties.Get("db") != "none")
            {
                Console.WriteLine("Initiating the database...");
                if (!DBManager.Initialize(properties.Get("db"), properties.Get("db.user"), properties.Get("db.pass"), properties.Get("db.host"), properties.Get("db.name")))
                {
                    Console.WriteLine("Unable to initialize the database! (" + properties.Get("db") + ")");
                    return false;
                }
            }

            //start the server
            Console.WriteLine("Starting the server...");
            if (!Server.Init(properties.Get("port"), properties.Get("maxconnections")))
            {
                Console.WriteLine("Server initialization failed. Error reported to log file.");
                return false;
            }

            Console.WriteLine("Particle initialized successfully.");

            return true;
        }

    }
}
