/*
 * 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.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.scene.Spatial;
import org.wyrez.shootingstars.data.AudioDataManager;
import org.wyrez.shootingstars.gui.controls.ButtonBase;
import org.wyrez.shootingstars.gui.controls.GuiMusicRemainingTimeControl;
import org.wyrez.shootingstars.gui.controls.GuiPlayerPointsControl;
import org.wyrez.shootingstars.gui.listener.GameListener;
import org.wyrez.shootingstars.helper.ScreenHelper;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.extras.Indicator;
import tonegod.gui.controls.lists.Dial;
import tonegod.gui.controls.text.Label;
import tonegod.gui.controls.windows.Panel;
import tonegod.gui.core.Screen;

/**
 *
 * @author Darth Affe
 */
public class GameGUI extends Panel implements Gui{

    private GameListener listener;
    private Button btnStart;
    private Button btnResume;
    private Button btnMenu;
    private Indicator indOverheat;
    private Spatial player;
    private Label lblPoints;
    private Label lblRemainingTime;
    private ScreenHelper screenHelper;
    private AudioDataManager audioDataManager;

    public GameGUI(Screen screen, GameListener listener, ScreenHelper screenHelper, AudioDataManager audioDataManager) {
        super(screen, new Vector2f(0f, 0f), new Vector2f(1280f, 720f)); //create for full hd
        this.listener = listener;
        this.screenHelper = screenHelper;
        this.audioDataManager = audioDataManager;
        this.setIgnoreMouse(true);
        this.setIsVisible(false);
        create();
    }

    private void create() {

        float startPointx = screenHelper.calcX(537f);
        float startPointy = screenHelper.calcY(576f);

        float startPointPointsx = screenHelper.calcX(1000f);
        float startPointPointsy = screenHelper.calcY(10f);

        float startPointYMenu = screenHelper.calcY(200f);

        float labelFontSize = screenHelper.calcX(20f);

        btnStart = new ButtonBase(screen, new Vector2f(startPointx, startPointy),
                new Vector2f(screenHelper.calcX(200f), screenHelper.calcY(30f))) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.start();
            }
        };
        btnStart.setText("Launch");
        btnStart.setTextAlign(BitmapFont.Align.Center);
        btnStart.setFontSize(labelFontSize);

        btnResume = new ButtonBase(screen, new Vector2f(startPointx, startPointYMenu),
                new Vector2f(screenHelper.calcX(200f), screenHelper.calcY(30f))) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.resume();
            }
        };
        btnResume.setText("Resume");
        btnResume.setTextAlign(BitmapFont.Align.Center);
        btnResume.setFontSize(labelFontSize);

        btnMenu = new ButtonBase(screen, new Vector2f(startPointx, startPointYMenu + screenHelper.calcY(40f)),
                new Vector2f(screenHelper.calcX(200f), screenHelper.calcY(30f))) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.cancelTrack();
            }
        };
        btnMenu.setText("Menu");
        btnMenu.setTextAlign(BitmapFont.Align.Center);
        btnMenu.setFontSize(labelFontSize);

        indOverheat = new Indicator(screen, new Vector2f(0f, 0f), Indicator.Orientation.VERTICAL) {
            @Override
            public void onChange(float arg0, float arg1) {
            }
        };
        indOverheat.setDimensions(screenHelper.calcX(75f), screenHelper.calcY(75f));
        indOverheat.centerToParent();
        indOverheat.setBaseImage("Textures/crosshair.png");
        indOverheat.setIndicatorImage("Textures/heat_ind_color.png");
        indOverheat.setOverlayImage("Textures/empty.png");
        indOverheat.setMaxValue(100f);
        updateOverhead(0f);

        lblPoints = new Label(screen, new Vector2f(startPointPointsx, startPointPointsy), new Vector2f(screenHelper.calcX(275f), screenHelper.calcY(180f)));
        lblPoints.setFontColor(new ColorRGBA(1f, 0f, 0f, 1f));
        lblPoints.setFontSize(screenHelper.calcX(30f));
        
        lblRemainingTime = new Label(screen, new Vector2f(startPointPointsx, startPointPointsy), new Vector2f(screenHelper.calcX(275f), screenHelper.calcY(240f)));
        lblRemainingTime.setFontColor(new ColorRGBA(1f, 0f, 0f, 1f));
        lblRemainingTime.setFontSize(screenHelper.calcX(30f));

        this.addChild(btnStart);
        this.addChild(btnResume);
        this.addChild(btnMenu);
        this.addChild(indOverheat);
        this.addChild(lblPoints);
        this.addChild(lblRemainingTime);

        btnResume.hide();
        btnMenu.hide();
        indOverheat.hide();
        lblPoints.hide();
        lblRemainingTime.hide();
    }

    public void setStart() {
        btnStart.hide();
        btnResume.hide();
        btnMenu.hide();
        indOverheat.show();
        lblPoints.show();
        lblRemainingTime.show();
    }

    public void setWait() {
        btnStart.show();
        btnResume.hide();
        btnMenu.hide();
        indOverheat.hide();
        lblPoints.hide();
        lblRemainingTime.hide();
    }

    public void showMenu() {
        btnStart.hide();
        btnResume.show();
        btnMenu.show();
        indOverheat.hide();
        lblPoints.hide();
        lblRemainingTime.hide();
    }

    public void resumeGame() {
        btnStart.hide();
        btnResume.hide();
        btnMenu.hide();
        indOverheat.show();
        lblPoints.show();
        lblRemainingTime.show();
    }

    public void attach() {
        screen.addElement(this);
    }

    public void detach() {
        screen.removeElement(this);
    }

    public void updateOverhead(float percent) {
        indOverheat.setCurrentValue(100 * percent);
    }

    public void setPoints(String points) {
        lblPoints.setText("Score: " + points);
    }
    
    public void setRemainingTime(float remainingTime) {
        int seconds = (int)remainingTime / 1000;
        int min = (int)(seconds / 60);
        lblRemainingTime.setText("Time: " + min + ":" + String.format("%02d", (seconds - min * 60)));
    }

    public void setPlayer(Spatial player) {
        this.player = player;
        this.addControl(new GuiPlayerPointsControl(player, this));
        this.addControl(new GuiMusicRemainingTimeControl(audioDataManager, this));
    }

    public void refresh() {
    }
}
