using SharpChat.S2CPackets;

namespace SharpChat.Commands {
    public class PasswordChannelCommand : IChatCommand {
        public bool IsMatch(ChatCommandContext ctx) {
            return ctx.NameEquals("pwd")
                || ctx.NameEquals("password");
        }

        public void Dispatch(ChatCommandContext ctx) {
            long msgId = ctx.Chat.RandomSnowflake.Next();

            if(!ctx.User.Can(ChatUserPermissions.SetChannelPassword) || ctx.Channel.IsOwner(ctx.User)) {
                ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
                return;
            }

            string chanPass = string.Join(' ', ctx.Args).Trim();

            if(string.IsNullOrWhiteSpace(chanPass))
                chanPass = string.Empty;

            ctx.Chat.UpdateChannel(ctx.Channel, password: chanPass);
            ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.CHANNEL_PASSWORD_CHANGED, false));
        }
    }
}