#import "RootViewController.h"
#import "CTRentalProperty.h"

@implementation RootViewController

- (void)viewDidLoad
{
   [super viewDidLoad];
   NSString *path = [[NSBundle mainBundle] pathForResource:@"CityMappings"
                                                    ofType:@"plist"];
   cityMappings = [[NSDictionary alloc] initWithContentsOfFile:path];

   allProperties = [[NSArray alloc] initWithObjects:
      [CTRentalProperty rentalPropertyOfType:TownHouse
                                  rentingFor:420.0f
                                   atAddress:@"ul. Wiejska 13, Gdańsk"],
      [CTRentalProperty rentalPropertyOfType:Unit
                                  rentingFor:365.0f
                                   atAddress:@"ul. Rogowa 74, Bydgoszcz"],
      [CTRentalProperty rentalPropertyOfType:Unit
                                  rentingFor:275.9f
                                   atAddress:@"ul. Kwiatowa 17, Barycz"],
      [CTRentalProperty rentalPropertyOfType:Mansion
                                  rentingFor:1500.0f
                                   atAddress:@"ul. Dobra 4, Gdańsk"],
      [CTRentalProperty rentalPropertyOfType:Mansion
                                  rentingFor:2000.0f
                                   atAddress:@"ul. Nowa 19, Klifowo"],
      nil];
   filteredProperties = [[NSMutableArray alloc] initWithArray:allProperties];

   self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]
      initWithTitle:@"Filtr"
              style:UIBarButtonItemStylePlain
             target:self
             action:@selector(filterList)];
}

- (void)filterList
{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Filtr" 
                                                   message:nil
                                                  delegate:self
                                         cancelButtonTitle:@"Anuluj"
                                         otherButtonTitles:nil];

   [alert addButtonWithTitle:@"Wszystkie"];
   [alert addButtonWithTitle:@"Nieruchomości przy drogach"];
   [alert addButtonWithTitle:@"Za mniej niż 300 zł tygodniowo"];
   [alert addButtonWithTitle:@"W cenie od 250 do 450 zł tygodniowo"];

   [alert show];
   [alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if (buttonIndex != 0)
   {
      NSPredicate *predicate;
      switch (buttonIndex) {
         case 1:
            predicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
            break;
         case 2:
            predicate = [NSPredicate predicateWithFormat:@"address CONTAINS 'Road'"];
            break;
         case 3:
            predicate = [NSPredicate predicateWithFormat:@"rentalPrice < 300"];
            break;
         case 4:
            predicate = [NSPredicate predicateWithFormat:
               @"rentalPrice BETWEEN { 250, 450 }"];
            break;
      }
      [filteredProperties release];
      filteredProperties = [[allProperties 
         filteredArrayUsingPredicate:predicate] retain];
      [self.tableView reloadData];
   }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return [filteredProperties count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView
      dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                     reuseIdentifier:CellIdentifier] autorelease];
   }

   CTRentalProperty *property = [filteredProperties objectAtIndex:indexPath.row];

   int indexOfComma = [property.address rangeOfString:@","].location;
   NSString *address = [property.address substringToIndex:indexOfComma];
   NSString *city = [property.address substringFromIndex:indexOfComma + 2];

   cell.textLabel.text = address;

   NSString *imageName = [cityMappings objectForKey:city];
   cell.imageView.image = [UIImage imageNamed:imageName];

   cell.detailTextLabel.text =
      [NSString stringWithFormat:@"Nieruchomości za %0.2f zł tygodniowo",
         property.rentalPrice];

   return cell;
}

- (void)dealloc
{
   [cityMappings release];
   [allProperties release];
   [filteredProperties release];
   [super dealloc];
}

@end
