//
//  BetterAway.m
//  BetterAway Extension
//
//  Created by Jer Noble on 7/20/12.
//  Copyright (c) 2012 Jer Noble. All rights reserved.
//

#import "TPIBetterAway.h"
#import <Foundation/NSCharacterSet.h>

static NSString* kBetterAwayIdleSuffix = @"BetterIdleIdleSuffix";
static NSString* kBetterAwayIdleCommand = @"betteraway";

@implementation TPIBetterAway

#pragma mark -- Initialization --
- (void)pluginLoadedIntoMemory:(IRCWorld *)world
{
    _world = world;
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{ kBetterAwayIdleSuffix: @"afk"}];
    _idleSuffix = [[[NSUserDefaults standardUserDefaults] stringForKey:kBetterAwayIdleSuffix] retain];
    NSNotificationCenter* nc = [[NSWorkspace sharedWorkspace] notificationCenter];
    [nc addObserver:self selector:@selector(onDisplaySleep:) name:NSWorkspaceScreensDidSleepNotification object:nil];
    [nc addObserver:self selector:@selector(onDisplayWake:) name:NSWorkspaceScreensDidSleepNotification object:nil];
}

- (void)pluginUnloadedFromMemory
{
    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
    _world = nil;
}

#pragma mark -- Properties --
@dynamic idleSuffix;
- (NSString*)idleSuffix
{
    return _idleSuffix;
}

- (void)setIdleSuffix:(NSString*)idleSuffix
{
    [idleSuffix retain];
    [_idleSuffix release];
    _idleSuffix = idleSuffix;
    [[NSUserDefaults standardUserDefaults] setObject:idleSuffix forKey:kBetterAwayIdleSuffix];

    for (IRCClient* client in _world.clients) {
        NSMutableString* nick = [NSMutableString stringWithString:client.localNickname];
        NSString* suffix = [@"|" stringByAppendingString:[self idleSuffix]];
        [nick replaceOccurrencesOfString:suffix withString:@"" options:0 range:NSMakeRange(0, [nick length])];
        [client changeNick:nick];
    }
}

#pragma mark -- NSWorkspace Notifications --

- (void)onDisplaySleep:(id)sender
{
    for (IRCClient* client in _world.clients) {
        NSMutableString* nick = [NSMutableString stringWithString:client.localNickname];
        NSString* suffix = [@"|" stringByAppendingString:[self idleSuffix]];
        [nick replaceOccurrencesOfString:suffix withString:@"" options:0 range:NSMakeRange(0, [nick length])];
        [nick appendString:suffix];
        [client printDebugInformation:[@"BetterAway setting nickname to " stringByAppendingString:nick]];
        [client changeNick:nick];
    }
}

- (void)onDisplayWake:(id)sender
{
    for (IRCClient* client in _world.clients) {
        NSMutableString* nick = [NSMutableString stringWithString:client.localNickname];
        NSString* suffix = [@"|" stringByAppendingString:[self idleSuffix]];
        [nick replaceOccurrencesOfString:suffix withString:@"" options:0 range:NSMakeRange(0, [nick length])];
        [client printDebugInformation:[@"BetterAway setting nickname to " stringByAppendingString:nick]];
        [client changeNick:nick];
    }
}

#pragma mark -- Plugin API --
- (NSArray *)pluginSupportsUserInputCommands
{
    return @[ kBetterAwayIdleCommand ];
}

- (void)messageSentByUser:(IRCClient *)client
				  message:(NSString *)messageString
				  command:(NSString *)commandString
{
    if ([[commandString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] compare:kBetterAwayIdleCommand options:NSCaseInsensitiveSearch] == NSOrderedSame) {
        NSString* idleSuffix = [messageString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (![idleSuffix length])
            [client printDebugInformation:[@"Idle suffix is currently " stringByAppendingString:[self idleSuffix]]];
        else {
            [self setIdleSuffix:idleSuffix];
            [client printDebugInformation:[@"Set idle suffix to " stringByAppendingString:idleSuffix]];
        }
    }
}

@end

