/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.drumkit;

import com.nokia.example.drumkit.helpers.ImageLoader;
import com.nokia.example.drumkit.views.FullTouchDrumkit;
import com.nokia.example.drumkit.views.Splash;
import com.nokia.example.drumkit.views.TouchAndTypeDrumkit;
import com.nokia.mid.ui.orientation.Orientation;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.MIDlet;


/**
 * The main MIDlet
 */
public class Main
        extends MIDlet
        implements CommandListener {

    private static Main self;
    private Display display;
    private SoundManager soundManager;
    private GameCanvas view;
    private GameCanvas padKit;
    private GameCanvas acousticKit;
    private Splash splash;
    private Command exit;

    /**
     * Get a singleton instance of the Main MIDlet
     */
    public static Main getInstance() {
        return self;
    }

    /**
     * Handle application start events
     */
    public void startApp() {
        self = this;

        exit = new Command("Exit", Command.EXIT, 1);
        
        try {
            Class.forName("com.nokia.mid.ui.orientation.Orientation");
            Orientation.setAppOrientation(Orientation.ORIENTATION_LANDSCAPE);
        }
        catch (ClassNotFoundException e) {}
        
        if (display == null) {
            display = Display.getDisplay(this);
            soundManager = new SoundManager();
            splash = new Splash();
            display.setCurrent(splash);
            initKit();
        }
    }

    /**
     * Handle application pause events
     */
    public void pauseApp() {
    }

    /**
     * Handle application destroy events
     */
    public void destroyApp(boolean unconditional) {
        soundManager.cleanUp();
    }

    /**
     * Displays the drumkit view.
     */
    public void viewKit() {
        if (view != null) {
            display.setCurrent(view);
        }
        
        splash = null;
    }
    
    /**
     * Switches between pad and acoustic drum kit GUIs.
     */
    public void switchKit() {
        if (display.getCurrent() instanceof FullTouchDrumkit) {
            display.setCurrent(padKit);
        }
        else {
            display.setCurrent(acousticKit);
        }
    }
    
    /**
     * Load drum kit views.
     */
    public void initKit() {
        if ((splash.getWidth() == 400
                && splash.getHeight() == 240)) {
            acousticKit = new FullTouchDrumkit(soundManager);
            view = acousticKit;            
            padKit = new TouchAndTypeDrumkit(soundManager);
        }
        else {
            view = new TouchAndTypeDrumkit(soundManager);
        }
    }

    /**
     * Display alert in silent mode.
     */
    public void sampleAlert() {
        Image note = ImageLoader.loadImage("/note.png");
        Alert alert =
                new Alert("Unable to load samples.",
                          "Sounds might not be enabled with your current Profile. "
                          + "Try changing the Profile to General and restart "
                          + "the application.",
                          note, AlertType.WARNING);
        alert.addCommand(exit);
        alert.setCommandListener(this);
        display.setCurrent(alert, view);
    }

    /**
     * Handle command actions
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == exit) {
            exit();
        }
    }

    /**
     * Exit the application
     */
    public void exit() {
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * @return True if the device is a S60 device
     */
    public static boolean isS60Phone() {
        String platform = System.getProperty("microedition.platform");
        
        if (platform == null) {
            platform = "";
        }
        
        if (platform.indexOf("sw_platform=S60") > 0) {
            return true;
        }
        
        if (platform.indexOf("/S60_") > 0) {
            return true;
        }
        
        try {
            Class.forName("com.symbian.gcf.NativeInputStream");
            return true;
        }
        catch (ClassNotFoundException e) {
        }

        return false;
    }
}
