/*
 * 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.text.DateFormat;
import java.util.List;
import org.wyrez.shootingstars.gui.controls.ButtonBase;
import org.wyrez.shootingstars.gui.listener.HighscoreListener;
import org.wyrez.shootingstars.gui.manager.HighscoreManager;
import org.wyrez.shootingstars.gui.model.Score;
import org.wyrez.shootingstars.helper.ScreenHelper;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.text.Label;
import tonegod.gui.controls.text.TextField;
import tonegod.gui.controls.windows.Panel;
import tonegod.gui.core.Screen;

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

    private HighscoreListener listener;
    private HighscoreManager highscoreManager;
    private ScreenHelper screenHelper;
    private Panel plnScores;
    private TextField txtFilter;

    public HighscoreGUI(Screen screen, HighscoreListener listener, HighscoreManager highscoreManager, ScreenHelper screenHelper) {
        super(screen, new Vector2f(0f, 0f), new Vector2f(1280f, 720f));
        this.listener = listener;
        this.highscoreManager = highscoreManager;
        this.screenHelper = screenHelper;
        this.setIgnoreMouse(true);
        this.setIsVisible(false);  
        plnScores = new Panel(screen, new Vector2f(0f, 0f),new Vector2f(0f, 0f));
        create();
    }

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

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

    private void create() {
        float startPointx = screenHelper.calcX(32f);
        float startPointy = screenHelper.calcY(18f);

        float marginScoreNameLeft = screenHelper.calcX(14f);
        float marginScorePointsLeft = screenHelper.calcX(300f);
        float marginScoreDateLeft = screenHelper.calcX(420f);
        float marginScoreSongNameLeft = screenHelper.calcX(600f);
        
        float labelFontSize = screenHelper.calcX(20f);
        
        float buttonHeight = screenHelper.calcY(40f);
        float buttonWidth = screenHelper.calcX(153f);
        float buttonFontSize = screenHelper.calcX(20f);

        List<Score> highscores = highscoreManager.getScores();

        Label lblScoreNameHead = new Label(screen, new Vector2f(startPointx + marginScoreNameLeft, startPointy), new Vector2f(screenHelper.calcX(250f), screenHelper.calcY(40f)));
        lblScoreNameHead.setText("Name");
        lblScoreNameHead.setFontSize(labelFontSize);

        Label lblScorePointsHead = new Label(screen, new Vector2f(startPointx + marginScorePointsLeft, startPointy), new Vector2f(screenHelper.calcX(120f), screenHelper.calcY(40f)));
        lblScorePointsHead.setText("Points");
        lblScorePointsHead.setFontSize(labelFontSize);
        
        Label lblScoreDateHead = new Label(screen, new Vector2f(startPointx + marginScoreDateLeft, startPointy), new Vector2f(screenHelper.calcX(180f), screenHelper.calcY(40f)));
        lblScoreDateHead.setText("Date");
        lblScoreDateHead.setFontSize(labelFontSize);

        Label lblScoreSongNameHead = new Label(screen, new Vector2f(startPointx + marginScoreSongNameLeft, startPointy), new Vector2f(screenHelper.calcX(600f), screenHelper.calcY(40f)));
        lblScoreSongNameHead.setText("Song Name");
        lblScoreSongNameHead.setFontSize(labelFontSize);

        drawHighscores(highscores);

        Button btnBack = new ButtonBase(screen, new Vector2f(startPointx + screenHelper.calcX(1060f), startPointy + screenHelper.calcY(622.8f)), new Vector2f(buttonWidth, buttonHeight)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                listener.back();
            }
        };
        btnBack.setText("Back");
        btnBack.setFontSize(buttonFontSize);
        
        txtFilter = new TextField(screen, new Vector2f(screenHelper.calcX(800f), screenHelper.calcY(15f)), new Vector2f(screenHelper.calcX(280f), screenHelper.calcY(40f)));
        
        Button btnFilter = new ButtonBase(screen, new Vector2f(screenHelper.calcX(1092f), screenHelper.calcY(15f)), new Vector2f(buttonWidth, buttonHeight)) {
            @Override
            public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                List<Score> scores = highscoreManager.getScoresWithSongName(txtFilter.getText());
                drawHighscores(scores);
            }
        };
        btnFilter.setText("Filter Song Name");
        btnFilter.setFontSize(buttonFontSize);

        this.addChild(lblScoreNameHead);
        this.addChild(lblScorePointsHead);
        this.addChild(lblScoreDateHead);
        this.addChild(lblScoreSongNameHead);
        this.addChild(plnScores);
        this.addChild(txtFilter);
        this.addChild(btnFilter);
        this.addChild(btnBack);
    }

    public void refresh() {
        List<Score> highscores = highscoreManager.getScores();
        drawHighscores(highscores);
    }
    
    private void drawHighscores(List<Score> scores) {
        plnScores.detachAllChildren();
        
        float startPointx = screenHelper.calcX(32f);
        float startPointy = screenHelper.calcY(18f);

        float marginScoreNameLeft = screenHelper.calcX(14f);
        float marginScorePointsLeft = screenHelper.calcX(300f);
        float marginScoreDateLeft = screenHelper.calcX(420f);
        float marginScoreSongNameLeft = screenHelper.calcX(600f);

        float marginScoreTop = screenHelper.calcY(50f);

        float labelFontSize = screenHelper.calcX(20f);
        
        int maxScores = 0;
        if (scores.size() > 10) {
            maxScores = 10;
        } else {
            maxScores = scores.size();
        }

        for (int i = 0; i < maxScores; i++) {
            Score score = scores.get(i);
            Label lblScoreName = new Label(screen, new Vector2f(startPointx + marginScoreNameLeft, startPointy + marginScoreTop), new Vector2f(screenHelper.calcX(250f), screenHelper.calcY(40f)));
            lblScoreName.setText(score.getPlayerName());
            lblScoreName.setFontSize(labelFontSize);

            Label lblScorePoints = new Label(screen, new Vector2f(startPointx + marginScorePointsLeft, startPointy + marginScoreTop), new Vector2f(screenHelper.calcX(120f), screenHelper.calcY(40f)));
            lblScorePoints.setText(String.valueOf(score.getScore()));
            lblScorePoints.setFontSize(labelFontSize);
            
            Label lblScoreDate = new Label(screen, new Vector2f(startPointx + marginScoreDateLeft, startPointy + marginScoreTop), new Vector2f(screenHelper.calcX(180f), screenHelper.calcY(40f)));
            lblScoreDate.setText(DateFormat.getDateTimeInstance().format(score.getDate()));
            lblScoreDate.setFontSize(labelFontSize);

            Label lblScoreSongName = new Label(screen, new Vector2f(startPointx + marginScoreSongNameLeft, startPointy + marginScoreTop), new Vector2f(screenHelper.calcX(600f), screenHelper.calcY(40f)));
            lblScoreSongName.setText(score.getSongName());
            lblScoreSongName.setFontSize(labelFontSize);

            plnScores.addChild(lblScoreName);
            plnScores.addChild(lblScorePoints);
            plnScores.addChild(lblScoreDate);
            plnScores.addChild(lblScoreSongName);
            marginScoreTop += screenHelper.calcY(50f);
        }
    }
}
