/*
 * Copyright (C) 2013 Rappold <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.manager;

/**
 *
 * @author Rappold
 */
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.util.*;
import org.wyrez.shootingstars.game.GameSettings;
import org.wyrez.shootingstars.gui.model.Score;
import org.wyrez.shootingstars.io.SQLiteConnector;
import org.wyrez.shootingstars.states.util.OptionSettings;

public class HighscoreManager {

    private List<Score> scores;
    private Connection connection;
    private SQLiteConnector connector;
    private ScoreComparator scoreComparator;

    public HighscoreManager(SQLiteConnector connector, ScoreComparator scoreComparator) {
        this.connector = connector;
        this.scoreComparator = scoreComparator;
        connection = connector.initDBConnection();
        scores = new ArrayList<Score>();
        loadScoreFile();
    }

    public List<Score> getScores() {
        sort(scores);
        return scores;
    }

    private void sort(List<Score> scores) {
        Collections.sort(scores, scoreComparator);
    }

    public void addScore(GameSettings gameSettings, OptionSettings optionSettings, int score) {
        String songName = gameSettings.getTrackArtist() + " ~ " + gameSettings.getTrackTitle();
        Date date = new Date(System.currentTimeMillis());
        scores.add(new Score(songName, optionSettings.getUsername(), score, date));
        updateScoreFile(optionSettings.getUsername(), score, songName, date);
    }

    public List<Score> getScoresWithSongName(String songName) {
        List<Score> tempScores = new ArrayList<Score>();

        try {
            ResultSet rs = connector.excecuteQuery(connection, "SELECT * FROM Scores WHERE LOWER(SongName) LIKE LOWER('%" + songName + "%');");
            while (rs.next()) {
                tempScores.add(new Score(rs.getString("SongName"), rs.getString("Name"), rs.getInt("Points"), DateFormat.getDateTimeInstance().parse(rs.getString("GameDate"))));
            }
            rs.close();
        } catch (Exception ex) {
        }
        sort(tempScores);
        return tempScores;
    }

    private void loadScoreFile() {
        try {
            ResultSet rs = connector.excecuteQuery(connection, "SELECT * FROM Scores;");
            while (rs.next()) {
                scores.add(new Score(rs.getString("SongName"), rs.getString("Name"), rs.getInt("Points"), DateFormat.getDateTimeInstance().parse(rs.getString("GameDate"))));
            }
            rs.close();
        } catch (Exception ex) {
        }
    }

    private void updateScoreFile(String name, int score, String songName, Date date) {
        try {
            connector.excecuteUpdate(connection, "INSERT INTO Scores(Name,Points,SongName,GameDate) values('" + name + "'," + score + ", '" + songName + "', '" + DateFormat.getDateTimeInstance().format(date) + "' )");
        } catch (Exception ex) {
        }
    }
}
