package dev.peerat.backend.repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dev.peerat.backend.Configuration;
import dev.peerat.backend.model.Badge;
import dev.peerat.backend.model.Puzzle;

public class DatabaseBadgeRepository extends BaseDatabaseQuery{
	
	public static String GET_BADGES_OF_PLAYER(){
		return Query.GET_BADGES_OF_PLAYER.request;
	}
	
	private static enum Query{
		
		// BADGES
		GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"),
		GET_BADGES_OF_PLAYER(
				"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?");
		

		private String request;

		Query(Query parent, String request) {
			this.request = parent.request + request;
		}

		Query(String request) {
			this.request = request;
		}

		public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
			return base.prepare(this.request);
		}

		@Override
		public String toString() {
			return this.request;
		}
	}
	
	private Configuration config;
	
	public DatabaseBadgeRepository(ConnectionManager con, Configuration config){
		super(con);
		this.config = config;
	}
	
	public Badge getBadge(int badgeId) {
		try {
			ensureConnection();
			PreparedStatement completionsStmt = Query.GET_BADGE.prepare(this);
			completionsStmt.setInt(1, badgeId);
			ResultSet result = completionsStmt.executeQuery();
			if (result.next()) {
				return makeBadge(result);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
