// clsStartupManager.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 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 General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using GUIControlLogic.Error;
using GUIControlLogic.Persistence;

//using System.ComponentModel;

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for clsStartupManager.
    /// </summary>
    public class clsStartupManager
    {
        private readonly clsSystemLog clsLog;
        private readonly clsSettings clss;
        private readonly Thread t;
        public ZANZILib.clsMain clsEngen = new ZANZILib.clsMain();
        private frmSplash frmSplash;
        //start the system log

        public clsStartupManager()
        {
            try
            {
                //load the splash

                frmSplash = new frmSplash(this);
                frmSplash.TopMost = true;
                t = new Thread(frmSplash.Show);
                t.IsBackground = true;
                t.Name = "Splash";
                //t.Start();
                frmSplash.Activate();
                Application.DoEvents();


                //start the system log
                clsLog = new clsSystemLog();

                //add traces
                Trace.Listeners.Add(new clsWebReportListener());
                Trace.Listeners.Add(new clsTextWriterListener());

                //load the settings class
                clss = new clsSettings();

                #region P2P engen init

                //p2p engen settings
                //initialize stuff, load settings, etc.

                clss.LoadFileScopeSettings(); //Stats.LoadSave.LoadSettings();
                SetSplashProgress("Starting P2P service");
                clsEngen.StartEngen();

//				if(Stats.settings.autoGnutella)
//					Gnutella.StartStop.Start();

                #endregion

                //read the bool for updates
                if (clss.m_SerialOptionsSetting.UpdateOnStartup)
                {
                    SetSplashProgress("Checking for updates");

                    //try to look for updates
                    //ZANZI.Upgrade.clsUpgrade cslup = new ZANZI.Upgrade.clsUpgrade();

                    //the upgrade failed
                    //	if (!cslup.CheckUpgrade(true,t) )
                    //	{

//						//clean up here
//						clss = null;
//						Trace.Listeners.Clear();
//
//						//do we have to clear the trace as wel?
//
//						//load the main form with a reference to the splash thread to kill
//						Application.Run(new ZANZIClient.clsMain(t));

                    //	}
                }


                //no updates
                //clean up here
                //clss = null;
                //Trace.Listeners.Clear();

                //do we have to clear the trace as wel?

                //load the main form with a reference to the splash thread to kill
                Application.Run(new clsMain(t, clsEngen, clsLog, clss));
            }
            catch (Exception )
            {
                //Application.Run(new clsMain(t, clsEngen, clsLog, clss));
                // new clsExeption(ex);
            }
        }

        /// <summary>
        /// Sets the splash screen progress
        /// </summary>
        /// <param name="message"></param>
        private void SetSplashProgress(string message)
        {
            if (frmSplash != null)
                frmSplash.SetProgress(message);
        }

        /// <summary>
        /// Turns splash screen off
        /// </summary>
        public void SetSplashOff()
        {
            if (frmSplash != null)
                frmSplash.Close();
            frmSplash = null;
            //this.Show();
        }
    }
}