using System ;
using System.Windows.Forms ;
using System.IO ;
using System.Collections.Generic ;
using System.Diagnostics ;

using log4net.Config ;

// --------------------------------------------------------------------

public static partial class Program
{

    public const string APP_NAME = "ASL Charts" ;
    private static bool mIsMono ;

    private static string mBaseDir ;
    private static string mResourcesDir ;
    private static string mDataDir ;
    private static JsonConfig mAppConfig = null ;
    private static JsonConfig mDataConfig = null ;
    private static JsonConfig mDebugConfig = null ;
    private static MainForm mMainForm = null ;
    private static Dictionary< string, List<string> > mStartupMsgs = new Dictionary<string,List<string>>() ;

    [STAThread]
    static void Main( string[] args )
    {
        // initialize
        mBaseDir = Application.StartupPath ;
        mIsMono = Type.GetType( "Mono.Runtime" ) != null ;

        // configure logging
        string fname = Path.Combine( mBaseDir, "log4net.xml" ) ;
        if ( ! File.Exists( fname ) )
            fname = Path.Combine( mBaseDir, "../log4net.xml" ) ;
        if ( File.Exists( fname ) )
             XmlConfigurator.Configure( new FileInfo( fname ) ) ;

        // locate the resources directory
        mResourcesDir = Path.Combine( mBaseDir, "resources" ) ;
        if ( ! Directory.Exists( mResourcesDir ) )
            mResourcesDir = Path.Combine( mBaseDir, "../resources" ) ;

        // locate the data directory
        mDataDir = Path.Combine( mBaseDir , "data" ) ;
        if ( ! Directory.Exists( mDataDir ) ) {
            mDataDir = Path.Combine( mBaseDir , "../data" ) ;
            if ( ! Directory.Exists( mDataDir ) )
                mDataDir = mBaseDir ;
        }

        try {

            // load the app config
            string fname0 = Path.GetFileNameWithoutExtension( System.Reflection.Assembly.GetEntryAssembly().Location ) + ".json" ;
            fname = Path.Combine( mBaseDir, fname0 ) ;
            if ( ! File.Exists( fname ) ) {
                fname = Path.Combine( mDataDir, fname0 ) ;
                if ( ! File.Exists( fname ) )
                    fname = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fname0 ) ;
            }
            mAppConfig = new JsonConfig( "app config", fname ) ;

            // load the data config
            mDataConfig = new JsonConfig( "data config", Path.Combine( mDataDir, "config.json" ) ) ;

            // load the debug config
            mDebugConfig = new JsonConfig( "debug config", Path.Combine( mBaseDir, "debug.json" ) ) ;

        } catch( Exception ex ) {
            showErrorMsg( "Initialization failed:\n\n" + ex.Message ) ;
            return ;
        }

        // install our message filter
        Application.AddMessageFilter( new MyMessageFilter() ) ;

        // run the application
        Application.EnableVisualStyles() ;
        Application.SetCompatibleTextRenderingDefault( false ) ;
        mMainForm = new MainForm() ;
        Application.Run( mMainForm ) ;
    }

    public static void showInfoMsg( string msg ) { MessageBox.Show(msg,APP_NAME,MessageBoxButtons.OK,MessageBoxIcon.Information) ; }
    public static void showWarningMsg( string msg ) { MessageBox.Show(msg,APP_NAME,MessageBoxButtons.OK,MessageBoxIcon.Warning) ; }
    public static void showErrorMsg( string msg ) { MessageBox.Show(msg,APP_NAME,MessageBoxButtons.OK,MessageBoxIcon.Error) ; }

    public static bool isMono { get { return mIsMono ; } }

    public static MainForm mainForm { get { return mMainForm ; } }
    public static string baseDir { get { return mBaseDir ; } }
    public static string resourcesDir { get { return mResourcesDir ; } }
    public static JsonConfig appConfig { get { return mAppConfig ; } }
    public static JsonConfig dataConfig { get { return mDataConfig ; } }
    public static JsonConfig debugConfig { get { return mDebugConfig ; } }
    public static string dataDir { get { return mDataDir ; } }
}
