/* -*- Mode: java; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */

// Copyright 2001-2003, Danger, Inc.  All Rights Reserved.
// This file is subject to the Danger, Inc. Sample Code License,
// which is provided in the file SAMPLE_CODE_LICENSE.
// Copies are also available from http://developer.danger.com/

package com.danger.terminal;

import danger.app.Application;
import danger.app.AppResources;
import danger.app.Event;
import danger.app.SettingsDB;
import danger.storage.StorageManager;
import danger.ui.ImageView;
import danger.ui.Pen;
import danger.ui.SplashScreen;
import danger.ui.View;


public class Terminal
    extends Application
    implements TerminalResources, TerminalEvents, AppResources
{
    public
    Terminal() 
    {
        sMe = this;
        mPrefs = new SettingsDB(PREFS_DBNAME, true);

        mSessionManager = new SessionManager(mPrefs);
        mTerminalScreen = new TerminalScreen(mSessionManager);
        mListWindow = (SessionListWindow) getResources().getScreen(ID_SESSION_LIST_SCREEN);
        mListWindow.init(mSessionManager, mTerminalScreen);
        mConsoleIsActive = false;
        
        // these may take a little while, do them as a separate thread
        new Thread ("Terminal: storage scanner") {
            public void run () {
                /* load any saved sessions */
                mSessionManager.loadSessions();
                
                StorageManager.registerStorageDeviceListener(Terminal.this);

                // for the simulator:
                JaramikoSession.rescanMedia();
            }
        }.start();
        
        updatePreviewScreen();
    }

    /* package */ void
    setBoring (boolean boring)
    {
        System.err.println("boring = " + boring);
        mSplashScreen = (SplashScreen) getResources().getSplashScreen(ID_SPLASH_SCREEN_RESOURCE);
        ImageView image = (ImageView) mSplashScreen.getDescendantWithID(ID_SPLASH_IMAGE);
        image.setImage(getResources().getBitmap(boring ? ID_SPLASH_SCREEN_NO_MONKEY : ID_SPLASH_SCREEN_NO_TITLE));
        updatePreviewScreen();
    }
    
    public void
    renderSplashScreen (View v, Pen p)
    {
        if (mSplashScreen == null) {
            mSplashScreen = (SplashScreen) getResources().getSplashScreen(ID_SPLASH_SCREEN_RESOURCE);
        }
        mSplashScreen.paint(v, p);
    }
    
    public void
    resume()
    {
        if (mSettingsScreen != null) {
            mSettingsScreen.show();
        } else if (mConsoleIsActive) {
            mTerminalScreen.show();
        } else {
            mListWindow.show();
        }
    }

    public static SettingsDB
    getSettingsDB ()
    {
        return mPrefs;
    }
    
    public static Terminal instance()
    {
        return sMe;
    }

    public SessionManager getSessionManager()
    {
        return mSessionManager;
    }

    public void
    returnToSessionList ()
    {
        mConsoleIsActive = false;
        mListWindow.show();
    }

    public void
    makeConsoleActive ()
    {
        mConsoleIsActive = true;
    }

    public void
    openSettings (ServerInfo info)
    {
        mSettingsScreen = (SettingsScreen) getResources().getScreen(ID_SETTINGS_SCREEN, this);
        mSettingsScreen.fillFrom(info);
        mSettingsScreen.show();
    }

    public void
    openSession (ServerInfo info)
    {
        mTerminalScreen.openSession(info);
        mTerminalScreen.show();
        mConsoleIsActive = true;
    }

    public boolean
    receiveEvent (Event e)
    {
        switch (e.type) {
        case Event.EVENT_DATASTORE_RESTORED:
            return mSessionManager.receiveEvent(e);

        case EVENT_SETTINGS_CANCEL:
            mSettingsScreen.hide();
            mSettingsScreen = null;
            break;

        case EVENT_SETTINGS_DONE:
            mSettingsScreen.save();
            mSettingsScreen.hide();
            mSettingsScreen = null;
            break;
        
        case Event.EVENT_STORAGE_DEVICE_STATE_CHANGED:
            if ((e.what == Event.WHAT_STORAGE_DEVICE_MOUNTED) || (e.what == Event.WHAT_STORAGE_DEVICE_UNMOUNTED)) {
                // we could be smarter here, but easiest just to rescan each time something changes.
                // (this also gives JaramikoSession a chance to clear the pkey cache.)
                JaramikoSession.rescanMedia();
            }
            return true;
        }

        return super.receiveEvent(e);
    }
    
    public TerminalScreen
    getTerminalScreen ()
    {
        return mTerminalScreen;
    }


    private SessionManager          mSessionManager;
    private SessionListWindow       mListWindow;
    private TerminalScreen          mTerminalScreen;
    private static SettingsDB       mPrefs;
    private boolean                 mConsoleIsActive;
    private SettingsScreen          mSettingsScreen;
    private SplashScreen            mSplashScreen;
    
    private static Terminal         sMe;
    private final static String     PREFS_DBNAME = "pref";
}
