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

#import "TestOmInspector.h"
#import "OmTestView.h"
#import "OmInspectorView.h"



// our test inspectable class type
@interface PType : NSObject

@property CGFloat f1;
@property CGFloat f2;

@end

@implementation PType
@end


@implementation TestOmInspector

-(void)test1
{
    ASSERT_EQUALS_INT(100, 100, "numbers?");
    ASSERT_EQUALS_INT(2,2,"OK");
}


// Helpers
+ (OmInspectorParamDesc *)addFloat:(NSMutableArray *)descs name:(NSString *)name
                               row:(int)row left:(int)left width:(int)width
{
    OmInspectorParamDesc *desc = [[OmInspectorParamDesc alloc] init];
    desc->name = name;
    desc->type = kOmInspectorParamTypeFloat;
    [descs addObject:desc];
    return desc;
}



-(void)testSliderCreation
{
    OmInspectorView *og = [[OmInspectorView alloc] initWithFrame:CGRectMake(0,0,200,200)];
    
    ASSERT_EQUALS_INT(YES, og != NULL, "alloc'd?");

    PType *p = [[PType alloc]init];
    
    NSMutableArray *propDescs = [[NSMutableArray alloc] init];
    [TestOmInspector addFloat:propDescs name:@"f1" row:0 left:0 width:10];
    [TestOmInspector addFloat:propDescs name:@"f2" row:0 left:0 width:10];
    
    [og setObject:p propertyDescs:propDescs];
    
    int sliderCount = (int) og.tPropertySliders.count;
    ASSERT_EQUALS_INT(2, sliderCount, "slider count");
}

-(void)testSlidersUpdateFromObject
{
    OmInspectorView *og = [[OmInspectorView alloc] initWithFrame:CGRectMake(0,0,200,200)];
    PType *p = [[PType alloc]init];

    NSMutableArray *propDescs = [[NSMutableArray alloc] init];
    [TestOmInspector addFloat:propDescs name:@"f1" row:0 left:0 width:10];
    [TestOmInspector addFloat:propDescs name:@"f2" row:0 left:0 width:10];
    [og setObject:p propertyDescs:propDescs];
    
    p.f1 = 0.5;
    CGFloat sliderF1 = ((UISlider *)(og.tPropertySliders[0])).value;
    ASSERT_EQUALS_FLOAT(0.5, sliderF1, "slider changed from property?");
    
    p.f1 = 0.6;
    CGFloat sliderF1b = ((UISlider *)(og.tPropertySliders[0])).value;
    ASSERT_EQUALS_FLOAT(0.6, sliderF1b, "slider changed from property?");
    
    p.f2 = 0.1;
    CGFloat sliderF2 = ((UISlider *)(og.tPropertySliders[1])).value;
    ASSERT_EQUALS_FLOAT(0.1, sliderF2, "slider changed from property?");
}

-(void)testObjectUpdatesFromSliders
{
    OmInspectorView *og = [[OmInspectorView alloc] initWithFrame:CGRectMake(0,0,200,200)];
    PType *p = [[PType alloc]init];

    NSMutableArray *propDescs = [[NSMutableArray alloc] init];
    [TestOmInspector addFloat:propDescs name:@"f1" row:0 left:0 width:10];
    [TestOmInspector addFloat:propDescs name:@"f2" row:0 left:0 width:10];
    [og setObject:p propertyDescs:propDescs];

    [og tChangeSlider:((UISlider *)(og.tPropertySliders[0])) value:0.15];
    ASSERT_EQUALS_FLOAT(0.15,p.f1, "property changed from slider?");

    [og tChangeSlider:((UISlider *)(og.tPropertySliders[0])) value:0.25];
    ASSERT_EQUALS_FLOAT(0.25,p.f1, "property changed from slider?");

    [og tChangeSlider:((UISlider *)(og.tPropertySliders[1])) value:0.45];
    ASSERT_EQUALS_FLOAT(0.45,p.f2, "property changed from slider?");
}

-(void)testInitialSliderValues
{
    OmInspectorView *og = [[OmInspectorView alloc] initWithFrame:CGRectMake(0,0,200,200)];
    PType *p1 = [[PType alloc]init];
    p1.f1 = .3;
    p1.f2 = .6;

    NSMutableArray *propDescs = [[NSMutableArray alloc] init];
    [TestOmInspector addFloat:propDescs name:@"f1" row:0 left:0 width:10];
    [TestOmInspector addFloat:propDescs name:@"f2" row:0 left:0 width:10];
    [og setObject:p1 propertyDescs:propDescs];

    CGFloat f;
    
    f = ((UISlider *)(og.tPropertySliders[0])).value;
    ASSERT_EQUALS_FLOAT(0.3,f,"got initial object value");
    f = ((UISlider *)(og.tPropertySliders[1])).value;
    ASSERT_EQUALS_FLOAT(0.6,f,"got initial object value");

}

-(void)testSettingNewObject
{
    OmInspectorView *og = [[OmInspectorView alloc] initWithFrame:CGRectMake(0,0,200,200)];
    PType *p1 = [[PType alloc]init];
    PType *p2 = [[PType alloc]init];
    CGFloat f;
    

    NSMutableArray *propDescs = [[NSMutableArray alloc] init];
    [TestOmInspector addFloat:propDescs name:@"f1" row:0 left:0 width:10];
    [TestOmInspector addFloat:propDescs name:@"f2" row:0 left:0 width:10];
    [og setObject:p1 propertyDescs:propDescs];
    
    p1.f1 = 0.6;
    f = ((UISlider *)(og.tPropertySliders[0])).value;
    ASSERT_EQUALS_FLOAT(0.6, f, "slider changed from property?");

    [og setObject:p2 propertyDescs:propDescs];
    int sliderCount = (int)og.tPropertySliders.count;
    ASSERT_EQUALS_INT(2, sliderCount, "slider count");
    
    p2.f1 = 0.7;
    f = ((UISlider *)(og.tPropertySliders[0])).value;
    ASSERT_EQUALS_FLOAT(0.7, f, "hooked on right object?");

    p2.f1 = 0.8;
    p1.f1 = 0.6;
    f = ((UISlider *)(og.tPropertySliders[0])).value;
    ASSERT_EQUALS_FLOAT(0.8, f, "hooked on right object?");

}


@end
