/*
 * Copyright (C) 2015 Admin
 *
 * 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 de.emc.warns;

import de.emc.config.Store;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;

/**
 *
 * @author Admin
 */
public class Warns implements Listener,CommandExecutor {
    public Plugin plugin;
    public static Connection c;
    public static Statement s;
    public Warns(Plugin plugin){
        this.plugin = plugin;
    }
    public void onEnable(){
        try {
            String user = Store.getString("sqluser");
            String pass = Store.getString("sqlpass");
            String ip = Store.getString("sqlip");
            String port = Store.getString("sqlport");
            String db = Store.getString("sqldb");
            String url = "jdbc:mysql://"+ip+":"+port+"/"+db;
            c = DriverManager.getConnection(url, user, pass);
            s = c.createStatement();
            plugin.getServer().getLogger().info(Store.getString("warnson"));
        } catch (SQLException ex) {
            Logger.getLogger(Warns.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void onDisable(){
        plugin.getServer().getLogger().info(Store.getString("warnsoff"));
    }
    @EventHandler
    public void onJoin(PlayerJoinEvent evt) throws SQLException{
        ResultSet res = s.executeQuery("SHOW DATABASES LIKE '"+evt.getPlayer().getName()+"';");
        if(!res.isBeforeFirst()){
            s.execute("CREATE TABLE "+evt.getPlayer().getName()+" (size int);");
        }
    }
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if(sender instanceof Player){
            Player p = (Player)sender;
        if(cmd.getName().equalsIgnoreCase("warn")){
            
            p.sendMessage(Store.getString("cmdexecuted"));
            return true;
        }
        if(cmd.getName().equalsIgnoreCase("warns")){
                try {
                    ResultSet res = s.executeQuery("SELECT * FROM "+p.getName());
                    res.next();
                    int size = res.getInt("size");
                    p.sendMessage("Warns");
                    if(size > 0){
                        for(int i = 0; i<size;i++){
                            String msg = res.getString("msg"+i);
                            p.sendMessage(i+": "+msg);
                        }
                    }
                    p.sendMessage("END");
                    p.sendMessage(Store.getString("cmdexecuted"));
                    return true;
                } catch (SQLException ex) {
                    Logger.getLogger(Warns.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
        }
        return false;
    }
}
