#import <Foundation/Foundation.h>
#include <stdlib.h>

#define tokenpaster(n) NSLog (@"token" #n " = %d", token##n)

typedef unsigned char BYTE;

#define CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define CHAR_LEN 26*2

#define MAX_TABLE_SIZE 26

char getRandomChar(){
    return CHARS[arc4random_uniform(CHAR_LEN)];
}

NSString* getRandomStr(int length){
    NSString* result = @"";
    
    int i;
    for(i=0;i<length;i++){
        result = [result stringByAppendingFormat: [NSString stringWithFormat:@"%c",getRandomChar()]];
    }
    
    return result;
}

@interface UMLGenerator : NSObject
    {
        double length;
        NSString* poetry;
        
        NSString* words[7];
    }
    
    - (void)initialize;
    - (NSString*) generate:(int) l;
    - (void)dealloc;
    - (void)setLength: (int) num1;
    - (int)length;


    @property(nonatomic, readwrite) double length;
@end

@implementation UMLGenerator

    - (void)initialize {
        words[0] = @"+";
        words[1] = @"-";
        words[2] = @"#";
        words[3] = @"[STATIC] +";
        words[4] = @"[STATIC] -";
        words[5] = @"[STATIC] #";
        words[6] = @"Class";
    
    	NSLog(@"UML generator prepared for generation! \n");
    }
    
    - (void)dealloc  {
        NSLog(@"UML generator deallocated!");
        [super dealloc];
    }
    
    - (void) setLength: (int) num1{
        length = num1;
    }
    
    - (int) length {
        return length;
    }
    
    /* function returning the max between two numbers */
    - (NSString*) generate:(int) l {
        NSString * const empty_str = @" ";
        NSString * const table_left = @"|";
        NSString * const table_top = @"-";
    
        length = l;
    	poetry= @"\n";
    	int k;
    	for(k = 0;k<MAX_TABLE_SIZE;k++){
            poetry = [poetry stringByAppendingFormat: table_top];
        }
        poetry = [poetry stringByAppendingFormat: @"\n"];
    	
        
    
        /* uppercase string */
        //poetry = [str2 uppercaseString];
        
        /* concatenates str1 and str2 */ //[NSString stringWithFormat:@"%d",r]
        //poetry = [poetry stringByAppendingFormat: getRandomStr(r)];
        
        int i;
        for(i=0;i<l;i++){
            int r = 5+arc4random_uniform(6);
            int word_index = r-5;
        
            NSString* cur_line = @"";
            cur_line = [cur_line stringByAppendingFormat: table_left];
            if(i == 0){
                cur_line = [cur_line stringByAppendingFormat: words[6]];
            } else {
                cur_line = [cur_line stringByAppendingFormat: words[word_index]];
            }
            
            cur_line = [cur_line stringByAppendingFormat: empty_str];
            cur_line = [cur_line stringByAppendingFormat: getRandomStr(r)];
            
            
            int curlen = [cur_line length];
            
            int padding_size = MAX_TABLE_SIZE-curlen-1;
            int k;
            for(k = 0;k<padding_size;k++){
                cur_line = [cur_line stringByAppendingFormat: empty_str];
            }
            cur_line = [cur_line stringByAppendingFormat: table_left];
            cur_line = [cur_line stringByAppendingFormat: @"\n"];
            
            
            for(k = 0;k<MAX_TABLE_SIZE;k++){
                cur_line = [cur_line stringByAppendingFormat: table_top];
            }
            
            cur_line = [cur_line stringByAppendingFormat: @"\n"];
            
            poetry = [poetry stringByAppendingFormat: cur_line];
            
        }
    
    	return poetry;
    }

@end

int main (int argc, const char * argv[]){

	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	UMLGenerator *generator = [[UMLGenerator alloc]init];
	[generator initialize];
	
    NSLog(@"Output:   %@\n", [generator generate:10] );
    [pool drain];

	return 0;
}