//
//  Vec3D.m
//  SFS2X
//
//  Created by Lapo on 28/11/13.
//  Copyright (c) 2013 A51 Integrated | http://a51integrated.com. All rights reserved.
//

#import "Vec3D.h"

@implementation Vec3D
{
    @private NSNumber* px;
    @private NSNumber* py;
    @private NSNumber* pz;
    @private BOOL useFloat;
}

+ (id) vectorWithX:(NSNumber *)x Y:(NSNumber *)y Z:(NSNumber *)z
{
    return [[Vec3D alloc] initWithX:x Y:y Z:z];
}

+ (id) vectorWithX:(NSNumber *)x Y:(NSNumber *)y
{
    return [[Vec3D alloc] initWithX:x Y:y];
}


- (id)initWithX:(NSNumber *)x Y:(NSNumber *)y Z:(NSNumber *)z;

{
    self = [super init];
    
    if (self)
    {
        px = x;
        py = y;
        pz = z;
        
        /*
         * objCType returns a (const char*)
         * We take the first char which should be 'i' for Integer and 'd' for Float/Double
         */
        useFloat = !([px objCType][0] == 'i' && [py objCType][0] == 'i' && [pz objCType][0] == 'i');
    }
    
    return self;
}

- (id)initWithX:(NSNumber *)x Y:(NSNumber *)y;
{
    self = [super init];
    
    if (self)
    {
        px = x;
        py = y;
        pz = [NSNumber numberWithInt:0];
        useFloat = !([px objCType][0] == 'i' && [py objCType][0] == 'i');
    }
    
    return self;
}

- (BOOL) isFloat
{
    return useFloat;
}

- (NSInteger)intX
{
    return [px integerValue];
}

- (NSInteger)intY
{
    return [py integerValue];
}

- (NSInteger)intZ
{
    return [pz integerValue];
}

- (NSNumber *) floatX
{
    // return a copy of the number, not the pointer to this one
    return [NSNumber numberWithFloat:[px floatValue]];
}

- (NSNumber *) floatY
{
    // return a copy of the number, not the pointer to this one
    return [NSNumber numberWithFloat:[py floatValue]];
}

- (NSNumber *) floatZ
{
    // return a copy of the number, not the pointer to this one
    return [NSNumber numberWithFloat:[pz floatValue]];
}


- (NSArray *) toArray
{
    return [NSArray arrayWithObjects:px, py, pz, nil];
}


// Internal Static constructor
+ (Vec3D *) fromArray:(NSArray*) array
{
    return [[Vec3D alloc] initWithX:[array objectAtIndex:0]
                                  Y:[array objectAtIndex:1]
                                  Z:[array objectAtIndex:2]];
}

@end
