/*
 File: LiveSupportView.m
 Abstract: The live support view that wraps up the CJGuestCall views.
 
 Copyright (c) 2013 Cisco Systems, Inc. All Rights Reserved.
 
 */

#import "LiveSupportView.h"

#import <JabberGuest/JabberGuest.h>

@interface LiveSupportView () {
    UIImageView * remoteView;
    UIActivityIndicatorView * aiView;
    CJGuestCallBarView * callBar;
    BOOL callTriedToConnect;
}
@end

@implementation LiveSupportView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        remoteView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [remoteView setBackgroundColor:[UIColor blackColor]];
        [remoteView setContentMode:UIViewContentModeScaleAspectFill];
        [remoteView setClipsToBounds:YES];
        [self addSubview:remoteView];
        
        aiView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        
        CGRect aiFrame = aiView.frame;
        aiFrame.origin.x = remoteView.frame.origin.x + ((remoteView.frame.size.width - aiView.frame.size.width) / 2.0);
        aiFrame.origin.y = remoteView.frame.origin.y + ((remoteView.frame.size.width - aiView.frame.size.width) / 2.0);
        aiView.frame = aiFrame;
        
        [self addSubview:aiView];
        [aiView startAnimating];
        
        callTriedToConnect = FALSE;
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleCallStateChange:)
                                                     name:CJGuestCallStateChangeNotification
                                                   object:nil];
        
        CJGuestCall * call = [CJGuestCall sharedInstance];
        [call setServerName:@"your.server.name.goes.here"];
        [call setToURI:@"your.uri.goes.here"];
        [call startCall];
    }
    return self;
}

- (void)layoutCJGuestCallViews
{
    [[CJGuestCall sharedInstance] setRemoteView:remoteView];
    
    CGPoint callBarOrigin;
    callBarOrigin.x = remoteView.frame.origin.x + ((remoteView.frame.size.width - (CJGuestCallBarButtonWidth * 2)) / 2.0);
    callBarOrigin.y = remoteView.frame.origin.y + remoteView.frame.size.height - CJGuestCallBarButtonHeight;
    callBar = [[CJGuestCallBarView alloc] initWithOrigin:callBarOrigin
                                                    mask:GuestCallBarButtonOptionAudioMute | GuestCallBarButtonOptionHook];
    callBar.delegate = self;
    [self addSubview:callBar];
}

- (void)handleCallStateChange:(NSNotification *)notification
{
    CJGuestCall * call = [CJGuestCall sharedInstance];
    
    switch (call.callState) {
        case GuestCallStateConnecting:
            callTriedToConnect = TRUE;
            break;
        case GuestCallStateConnected:
            [aiView stopAnimating];
            [self layoutCJGuestCallViews];
            break;
        case GuestCallStateDisconnecting:
        case GuestCallStateDisconnected:
            if (callTriedToConnect)
                [_delegate supportCallIsFinished];
            break;
        default:
            break;
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:CJGuestCallStateChangeNotification
                                                  object:nil];
}

@end
