/******************************************************************************
 * Copyright AllSeen Alliance. All rights reserved.
 *
 *    Permission to use, copy, modify, and/or distribute this software for any
 *    purpose with or without fee is hereby granted, provided that the above
 *    copyright notice and this permission notice appear in all copies.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 ******************************************************************************/

#import "AJCPSContainer.h"
#import "alljoyn/controlpanel/Container.h"
#import "alljoyn/controlpanel/Widget.h"
#import "AJCPSControlPanelDevice.h"
#import "AJCPSAction.h"
#import "AJCPSActionWithDialog.h"
#import "AJCPSLabel.h"
#import "AJCPSProperty.h"
#import "AJCPSDialog.h"
#import "AJCPSErrorWidget.h"

@interface AJCPSContainer ()
// uses Widget's handle
@end

@implementation AJCPSContainer

- (id)initWithHandle:(ajn::services::Container *)handle
{
	self = [super initWithHandle:handle];
	if (self) {
		//self.handle = handle;
	}
	return self;
}

/**
 * Register the BusObjects for this Widget
 * @param bus - the bus to be used
 * @return status - success/failure
 */
- (QStatus)registerObjects:(AJNBusAttachment *)bus
{
	return ((ajn::services::Container *)self.handle)->registerObjects((ajn::BusAttachment *)[bus handle]);
}

/**
 * Unregister the BusObjects for this widget
 * @param bus
 * @return status - success/failure
 */
- (QStatus)unregisterObjects:(AJNBusAttachment *)bus
{
	return ((ajn::services::Container *)self.handle)->unregisterObjects((ajn::BusAttachment *)[bus handle]);
}

/**
 * Get the ChildWidget Vector
 * @return children widgets
 */
//const std : : vector <Widget *>& getChildWidgets() const;
- (NSArray *)getChildWidgets
{
	const std::vector <ajn::services::Widget *> cpp_childWidgets = ((ajn::services::Container *)self.handle)->getChildWidgets();
    
	NSMutableArray *childWidgets = [[NSMutableArray alloc]init];
    
	for (int i = 0; i != cpp_childWidgets.size(); i++) {
        switch (cpp_childWidgets.at(i)->getWidgetType()) {
            case AJCPS_CONTAINER:
                [childWidgets addObject:[[AJCPSContainer alloc]initWithHandle:(ajn::services::Container *)cpp_childWidgets.at(i)]];

                break;
            case AJCPS_ACTION:
                [childWidgets addObject:[[AJCPSAction alloc]initWithHandle:(ajn::services::Action *)cpp_childWidgets.at(i)]];

                break;

            case AJCPS_ACTION_WITH_DIALOG:
                [childWidgets addObject:[[AJCPSActionWithDialog alloc]initWithHandle:(ajn::services::ActionWithDialog *)cpp_childWidgets.at(i)]];

                break;

            case AJCPS_LABEL:
                [childWidgets addObject:[[AJCPSLabel alloc]initWithHandle:(ajn::services::Label *)cpp_childWidgets.at(i)]];

                break;

            case AJCPS_PROPERTY:
                [childWidgets addObject:[[AJCPSProperty alloc]initWithHandle:(ajn::services::Property *)cpp_childWidgets.at(i)]];

                break;
            case AJCPS_DIALOG:
                [childWidgets addObject:[[AJCPSDialog alloc]initWithHandle:(ajn::services::Dialog *)cpp_childWidgets.at(i)]];

                 break;
                
            case AJCPS_ERROR:
                [childWidgets addObject:[[AJCPSErrorWidget alloc]initWithHandle:(ajn::services::ErrorWidget *)cpp_childWidgets.at(i)]];
                
                break;
                
            default:
                NSLog(@"Error. Request to create a widget of unknown type %d",cpp_childWidgets.at(i)->getWidgetType());
                break;
        }
    }

    
	return childWidgets;
}

- (bool)getIsDismissable
{
	return ((ajn::services::Container *)self.handle)->getIsDismissable();
}

@end