/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techcavern.wavetact.utils.events;

import com.techcavern.wavetact.utils.GetUtils;
import com.techcavern.wavetact.utils.PermUtils;
import com.techcavern.wavetact.utils.objects.GenericCommand;
import org.apache.commons.lang3.ArrayUtils;
import org.pircbotx.Channel;
import org.pircbotx.PircBotX;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.PrivateMessageEvent;

/**
 * @author jztech101
 */
public class PrivateMessageListener extends ListenerAdapter<PircBotX> {

    @Override
    public void onPrivateMessage(PrivateMessageEvent<PircBotX> event) throws Exception {
        String[] messageParts = event.getMessage().split(" ");
        String m = messageParts[0].toLowerCase();
        GenericCommand Command = GetUtils.getCommand(m);
        if (Command == null) {
            Command = GetUtils.getCommand(m.replaceFirst(GetUtils.getCommandChar(event.getBot()), ""));
        }
        messageParts = ArrayUtils.remove(messageParts, 0);
        Channel channel = null;
        if (messageParts.length >= 1 && messageParts[0].startsWith("#")) {
            channel = GetUtils.getChannelbyName(event.getBot(), messageParts[0]);
            messageParts = ArrayUtils.remove(messageParts, 0);
        }
        if (Command != null) {
            if ((Command.getPermLevel() == 9 || Command.getPermLevel() == 14 || Command.getPermLevel() == 6) || (channel != null)) {
                int UserPermLevel = PermUtils.getPermLevel(event.getBot(), event.getUser(), channel);
                if (UserPermLevel >= Command.getPermLevel()) {
                    if (Command.getPermLevel() == 9) {
                        if (channel.isOp(event.getBot().getUserBot()) || channel.isOp(event.getBot().getUserBot()) || channel.isOwner(event.getBot().getUserBot())) {
                            Command.onCommand(event.getUser(), event.getBot(), channel, true, UserPermLevel, messageParts);
                        } else {
                            event.getUser().send().notice("Error: I must be at op or higher to perform the operation requested");
                        }
                    } else if (Command.getPermLevel() == 14) {

                        if (channel.isOwner(event.getBot().getUserBot())) {
                            Command.onCommand(event.getUser(), event.getBot(), channel, true, UserPermLevel, messageParts);
                        } else {
                            event.getUser().send().notice("Error: I must be owner to perform the operation requested");
                        }
                    } else if (Command.getPermLevel() == 6) {
                        if (channel.isOwner(event.getBot().getUserBot()) || channel.isOp(event.getBot().getUserBot()) || channel.isHalfOp(event.getBot().getUserBot()) || channel.isSuperOp(event.getBot().getUserBot())) {
                            Command.onCommand(event.getUser(), event.getBot(), channel, true, UserPermLevel, messageParts);
                        } else {
                            if(event.getBot().getServerInfo().getPrefixes().contains("h")) {
                                event.getUser().send().notice("Error: I must be half-op or higher to perform the operation requested");
                            }else{
                                event.getUser().send().notice("Error: I must be op or higher to perform the operation requested");
                            }
                        }
                    } else {
                        Command.onCommand(event.getUser(), event.getBot(), channel, true, UserPermLevel, messageParts);
                    }

                } else {
                    event.getUser().send().message("Permission Denied");
                }
            } else {
                event.getUser().send().message("Channel Must be Specified as argument #1");
            }
        }
    }
}




@@ -1498,16 +1498,17 @@
 l() 
-== 9 %7C%7C 
+%3E= 0 && (
 Comm
@@ -1526,21 +1526,20 @@
 Level() 
-== 14
+%3C= 5
  %7C%7C Comm
@@ -1557,20 +1557,20 @@
 Level() 
-== 6
+%3E 18
 ) %7C%7C (ch
@@ -1584,16 +1584,17 @@
 = null))
+)
  %7B%0A     

Fix Chan Requirements Checking for PMs