package com.github.games647.fastlogin.core.importer;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

public class AutoInImporter extends Importer {

    private static final String USER_TABLE = "nicknames";
    private static final String UUID_TABLE = "uuids";
    private static final String SESSION_TABLE = "sessions";

    @Override
    public int importData(DataSource source, DataSource target, String targetTable) throws SQLException {
        Connection con = null;
        Statement stmt = null;
        try {
            con = source.getConnection();
            stmt = con.createStatement();
            int importedRows = stmt.executeUpdate("INSERT INTO " + targetTable + " SELECT"
                    + " name AS Name,"
                    + " enabledLogin AS Premium,"
                    + " '' AS LastIp,"
                    + " REPLACE(puuid, '-', '') AS UUID"
                    + " FROM " + USER_TABLE
                    + " JOIN " + UUID_TABLE
                    + " ON " + UUID_TABLE + ".id = " + UUID_TABLE + ".nickname_id");
            return importedRows;
        } finally {
            closeQuietly(stmt);
            closeQuietly(con);
        }
    }
}

@@ -746,16 +746,46 @@
 able + %22
+ (Name, Premium, LastIp, UUID)
  SELECT%22
@@ -848,221 +848,572 @@
     
-+ %22 enabledLogin AS Premium,%22%0A                    + %22 '' AS LastIp,%22%0A                    + %22 REPLACE(puuid, '-', '') AS UUID%22%0A                    + %22 FROM %22 + USER_TABLE%0A                    + %22 JOIN %22 + UUID_TABLE
+/* Enable premium authentication only for those who want to be auto logged in, so%0A                    they have their cracked protection disabled */%0A                    + %22 !protection AND premium AS Premium,%22%0A                    + %22 '' AS LastIp,%22%0A                    /* Remove the dashes - returns null if puuid is null too */%0A                    + %22 REPLACE(puuid, '-', '') AS UUID%22%0A                    + %22 FROM %22 + USER_TABLE%0A                    /* Get the premium uuid */%0A                    + %22 LEFT JOIN %22 + %22 (%22%0A                    /* Prevent duplicates */
 %0A   
@@ -1436,11 +1436,21 @@
  + %22
- ON
+SELECT * FROM
  %22 +
@@ -1468,22 +1468,98 @@
  + %22
-.id =
+ GROUP BY nickname_id%22%0A                    + %22) uuids%22%0A                    + %22 ON
  %22 + U
-UID
+SER
 _TAB
@@ -1565,27 +1565,461 @@
 BLE + %22.
-nickname_id
+id = uuids.nickname_id%22);%0A%0A            /* FastLogin will also make lookups on the uuid column for name changes%0A            the old 1.6.2 version won't check if those user have premium enabled%0A%0A            so it could happen that a premium could steal the account if we don't do this%0A%0A            It seems the uuid is saved on autoin too if the player is cracked */%0A            stmt.executeUpdate(%22UPDATE %60premium%60 SET %60UUID%60=NULL WHERE PREMIUM=0
 %22);%0A    

Update AutoIn importer