/*
 * 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.SoundManager;
import com.nokia.example.drumkit.components.*;
import com.nokia.example.drumkit.helpers.ImageLoader;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class FullTouchDrumkit
    extends DrumKit
    implements Button.Listener
{
    private Image backgroundImage;

    /**
     * Constructor.
     * @param sm The sound manager instance.
     */
    public FullTouchDrumkit(SoundManager sm) {
        super(sm);
        this.soundManager = sm;
        
        backgroundImage = ImageLoader.loadImage("/background.png");
        
        // Create drum components
        drums = new Drum[13];
        drums[SoundManager.SAMPLE_KICK] = new Drum(ImageLoader.loadImage("/kick.png"), SoundManager.SAMPLE_KICK, 0, 118, 2);
        drums[SoundManager.SAMPLE_SNARE] = new Drum(ImageLoader.loadImage("/snare.png"), SoundManager.SAMPLE_SNARE, 105, 144, 2);
        drums[SoundManager.SAMPLE_TOM1] = new Drum(ImageLoader.loadImage("/tom1.png"), SoundManager.SAMPLE_TOM1, 107, 78, 2);
        drums[SoundManager.SAMPLE_TOM2] = new Drum(ImageLoader.loadImage("/tom2.png"), SoundManager.SAMPLE_TOM2, 175, 74, 2);
        drums[SoundManager.SAMPLE_TOM3] = new Drum(ImageLoader.loadImage("/tom3.png"), SoundManager.SAMPLE_TOM3, 236, 120, 2);
        drums[SoundManager.SAMPLE_COWBELL] = new Drum(ImageLoader.loadImage("/cowbell.png"), SoundManager.SAMPLE_COWBELL, 201, 178, 2);
        drums[SoundManager.SAMPLE_HIHAT_OPEN] = new Drum(ImageLoader.loadImage("/hihatopenupper.png"), SoundManager.SAMPLE_HIHAT_OPEN, 315, 118, 2);
        drums[SoundManager.SAMPLE_HIHAT_CLOSED] = new Drum(ImageLoader.loadImage("/hihatclosed.png"), SoundManager.SAMPLE_HIHAT_CLOSED, 295, 177, 2);
        drums[SoundManager.SAMPLE_CRASH] = new Drum(ImageLoader.loadImage("/crash.png"), SoundManager.SAMPLE_CRASH, 0, 47, 2);
        drums[SoundManager.SAMPLE_CHINA] = new Drum(ImageLoader.loadImage("/china.png"), SoundManager.SAMPLE_CHINA, 54, 20, 2);
        drums[SoundManager.SAMPLE_SPLASH] = new Drum(ImageLoader.loadImage("/splash.png"), SoundManager.SAMPLE_SPLASH, 154, 31, 2);
        drums[SoundManager.SAMPLE_RIDE_BELL] = new Drum(ImageLoader.loadImage("/ride2.png"), SoundManager.SAMPLE_RIDE_BELL, 230, 23, 2);
        drums[SoundManager.SAMPLE_RIDE] = new Drum(ImageLoader.loadImage("/ride1.png"), SoundManager.SAMPLE_RIDE, 270, 56, 2);
        
        // Create button components
        buttons = new Button[5];
        buttons[PLAY_BUTTON] = new Button("/play.png", "/play_pressed.png", "/play_disabled.png", this);
        buttons[PLAY_BUTTON].setPosition(127, 0);
        buttons[PLAY_BUTTON].disable();
        buttons[STOP_BUTTON] = new Button("/stop.png", "/stop_pressed.png", "/play_disabled.png", this);
        buttons[STOP_BUTTON].setPosition(220, 0);
        buttons[STOP_BUTTON].setVisible(false);
        buttons[REC_BUTTON] = new Button("/record_pressed.png", "/record_pressed.png", "/record_disabled.png", this);      
        buttons[REC_BUTTON].setPosition(220, 0);
        buttons[MODE_BUTTON] = new Button("/padmode.png", "/padmode.png", "/padmode.png", this);      
        buttons[MODE_BUTTON].setPosition(5, 0);
        buttons[EXIT_BUTTON] = new Button("/exit.png", "/exit_pressed.png", "/exit.png", this);        
        buttons[EXIT_BUTTON].setPosition(357, 0);
    }
    
    /**
     * Render graphics
     *
     * @param g
     */
    public void paint(Graphics g) {
        if (paintBackground) {
            // Draw the whole background
            g.drawImage(backgroundImage, 0, 0, 0);
            paintBackground = false;
        }
        else {
            // Draw background under button components
            g.setClip(0, 0, getWidth(), buttons[0].getY() + buttons[0].getHeight());
            g.drawImage(backgroundImage, 0, 0, 0);
            
            // Remove drum highlights
            for (int i = 0; i < drums.length; i++) {
                Drum d = drums[i];
                
                if (d.needsRepaint() && !d.isHit()) {
                    g.setClip(d.getX(), d.getY(), d.getWidth(), d.getHeight());
                    g.drawImage(backgroundImage, 0, 0, 0);
                }
            }
        }

        g.setClip(0, 0, getWidth(), getHeight());
        
        // Draw drum highlights
        for (int i = 0; i < drums.length; i++) {
            Drum d = drums[i];
            
            if (d.needsRepaint()) {
                d.draw(g);
            }
        }
        
        // Draw buttons
        for (int i = 0; i < buttons.length; i++) {
            buttons[i].paint(g);
        }
    }
}
