package org.aegee.runanddine.registration;

import java.sql.*;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.aegee.runanddine.util.data.ModelNotExistingException;
import org.aegee.runanddine.util.model.MySQLModelManager;

/**
 * Manages persistent GroupRegistration models
 */
public class GroupRegistrationManager extends MySQLModelManager<GroupRegistration> {

    /**
     * Singleton instance of registration manager
     */
    private static GroupRegistrationManager instance = null;

    /**
     * Init GroupRegistrationManager (private because of singleton pattern)
     */
    private GroupRegistrationManager() {
        super();
        this.table = "group_registrations";
    }

    /**
     * Return singleton instance of GroupRegistrationManager
     *
     * @return Singleton instance of GroupRegistrationManager
     */
    public static GroupRegistrationManager getInstance() {
        // create singleton instance if it does not exist
        if (GroupRegistrationManager.instance == null) {
            GroupRegistrationManager.instance = new GroupRegistrationManager();
        }

        return GroupRegistrationManager.instance;
    }

    /**
     * Get GroupRegistration by validation token of one of its members
     *
     * @param id Validation token of one members from a GroupRegistration
     * @return GroupRegistration with member that has the validation token
     * @throws ModelNotExistingException
     */
    public GroupRegistration getByValidationToken(String token) throws ModelNotExistingException {
        try {
            PreparedStatement stmt = this.con.prepareStatement("SELECT id, serialized_object FROM " + this.table + " WHERE validation_token1=? OR validation_token2=?");
            // bind token
            stmt.setString(1, token);
            stmt.setString(2, token);
            // search for model
            ResultSet rs = stmt.executeQuery();
            // check if model exists
            if (rs.next()) {
                // deserialize model
                GroupRegistration model = this.readObject(rs.getBytes("serialized_object"));
                model.setId(rs.getInt("id"));
                rs.close();
                stmt.close();

                return model;
            } else {
                throw new ModelNotExistingException();
            }
        } catch (SQLException e) {
            e.printStackTrace(System.err);
            return null;
        }
    }

    /**
     * Get list of all existing GroupRegistrations for given RunAndDine
     *
     * @param runAndDineId ID of RunAndDine
     * @return List of all existing GroupRegistrations for given RunAndDine
     */
    public List<GroupRegistration> getAllByRunAndDineId(int runAndDineId) {
        try {
            List<GroupRegistration> models = new LinkedList<GroupRegistration>();
            // select all models for RunAndDine
            PreparedStatement stmt = this.con.prepareStatement("SELECT id, serialized_object FROM " + this.table + " WHERE rad_id=?");
            // bind RunAndDine ID
            stmt.setInt(1, runAndDineId);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                // deserialize model
                GroupRegistration model = this.readObject(rs.getBytes("serialized_object"));
                model.setId(rs.getInt("id"));
                // add model to list
                models.add(model);
            }

            rs.close();
            stmt.close();

            return models;
        } catch (SQLException e) {
            e.printStackTrace(System.err);
            return null;
        }
    }

    public List<GroupRegistration> getAllValidatedByRunAndDineId(int runAndDineId) {
        Collection<GroupRegistration> allGroupRegs = GroupRegistration.OBJECTS.getAllByRunAndDineId(runAndDineId);
        LinkedList<GroupRegistration> validatedGroupRegs = new LinkedList<GroupRegistration>();
        for (GroupRegistration gr : allGroupRegs) {
            if (gr.isIsValidated1() && gr.isIsValidated2()) {
                validatedGroupRegs.add(gr);
            }
        }
        return validatedGroupRegs;
    }

    /**
     * Stores new model or updates existing one
     *
     * @param model Model to be saved/updated
     */
    @Override
    public void save(GroupRegistration model) {
        try {
            // update model
            if (this.hasModel(model.getId())) {
                PreparedStatement stmt = this.con.prepareStatement("UPDATE " + this.table + " SET serialized_object=?, rad_id=?, validation_token1=?, validation_token2=? WHERE id=?");
                // bind values
                stmt.setObject(1, model);
                stmt.setInt(2, model.getRunAndDineId());
                stmt.setString(3, model.getValidationToken1());
                stmt.setString(4, model.getValidationToken2());
                stmt.setInt(5, model.getId());
                // execute update
                stmt.executeUpdate();
                stmt.close();
                // save new model
            } else {
                PreparedStatement stmt = this.con.prepareStatement("INSERT INTO " + this.table + " (serialized_object, rad_id, validation_token1, validation_token2) VALUES (?, ?, ?, ?)");
                // bind values to statement
                stmt.setObject(1, model);
                stmt.setInt(2, model.getRunAndDineId());
                stmt.setString(3, model.getValidationToken1());
                stmt.setString(4, model.getValidationToken2());
                // execute insert
                stmt.executeUpdate();
                // retrieve auto increment id
                ResultSet rs = stmt.getGeneratedKeys();
                rs.next();
                model.setId(rs.getInt(1));
                rs.close();
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }
    }

    /**
     * Create empty GroupRegistration
     *
     * @return Empty GroupRegistration
     */
    public GroupRegistration newObject() {
        GroupRegistration model = new GroupRegistration();

        return model;
    }
}