#import "PrefTable.h"

@implementation PrefTable
- (id)initWithFrame:(CGRect)rect {
    if ((self == [ super initWithFrame: rect ]) != nil) {
        int i, j;

        for(i=0;i<NUM_GROUPS;i++) {
            groupcell[i] = NULL;
            for(j=0;j<CELLS_PER_GROUP;j++)
                cells[i][j] = NULL;
        }

        [ self setDataSource: self ];
        [ self setDelegate: self ];
    }

    return self;
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)aTable {

    /* Number of logical groups, including labels */
    return NUM_GROUPS;
}

 - (int)preferencesTable:(UIPreferencesTable *)aTable
    numberOfRowsInGroup:(int)group
{
    switch (group) {
        case 0:
            return 1;
            break;
        case 1:
            return 2;
            break;
    }
}

- (UIPreferencesTableCell *)preferencesTable:
    (UIPreferencesTable *)aTable
    cellForGroup:(int)group
{
     if (groupcell[group] != NULL)
         return groupcell[group];

     groupcell[group] = [ [ UIPreferencesTableCell alloc ] init ];
     switch (group) {
         case 0:
             [ groupcell[group] setTitle: @"Presentation" ];
             break;
         case 1:
             [ groupcell[group] setTitle: @"Search" ];
             break;
     }
     return groupcell[group];
}

- (float)preferencesTable:(UIPreferencesTable *)aTable
    heightForRow:(int)row
    inGroup:(int)group
    withProposedHeight:(float)proposed
{
    /* Return height for group titles */
    if (row == -1) {
        if (group < 2)
            return 40;
    }

    if (group == 0 && row == 0) {
      return 55.0;
    }


    return proposed;
}

- (BOOL)preferencesTable:(UIPreferencesTable *)aTable
    isLabelGroup:(int)group
{
    return NO;
}

-(UIPreferencesTableCell*) preferencesTable:
    (UIPreferencesTable *)aTable
    cellForRow:(int)row
    inGroup:(int)group
{
    UIPreferencesTableCell *cell;

    if (cells[group][row] != NULL)
        return cells[group][row];

    cell = [ [ UIPreferencesTableCell alloc ] init ];
    [ cell setEnabled: YES ];

    switch (group) {
        case (0):
            switch (row) {
                case (0):
                    [ cell setTitle:@"Language" ];
                    langControl = [ [ UISegmentedControl alloc ]
                        initWithFrame:CGRectMake(170, 5, 135, 50)
                    ];
		    [ langControl insertSegment:0
                        withTitle:@"English" animated: NO ];
                    [ langControl insertSegment:1
                        withTitle:@"French" animated: NO ];
                    [ langControl selectSegment: 0 ];
                    [ cell addSubview: langControl ];
                    break;
	    }
	    break;
        case (1):
            switch (row) {
                case (0):
                    [ cell setTitle:@"Stemming" ];
                    stemmingControl = [ [ UISwitchControl alloc ]
                        initWithFrame:CGRectMake(170, 5, 120, 55)
                    ];
                    [ stemmingControl setValue: 0.0 ];
                    [ cell addSubview: stemmingControl ];
                    break;
                case (1):
                    [ cell setTitle:@"Case matching" ];
                    caseControl = [ [ UISwitchControl alloc ]
                        initWithFrame:CGRectMake(170, 5, 120, 30)
                    ];
                    [ caseControl setValue: 0.0 ];
                    [ caseControl setAlternateColors: NO ];
                    [ cell addSubview: caseControl ];
                    break;
                case (2):
                    [ cell setTitle:@"Case matching" ];
                    caseControl = [ [ UISwitchControl alloc ]
                        initWithFrame:CGRectMake(170, 5, 120, 30)
                    ];
                    [ caseControl setValue: 0.0 ];
                    [ caseControl setAlternateColors: NO ];
                    [ cell addSubview: caseControl ];
                    break;
            }
            break;
	    
    }

    [ cell setShowSelection: NO ];
    cells[group][row] = cell;
    return cell;
}

-(NSString*) languagePref
{
  NSString* lang = [NSString alloc];

  if (langControl.selectedSegment==0) {
    lang = [lang initWithString:@"en"];
  }
  else {
    lang = [lang initWithString:@"fr"];
  }

  return lang;
}

@end
