/*
 * Copyright (C) 2013 Snowsun <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.input.event.MouseButtonEvent;
import com.jme3.math.Vector2f;
import java.io.File;
import org.wyrez.shootingstars.game.GameSettings;
import org.wyrez.shootingstars.gui.controls.ButtonBase;
import org.wyrez.shootingstars.gui.listener.FileMetaInfoListener;
import org.wyrez.shootingstars.helper.ScreenHelper;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.text.Label;
import tonegod.gui.controls.windows.Panel;
import tonegod.gui.core.Screen;

/**
 *
 * @author Snowsun
 */
public class FileMetaInfoGUI extends Panel implements Gui {

    private FileMetaInfoListener listener;
    private GameSettings settings;
    private ScreenHelper screenHelper;
    private Label lblFileName;
    private Label lblArtist;
    private Label lblTitel;
    private Label lblDuration;

    public FileMetaInfoGUI(Screen screen, FileMetaInfoListener listener, GameSettings settings, ScreenHelper screenHelper) {
        super(screen, new Vector2f(0f, 0f), new Vector2f(1280f, 720f));
        this.listener = listener;
        this.settings = settings;
        this.screenHelper = screenHelper;
        this.setIgnoreMouse(true);
        this.setIsVisible(false);
        create();
    }

    private void create() {
        float startPointx = screenHelper.calcX(256f);
        float startPointy = screenHelper.calcY(202f);
        float marginLeft = screenHelper.calcX(110f);
        float buttonWidth = screenHelper.calcX(153.6f);
        float labelFontSize = screenHelper.calcX(20f);

        Label lblFileNameHead = new Label(screen, new Vector2f(startPointx, startPointy), new Vector2f(screenHelper.calcX(100f), screenHelper.calcY(40f)));
        lblFileNameHead.setText("Track Name");
        lblFileNameHead.setFontSize(labelFontSize);

        lblFileName = new Label(screen, new Vector2f(startPointx + marginLeft, startPointy), new Vector2f(screenHelper.calcX(400f), screenHelper.calcY(40f)));
        lblFileName.setFontSize(labelFontSize);

        Label lblArtistHead = new Label(screen, new Vector2f(startPointx, startPointy + screenHelper.calcY(50f)), new Vector2f(screenHelper.calcX(100f), screenHelper.calcY(40f)));
        lblArtistHead.setText("Artist");
        lblArtistHead.setFontSize(labelFontSize);

        lblArtist = new Label(screen, new Vector2f(startPointx + marginLeft, startPointy + screenHelper.calcY(50f)), new Vector2f(screenHelper.calcX(400f), screenHelper.calcY(40f)));
        lblArtist.setFontSize(labelFontSize);

        Label lblTitelHead = new Label(screen, new Vector2f(startPointx, startPointy + screenHelper.calcY(100f)), new Vector2f(screenHelper.calcX(100f), screenHelper.calcY(40f)));
        lblTitelHead.setText("Titel");
        lblTitelHead.setFontSize(labelFontSize);

        lblTitel = new Label(screen, new Vector2f(startPointx + marginLeft, startPointy + screenHelper.calcY(100f)), new Vector2f(screenHelper.calcX(400f), screenHelper.calcY(40f)));
        lblTitel.setFontSize(labelFontSize);

        Label lblDurationHead = new Label(screen, new Vector2f(startPointx, startPointy + screenHelper.calcY(150f)), new Vector2f(screenHelper.calcX(100f), screenHelper.calcY(40f)));
        lblDurationHead.setText("Duration");
        lblDurationHead.setFontSize(labelFontSize);

        lblDuration = new Label(screen, new Vector2f(startPointx + marginLeft, startPointy + screenHelper.calcY(150f)), new Vector2f(screenHelper.calcX(400f), screenHelper.calcY(40f)));
        lblDuration.setFontSize(labelFontSize);

        Button btnStartLoading = new ButtonBase(screen, new Vector2f(startPointx + screenHelper.calcX(653f), startPointy + screenHelper.calcY(438.8f)), new Vector2f(buttonWidth, screenHelper.calcY(40))) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.startLoading();
            }
        };
        btnStartLoading.setText("Play");
        btnStartLoading.setFontSize(labelFontSize);

        Button btnCancel = new ButtonBase(screen, new Vector2f(startPointx + screenHelper.calcX(836f), startPointy + screenHelper.calcY(438.8f)), new Vector2f(buttonWidth, screenHelper.calcY(40))) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.cancel();
            }
        };
        btnCancel.setText("Cancel");
        btnCancel.setFontSize(labelFontSize);

        this.addChild(lblFileNameHead);
        this.addChild(lblFileName);
        this.addChild(lblArtistHead);
        this.addChild(lblArtist);
        this.addChild(lblTitelHead);
        this.addChild(lblTitel);
        this.addChild(lblDurationHead);
        this.addChild(lblDuration);
        this.addChild(btnStartLoading);
        this.addChild(btnCancel);
    }

    public void refresh() {
        lblDuration.setText(String.valueOf(settings.getTrackLength()) + " s");
        lblTitel.setText(settings.getTrackTitle());
        lblArtist.setText(settings.getTrackArtist());
        lblFileName.setText(new File(settings.getVideoFile()).getName());
    }

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

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