/*
 * 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.views;

import com.nokia.example.drumkit.Main;
import com.nokia.example.drumkit.components.Button;
import com.nokia.example.drumkit.helpers.ImageLoader;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/**
 * The splash screen.
 */
public class Splash
        extends GameCanvas {

    private Image infoImg;
    private Button startBtn;
    private Button projectsBtn;

    /**
     * Constructor.
     */
    public Splash() {
        super(false);
        setFullScreenMode(true);
        int horizontalCenter = getWidth() / 2;
        int verticalCenter = getHeight() / 2;
        boolean landscape = (getWidth() > getHeight()) && (getWidth() >= 400);

        if (landscape) {
            infoImg = ImageLoader.loadImage("/info_view_landscape.png");
        }
        else {
            infoImg = ImageLoader.loadImage("/info_view.png");
        }

        startBtn = new Button("/start.png",
                              "/start_pressed.png",
                              "/start.png",
                              new Button.Listener()
        {
            public void clicked(Button button) {
                Main.getInstance().viewKit();
            }
        });
        
        if (landscape) {
            startBtn.setPosition(horizontalCenter - startBtn.getWidth() / 2 + 80,
                                 verticalCenter);
        }
        else {
            startBtn.setPosition(horizontalCenter - startBtn.getWidth() / 2,
                                 verticalCenter + 56);
        }

        projectsBtn = new Button("/link.png",
                                 "/link_pressed.png",
                                 "/link.png",
                                 new Button.Listener()
        {
            public void clicked(Button button) {
                Main midlet = Main.getInstance();
                
                try {
                    if (midlet.platformRequest("http://projects.developer.nokia.com/JMEDrumkit")) {
                        midlet.exit();
                    }
                }
                catch (ConnectionNotFoundException cnfe) {
                    Main.getInstance().viewKit();
                }
            }
        });
        
        if (landscape) {
            projectsBtn.setPosition(horizontalCenter - projectsBtn.getWidth() / 2 - 75,
                                    verticalCenter + 60);
        }
        else {
            projectsBtn.setPosition(horizontalCenter - projectsBtn.getWidth() / 2,
                                    verticalCenter + 107);
        }
    }

    /**
     * Render the splash screen
     */
    protected void render() {
        Graphics g = getGraphics();
        g.drawImage(infoImg, getWidth() / 2, getHeight() / 2,
                    Graphics.HCENTER | Graphics.VCENTER);
        startBtn.paint(g);
        projectsBtn.paint(g);
        flushGraphics();
    }

    /**
     * Handle show events
     */
    protected void showNotify() {
        render();
    }

    /**
     * Handle pressed events
     *
     * @param x
     * @param y
     */
    protected void pointerPressed(int x, int y) {
        startBtn.pointerPressed(x, y);
        projectsBtn.pointerPressed(x, y);
        render();
    }

    /**
     * Handle dragged events
     *
     * @param x
     * @param y
     */
    protected void pointerDragged(int x, int y) {
        startBtn.pointerDragged(x, y);
        projectsBtn.pointerDragged(x, y);
        render();
    }

    /**
     * Handle released events
     *
     * @param x
     * @param y
     */
    protected void pointerReleased(int x, int y) {
        startBtn.pointerReleased(x, y);
        projectsBtn.pointerReleased(x, y);
        render();
    }
}
