/*
 * Copyright (C) 2013 Darth Affe <http://wyrez.org> and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.wyrez.shootingstars.gui;

import com.jme3.asset.AssetManager;
import com.jme3.font.BitmapFont;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.scene.Spatial;
import com.jme3.ui.Picture;
import org.wyrez.shootingstars.gui.controls.ButtonBase;
import org.wyrez.shootingstars.gui.controls.IndicatorBase;
import org.wyrez.shootingstars.gui.listener.GameListener;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.extras.Indicator;
import tonegod.gui.controls.text.Label;
import tonegod.gui.controls.windows.Panel;
import tonegod.gui.core.Screen;
import tonegod.gui.effects.Effect;

/**
 *
 * @author Darth Affe
 */
public class GameGUI extends Panel {
    
    private GameListener listener;
    private Button btnStart;
    private Button btnResume;
    private Button btnMenu;
    private Picture picCrosshair;
    private Indicator indOverheat;
    private AssetManager assetManager;
    
    public GameGUI(Screen screen, GameListener listener, AssetManager assetManager) {
        super(screen, new Vector2f(0f, 0f), new Vector2f(1280f, 720f)); //create for full hd
        this.listener = listener;
        this.assetManager = assetManager;
        this.setIgnoreMouse(true);
        this.setIsVisible(false);
        create();
    }
    
    private void create() {
        
        float startPointx = 537f;
        float startPointy = 576f;
        
        float startPointCrosshairx = 441f;
        float startPointCrosshairy = 560f;
        
        float startPointOverheatx = 505f;
        float startPointOverheaty = 270f;
        
        float startPointYMenu = 200f;
        
        btnStart = new ButtonBase(screen, new Vector2f(startPointx, startPointy),
                new Vector2f(200f, 30f)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.start();
            }
        };
        btnStart.setText("Launch");
        btnStart.setTextAlign(BitmapFont.Align.Center);
        
        btnResume = new ButtonBase(screen, new Vector2f(startPointx, startPointYMenu),
                new Vector2f(200f, 30f)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.resume();
            }
        };
        btnResume.setText("Resume");
        btnResume.setTextAlign(BitmapFont.Align.Center);
        
        btnMenu = new ButtonBase(screen, new Vector2f(startPointx, startPointYMenu + 40f),
                new Vector2f(200f, 30f)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.cancelTrack();
            }
        };
        btnMenu.setText("Menu");
        btnMenu.setTextAlign(BitmapFont.Align.Center);
        
        picCrosshair = new Picture("Crosshair");
        picCrosshair.setWidth(400f);
        picCrosshair.setHeight(400f);
        picCrosshair.setImage(assetManager, "Textures/Crosshair.png", true);

        Panel plnCrosshair = new Panel(screen, new Vector2f(startPointCrosshairx, startPointCrosshairy), new Vector2f(0f, 0f));
        plnCrosshair.setIsVisible(false);
        plnCrosshair.setIgnoreMouse(true);
        plnCrosshair.attachChild(picCrosshair);  
        
        indOverheat = new IndicatorBase(screen, new Vector2f(startPointOverheatx, startPointOverheaty),
                new Vector2f(275f, 180f), Indicator.Orientation.VERTICAL);        
        indOverheat.setIndicatorColor(new ColorRGBA(0f, 0f, 1f, 0.5f));
        indOverheat.setAlphaMap("Textures/Crosshair_AlphaMap.png");
        indOverheat.setOverlayImage("Textures/Crosshair_OverlayImage.png");
        //indOverheat.setBaseImage("Textures/Crosshair_Overheat_Fog.png");        
        
        this.addChild(btnStart);
        this.addChild(btnResume);
        this.addChild(btnMenu);        
        this.addChild(indOverheat);
        this.addChild(plnCrosshair);
        
        btnResume.hide();
        btnMenu.hide();
        picCrosshair.setCullHint(CullHint.Always);
        indOverheat.hide();
    }
    
    public void setStart() {
        btnStart.hide();
        btnResume.hide();
        btnMenu.hide();
        picCrosshair.setCullHint(CullHint.Inherit);
        indOverheat.show();
    }
    
    public void setWait() {
        btnStart.show();
        btnResume.hide();
        btnMenu.hide();
        picCrosshair.setCullHint(CullHint.Always);
        indOverheat.hide();
    }
    
    public void showMenu() {
        btnStart.hide();
        btnResume.show();
        btnMenu.show();
        picCrosshair.setCullHint(CullHint.Always);
        indOverheat.hide();
    }
    
    public void resumeGame() {
        btnStart.hide();
        btnResume.hide();
        btnMenu.hide();
        picCrosshair.setCullHint(CullHint.Inherit);
        indOverheat.show();
    }
    
    public void attach() {
        screen.addElement(this);
    }
    
    public void detach() {
        screen.removeElement(this);
    }
    
    public void updateOverhead(float percent) {
        indOverheat.setCurrentValue(percent);
    }

    public void setMaxTimeToOverheat(float maxTimeToOverheat) {
        indOverheat.setMaxValue(maxTimeToOverheat);
    }
}
