/*
 * 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.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.AudioHeader;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.wyrez.shootingstars.game.GameSettings;
import org.wyrez.shootingstars.gui.controls.ButtonBase;
import org.wyrez.shootingstars.gui.listener.FileMetaInfoListener;
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 String fileName = "";
    private String duration = "";
    private String title = "";
    private String artist = "";

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

    private void create() {
        float startPointx = 256f;
        float startPointy = 202f;
        float marginLeft = 110f;
        float buttonWidth = 153.6f;
        
        File file = null;
        if(settings.useVideo()) {
            file = new File(settings.getVideoFile());
        } else {
            file = new File(settings.getAudioFile());
        }

        readMetaData(file);
        
        Label lblFileNameHead = new Label(screen, new Vector2f(startPointx, startPointy), new Vector2f(100f, 40f));
        lblFileNameHead.setText("Track Name");
        
        Label lblFileName = new Label(screen, new Vector2f(startPointx + marginLeft, startPointy), new Vector2f(400f, 40f));
        lblFileName.setText(fileName);
        
        Label lblArtistHead = new Label(screen, new Vector2f(startPointx, startPointy + 50f), new Vector2f(100f, 40f));
        lblArtistHead.setText("Artist");
        
        Label lblArtist = new Label(screen, new Vector2f(startPointx + marginLeft, startPointy + 50f), new Vector2f(400f, 40f));
        lblArtist.setText(artist);
        
        Label lblTitelHead = new Label(screen, new Vector2f(startPointx, startPointy + 100f), new Vector2f(100f, 40f));
        lblTitelHead.setText("Titel");
        
        Label lblTitel = new Label(screen, new Vector2f(startPointx + marginLeft, startPointy + 100f), new Vector2f(400f, 40f));
        lblTitel.setText(title);
        
        Label lblDurationHead = new Label(screen, new Vector2f(startPointx, startPointy + 150f), new Vector2f(100f, 40f));
        lblDurationHead.setText("Duration");
        
        Label lblDuration = new Label(screen, new Vector2f(startPointx + marginLeft, startPointy + 150f), new Vector2f(400f, 40f));
        lblDuration.setText(duration);
        
        Button btnStartLoading = new ButtonBase(screen, new Vector2f(startPointx + 653f, startPointy + 438.8f), new Vector2f(buttonWidth, 40)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.startLoading();
            }
        };
        btnStartLoading.setText("Play");
        
        Button btnCancel = new ButtonBase(screen, new Vector2f(startPointx + 836f, startPointy + 438.8f), new Vector2f(buttonWidth, 40)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.cancel();
            }
        };
        btnCancel.setText("Cancel");
        
        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);
    }

    private void readMetaData(File file) {
        try {
            fileName = file.getName();
            AudioFile f = AudioFileIO.read(file);
            Tag tag = f.getTag();
            AudioHeader ah = f.getAudioHeader();

            if (ah != null) {
                duration = String.valueOf(ah.getTrackLength()) + " s";
            }

            if (tag != null) {
                if (tag.hasField(FieldKey.TITLE)) {
                    title = tag.getFirst(FieldKey.TITLE);
                }
                artist = tag.getFirst(FieldKey.ARTIST);
            }
        } catch (Exception ex) {
        }
    }

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

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