//
//  TestStageView.m
//  OminoGobo
//
//  Created by David Van Brink on 1/9/14.
//  Copyright (c) 2014 omino. All rights reserved.
//

#import "TestStageView.h"

#import "OGModel.h"
#import "StageView.h"
#import "OmTestView.h"
#import "StageItemLayer.h"

@interface TStageListener : NSObject<StageViewListener>
{
    @public
    OGStageItem *selected;
    int changes;
}
@end

@implementation TStageListener
- (id) init
{
    self = [super init];
    self->changes = 0;
    self->selected = NULL;
    return self;
}

- (void) selectedStageItem:(OGStageItem *)newSelectedItem
{
    self->selected = newSelectedItem;
    self->changes++;
}

- (void) doubleTap
{
    // nada
}
@end


static CGRect aFrame = {0,0,800,200};
@implementation TestStageView
- (void) testAlloc
{
    StageView *sv = [[StageView alloc]initWithFrame:aFrame];
    ASSERT_NOT_NULL(sv, "stage view");
    
    OGScene *sc = [[OGScene alloc]init];
    [sv setScene:sc];
}


- (void) testItemsUpdated
{
    StageView *sv = [[StageView alloc]initWithFrame:aFrame];
    
    OGScene *sc = [[OGScene alloc]init];
    [sv setScene:sc];
    
    OGStageItem *si = [[OGStageItem alloc] init];
    [sc addStageItem:si];
    NSArray *itemLayers = sv.tLayers;
    ASSERT_EQUALS_INT(1,itemLayers.count,"layer for stage item?");

    OGStageItem *si2 = [[OGStageItem alloc] init];
    [sc addStageItem:si2];
    itemLayers = sv.tLayers;
    ASSERT_EQUALS_INT(2,itemLayers.count,"layer for stage item?");

    [sc removeStageItem:si];
    itemLayers = sv.tLayers;
    ASSERT_EQUALS_INT(1,itemLayers.count,"layer for stage item?");
    
    StageItemLayer *l0 = sv.tLayers[0];
    [l0 tick]; // advance everyone...
    ASSERT_EQUALS_FLOAT(400, l0.layer.position.x,"moved?");
    ASSERT_EQUALS_FLOAT(100, l0.layer.position.y,"moved?");

    // cause it to migrate "upwards"... by some amount...
    si2.pos = CGPointMake(1,1);
    [l0 tick]; // only migrates and the Tick.

    ASSERT_EQUALS_INT(YES,400 < l0.layer.position.x,"moved?");
    ASSERT_EQUALS_INT(YES,100 < l0.layer.position.y,"moved?");


}

- (void) testSelectedItemListening
{
    TStageListener *sl = [[TStageListener alloc] init];

    StageView *sv = [[StageView alloc]initWithFrame:aFrame];
    
    OGScene *sc = [[OGScene alloc]init];
    [sv setScene:sc];
    
    OGStageItem *si1 = [[OGStageItem alloc] init];
    [sc addStageItem:si1];
    
    OGStageItem *si2 = [[OGStageItem alloc] init];
    [sc addStageItem:si2];
    
    // start listening.
    [sv addStageListener:sl];
    
    [sv tSelectStageItem:si1];
    ASSERT_EQUALS(si1,sl->selected, "got select notification?");
    
    [sc removeStageItem:si1]; // should cause a deselect.
    ASSERT_EQUALS_INT(2,sl->changes, "removing should cause deslect");
    ASSERT_EQUALS(NULL,sl->selected, "got select notification?");
}
@end
