package com.forgeessentials.playerlogger.types;

import com.forgeessentials.playerlogger.ModulePlayerLogger;
import com.forgeessentials.util.OutputHandler;
import com.google.common.base.Charsets;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

import javax.sql.rowset.serial.SerialBlob;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BlockChangeType extends LogType {
    public BlockChangeType(blockChangeLogCategory cat, EntityPlayer player, String block, int X, int Y, int Z, TileEntity te)
    {
        super();
        SerialBlob teBlob = null;
        if (te != null)
        {
            NBTTagCompound nbt = new NBTTagCompound();
            te.writeToNBT(nbt);
            nbt.setString("TE_CLASS", te.getClass().getCanonicalName());

            try
            {
                teBlob = new SerialBlob(nbt.toString().getBytes(Charsets.UTF_8));
            }
            catch (Exception e)
            {
                OutputHandler.felog.severe(e.toString());
                e.printStackTrace();
            }
        }

        try
        {
            PreparedStatement ps = ModulePlayerLogger.getConnection().prepareStatement(getprepareStatementSQL());
            ps.setString(1, player.getPersistentID().toString());
            ps.setString(2, cat.toString());
            ps.setString(3, block);
            ps.setInt(4, player.dimension);
            ps.setInt(5, X);
            ps.setInt(6, Y);
            ps.setInt(7, Z);
            ps.setTimestamp(8, time);
            ps.setBlob(9, teBlob);
            ps.execute();
            ps.clearParameters();
            ps.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public BlockChangeType()
    {
        super();
    }

    @Override
    public String getName()
    {
        return "blockChange";
    }

    @Override
    public String getTableCreateSQL()
    {
        return "CREATE TABLE IF NOT EXISTS " + getName()
                + "(id INT UNSIGNED NOT NULL AUTO_INCREMENT,PRIMARY KEY (id), player VARCHAR(255), category VARCHAR(32), block VARCHAR(32), Dim INT, X INT, Y INT, Z INT, time DATETIME, te LONGBLOB)";
    }

    @Override
    public String getprepareStatementSQL()
    {
        return "INSERT INTO " + getName() + " (player, category, block, Dim, X, Y, Z, time, te) VALUES (?,?,?,?,?,?,?,?,?);";
    }

    public enum blockChangeLogCategory {
        broke, placed, interact
    }
}

@@ -1379,16 +1379,76 @@
 tring(2,
+ player.getCommandSenderName());%0A            ps.setString(3,
  cat.toS
@@ -1482,17 +1482,17 @@
 tString(
-3
+4
 , block)
@@ -1515,17 +1515,17 @@
 .setInt(
-4
+5
 , player
@@ -1559,17 +1559,17 @@
 .setInt(
-5
+6
 , X);%0A  
@@ -1588,17 +1588,17 @@
 .setInt(
-6
+7
 , Y);%0A  
@@ -1617,17 +1617,17 @@
 .setInt(
-7
+8
 , Z);%0A  
@@ -1652,17 +1652,17 @@
 mestamp(
-8
+9
 , time);
@@ -1685,17 +1685,18 @@
 setBlob(
-9
+10
 , teBlob
@@ -2240,16 +2240,39 @@
 AR(255),
+ username VARCHAR(255),
  categor
@@ -2489,16 +2489,26 @@
 (player,
+ username,
  categor
@@ -2566,16 +2566,18 @@
 ,?,?,?,?
+,?
 );%22;%0A   

Add a friendlyName column to PL which shows the player's username - should close #1292