#import "ImperialObject.h"

@implementation ImperialObject

- (void)awakeFromNib
// This is one of the last methods executed after the nib file is loaded.
// This is the Best way to initialize an object and jump into
// a method after the nib file was loaded.
{
        [BMIPanel center];
        [BMIPanel makeKeyAndOrderFront:NULL];
        [WeightTextField selectText:self];
}


- (void)Calc:(id)sender
{

    // Decalare Variables

        float weight;
        float height;
        float heightInt;
        float index;
        float gender;
        float agegroup;
        float dummyValue;

    // Assign variables

         heightInt = [HeightTextField intValue];
            weight = [WeightTextField intValue];

    // Calculatate BMI and display BMI

            // Convert Inches into feet
            height = heightInt / 12;

            // From Pound to Kilogram
            dummyValue = (weight*0.453);
            weight = dummyValue;

            // From Feet to Meter
            dummyValue = (0.3048*height);
            height = dummyValue;

            index = (weight/(height*height));

            // Display BMI index number
            
            	// Sets TextField as a NSFloatType
            	[[IndexTextField cell] setEntryType:NSFloatType];
                
            	// Format the TextField with two number at the left of the floating point
            	// and two numbers at the right of the floating point.
            	[[IndexTextField cell] setFloatingPointFormat:NO left:2 right:2];
                
            	[IndexTextField setDoubleValue:index];// Display result
            	[IndexTextField display];
            
            	// Used for MiscKit Gauge  [IndexGauge setDoubleValue:index];// Display result
            	// Used for MiscKit Gauge  [IndexGauge display];


            // Gender Male = 1, female = 2
            gender = [GenderButton selectedTag];

            // Age Group < 18 = 1, 20-29 = 2, >29 = 3
            agegroup = [AgeGroupButton selectedTag];

            // Figure out in what category the person is in
            // and display the status for that person.

            // Below 18, male, normal
            if ((agegroup == 1) && (gender == 1) && (index < 22))	
            {
                [StatusTextField setStringValue:@"Normal"];
                [StatusTextField setTextColor:[NSColor greenColor]];
            };

            // Below 18, male, overweight
            if ((agegroup == 1) && (gender == 1) && (index > 22))	
            {
                [StatusTextField setStringValue:@"Over weight"];
                [StatusTextField setTextColor:[NSColor redColor]];
            };

            // Below 18, female, normal
            if ((agegroup == 1) && (gender == 2) && (index < 20))	
            {
                [StatusTextField setStringValue:@"Normal"];
                [StatusTextField setTextColor:[NSColor greenColor]];
            };

            // Below 18, female, overweight
            if ((agegroup == 1) && (gender == 2) && (index > 20))	
            {
                [StatusTextField setStringValue:@"Over weight"];
                [StatusTextField setTextColor:[NSColor redColor]];
            };



            // 20-29, Male, under weight
            if ((agegroup == 2) && (gender == 1) && (index < 20))	
            {
                [StatusTextField setStringValue:@"Under weight"];
                [StatusTextField setTextColor:[NSColor yellowColor]];
            };

            // 20-29, male, normal
            if ((agegroup == 2) && (gender == 1) && (index < 27.8) && (index > 20))
            {
                [StatusTextField setStringValue:@"Normal"];
                [StatusTextField setTextColor:[NSColor greenColor]];
            };

            // 20-29, male, over weight
            if ((agegroup == 2) && (gender == 1) && (index > 27.8))
            {
                [StatusTextField setStringValue:@"Over weight"];
                [StatusTextField setTextColor:[NSColor redColor]];
            };

            // 20-29, female, under weight
            if ((agegroup == 2) && (gender == 2) && (index < 20))	
            {
                [StatusTextField setStringValue:@"Under weight"];
                [StatusTextField setTextColor:[NSColor yellowColor]];
            };


            // 20-29, female, normal
            if ((agegroup == 2) && (gender == 2) && (index < 27.3) && (index > 20))
            {
                [StatusTextField setStringValue:@"Normal"];
                [StatusTextField setTextColor:[NSColor greenColor]];
            };

            // 20-29, female, over weight
            if ((agegroup == 2) && (gender == 2) && (index > 27.3))
            {
                [StatusTextField setStringValue:@"Over weight"];
                [StatusTextField setTextColor:[NSColor redColor]];
            };



            // Over 29, Male, under weight
            if ((agegroup == 3) && (gender == 1) && (index < 20))	
            {
                [StatusTextField setStringValue:@"Under weight"];
                [StatusTextField setTextColor:[NSColor yellowColor]];
            };


            // Over 29, male, normal
            if ((agegroup == 3) && (gender == 1) && (index < 25) && (index > 20))
            {
                [StatusTextField setStringValue:@"Normal"];
                [StatusTextField setTextColor:[NSColor greenColor]];
            };

            // Over 29, male, over weight
            if ((agegroup == 3) && (gender == 1) && (index > 25))
            {
                [StatusTextField setStringValue:@"Over weight"];
                [StatusTextField setTextColor:[NSColor redColor]];
            };

            // Over 29, female, under weight
            if ((agegroup == 3) && (gender == 2) && (index < 20))	
            {
                [StatusTextField setStringValue:@"Under weight"];
                [StatusTextField setTextColor:[NSColor yellowColor]];
            };


            // Over 29, female, normal
            if ((agegroup == 3) && (gender == 2) && (index < 24) && (index > 20))
            {
                [StatusTextField setStringValue:@"Normal"];
                [StatusTextField setTextColor:[NSColor greenColor]];
            };

            // Over 29, female, over weight
            if ((agegroup == 3) && (gender == 2) && (index > 24))
            {
                [StatusTextField setStringValue:@"Over weight"];
                [StatusTextField setTextColor:[NSColor redColor]];
            };

    
    
}

- (void)HeightRadioInput:(id)sender
{

    // Assign variables
    int feetinput;
    int inchesinput;
    float convertedinches;
    float height;

    // Assigns the value of the currently selected button
    feetinput = [FeetButton selectedTag];

    // Assigns the value of the currently selected button
    inchesinput = [InchesButton selectedTag];

    // Convert inches fraction (5/10) to floating point (0.50)

    convertedinches = feetinput * 12;
    height = inchesinput + convertedinches;

    // Display Height in the Height textfield
    [HeightTextField setIntValue:height];// Display result
    [HeightTextField display];

    
}

@end
