//
//  RSTextFieldCell.m
//  RSCustomViews
//
//  Created by Mike Mayo on 1/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RSTextFieldCell.h"
#import "UIColor+MoreColors.h"


@implementation RSTextFieldCell

@synthesize textField, modalPresentationStyle, fixedTextLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initializeSubviews:style withFixedTextLabel:nil];
    }
    return self;
}

- (id)initCellWithFixedLabel:(NSString *)fixedLabelText withStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initializeSubviews:style withFixedTextLabel:fixedLabelText];
    }
    return self;
}
    
- (void)initializeSubviews:(UITableViewCellStyle)style withFixedTextLabel:(NSString *)fixedLabelText {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    textField = [[UITextField alloc] initWithFrame:self.detailTextLabel.frame];
    textField.textAlignment = self.detailTextLabel.textAlignment;
    textField.returnKeyType = UIReturnKeyDone;
    textField.backgroundColor = [UIColor clearColor];
    textField.adjustsFontSizeToFitWidth = NO;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    if (style == UITableViewCellStyleValue1) {
        textField.font = [UIFont systemFontOfSize:17.0];
    } else if (style == UITableViewCellStyleValue2) {
        textField.font = [UIFont boldSystemFontOfSize:15.0];
    }

    if (self.detailTextLabel) {
        textField.textColor = self.detailTextLabel.textColor;        
    }
    [self addSubview:textField];
    
    self.detailTextLabel.textColor = [UIColor clearColor];
    self.detailTextLabel.backgroundColor = [UIColor clearColor];
    self.detailTextLabel.highlightedTextColor = [UIColor clearColor];
    self.detailTextLabel.text = @"Using a very long string here to make sure that the UILabel is rendered at the maximum width so we can copy it for the UITextField.";  
    
    if (fixedLabelText) {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            fixedTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(21.0, 14.5, 458.0, 18.0)];
        } else {
            fixedTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(21.0, 14.5, 280.0, 18.0)];
        }    
        fixedTextLabel.font = [UIFont systemFontOfSize:17.0];
        fixedTextLabel.textColor = [UIColor value1DetailTextLabelColor];
        fixedTextLabel.backgroundColor = [UIColor clearColor];
        fixedTextLabel.textAlignment = UITextAlignmentRight;
        fixedTextLabel.text = fixedLabelText;
        [self addSubview:fixedTextLabel];
    }
}


- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGRect aRect = self.detailTextLabel.frame;
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (modalPresentationStyle == UIModalPresentationFormSheet) {
            textField.frame = CGRectMake(aRect.origin.x + 31.0, aRect.origin.y + 1.0, aRect.size.width - 24, aRect.size.height);
        } else {
            textField.frame = CGRectMake(aRect.origin.x + 35.0, aRect.origin.y + 1.0, aRect.size.width - 24, aRect.size.height);
        }
    } else {
        textField.frame = CGRectMake(aRect.origin.x + 10.0, aRect.origin.y + 1.0, aRect.size.width, aRect.size.height);
    }
    
    if (fixedTextLabel) {
        int xOffset;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            xOffset = 5;
        else
            xOffset = 3;
        CGSize fixedLabelTextSize = [fixedTextLabel.text sizeWithFont:fixedTextLabel.font constrainedToSize:CGSizeMake(280.0, 900.0f)];
        textField.frame = CGRectMake(textField.frame.origin.x,
                                     textField.frame.origin.y,
                                     textField.frame.size.width - fixedLabelTextSize.width + xOffset,
                                     textField.frame.size.height);
    }
    
    // this isn't generic.  this is for text decoration on the end of the textField
    if (self.accessoryView) {
        aRect = textField.frame;
        aRect.origin.x += 9.0;
        textField.frame = aRect;
    }
    
    textField.contentStretch = self.detailTextLabel.contentStretch;
    textField.backgroundColor = [UIColor clearColor];
    textField.transform = self.detailTextLabel.transform;
    textField.clipsToBounds = self.detailTextLabel.clipsToBounds;
    textField.clearsContextBeforeDrawing = self.detailTextLabel.clearsContextBeforeDrawing;
    textField.contentMode = self.detailTextLabel.contentMode;
    textField.autoresizingMask = self.detailTextLabel.autoresizingMask;
    textField.autoresizesSubviews = YES;
    textField.font = self.detailTextLabel.font;
}

- (void)dealloc {
    [textField release];
    [fixedTextLabel release];
    [super dealloc];
}

@end
