Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SearchView.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 25f8c4963e34b7fc7e299dffcd0ef8c3a114dd56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#import "Common.h"
#import "Framework.h"
#import "LocationManager.h"
#import "LocationManager.h"
#import "MapsAppDelegate.h"
#import "MapViewController.h"
#import "MWMMapViewControlsManager.h"
#import "SearchCategoryCell.h"
#import "SearchResultCell.h"
#import "SearchShowOnMapCell.h"
#import "SearchSuggestCell.h"
#import "SearchView.h"
#import "Statistics.h"
#import "ToastView.h"
#import "UIColor+MapsMeColor.h"
#import "UIFont+MapsMeFonts.h"
#import "UIKitCategories.h"

#include "geometry/angles.hpp"
#include "geometry/distance_on_sphere.hpp"

#include "indexer/mercator.hpp"

#include "platform/platform.hpp"
#include "platform/preferred_languages.hpp"
#include "platform/settings.hpp"

#include "search/params.hpp"
#include "search/result.hpp"

@interface SearchResultsWrapper : NSObject

- (id)initWithResults:(search::Results const &)res;

- (search::Result const &)resultWithPosition:(NSInteger)position;
- (NSInteger)suggestsCount;
- (NSInteger)count;
- (BOOL)isEndMarker;
- (BOOL)isEndedNormal;

@end

@interface SearchResultsWrapper ()

@property (nonatomic) NSMutableDictionary * distances;

@end

@implementation SearchResultsWrapper
{
  search::Results m_results;
}

- (id)initWithResults:(search::Results const &)results
{
  self = [super init];

  m_results = results;

  return self;
}

- (NSMutableDictionary *)distances
{
  if (!_distances)
    _distances = [[NSMutableDictionary alloc] init];
  return _distances;
}

- (NSInteger)count
{
  return m_results.GetCount();
}

- (NSInteger)suggestsCount
{
  return m_results.GetSuggestsCount();
}

- (search::Result const &)resultWithPosition:(NSInteger)position
{
  return m_results.GetResult(position);
}

- (BOOL)isEndMarker
{
  return m_results.IsEndMarker();
}

- (BOOL)isEndedNormal
{
  return m_results.IsEndedNormal();
}

@end


typedef NS_ENUM(NSUInteger, CellType)
{
  CellTypeResult,
  CellTypeSuggest,
  CellTypeShowOnMap,
  CellTypeCategory
};


@interface SearchView () <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, SearchBarDelegate, LocationObserver>

@property (nonatomic) UITableView * tableView;
@property (nonatomic) SolidTouchView * topBackgroundView;
@property (nonatomic) UILabel * emptyResultLabel;

@property (nonatomic) SearchResultsWrapper * wrapper;
@property (nonatomic) NSArray * categoriesNames;

@end

@implementation SearchView

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];

  [self addSubview:self.tableView];
  [self addSubview:self.topBackgroundView];
  self.topBackgroundView.height = [self defaultTopBackgroundHeight];
  [self addSubview:self.searchBar];
  [self.tableView addSubview:self.emptyResultLabel];

  self.emptyResultLabel.center = CGPointMake(self.width / 2, 40);
  self.emptyResultLabel.hidden = YES;

  self.searchBar.midX = self.width / 2;

  [self setState:SearchViewStateHidden animated:NO];

  [self.tableView registerClass:[SearchCategoryCell class] forCellReuseIdentifier:[SearchCategoryCell className]];
  [self.tableView registerClass:[SearchResultCell class] forCellReuseIdentifier:[SearchResultCell className]];
  [self.tableView registerClass:[SearchSuggestCell class] forCellReuseIdentifier:[SearchSuggestCell className]];
  [self.tableView registerClass:[SearchShowOnMapCell class] forCellReuseIdentifier:[SearchShowOnMapCell className]];

  [self layoutSubviews];
  self.tableView.contentOffset = CGPointMake(0, -self.topBackgroundView.height);

  return self;
}

static BOOL keyboardLoaded = NO;

- (void)setState:(SearchViewState)state animated:(BOOL)animated
{
  [self.delegate searchViewWillEnterState:state];
  // Clear search results on the map when clear in the search bar on the map is pressed
  if (_state == SearchViewStateResults && state == SearchViewStateHidden)
    [self clearSearchResultsMode];

  UIViewAnimationOptions const options = UIViewAnimationOptionCurveEaseInOut;
  double const damping = 0.9;
  NSTimeInterval const duration = animated ? 0.3 : 0;
  CGFloat const searchBarOffset = (state == SearchViewStateHidden) ? -self.searchBar.height : [self defaultSearchBarMinY];

  CGFloat const cancelButtonMinX = (state == SearchViewStateResults) ? self.searchBar.width - 4 : self.searchBar.cancelButton.minX;
  CGFloat const textFieldBackgroundWidth = cancelButtonMinX - self.searchBar.fieldBackgroundView.minX - 8;
  CGFloat const textFieldWidth = textFieldBackgroundWidth - 60.;

  LocationManager const * const locationManager = [MapsAppDelegate theApp].m_locationManager;
  if (state == SearchViewStateFullscreen)
  {
    [locationManager start:self];

    GetFramework().PrepareSearch();

    if (keyboardLoaded)
      [self.searchBar.textField becomeFirstResponder];
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
      self.tableView.alpha = 1;
    } completion:nil];
    [UIView animateWithDuration:duration delay:0 damping:damping initialVelocity:0 options:options animations:^{
      self.searchBar.cancelButton.alpha = 1;
      self.topBackgroundView.minY = 0;
      self.topBackgroundView.height = [self defaultTopBackgroundHeight];
      self.searchBar.minY = searchBarOffset;
      self.searchBar.alpha = 1;
      self.searchBar.fieldBackgroundView.width = textFieldBackgroundWidth;
      self.searchBar.textField.width = textFieldWidth;
      [self.searchBar.clearButton setImage:[UIImage imageNamed:@"SearchBarClearButton"] forState:UIControlStateNormal];
    } completion:^(BOOL) {
      if (!keyboardLoaded)
      {
        keyboardLoaded = YES;
        [self.searchBar.textField becomeFirstResponder];
      }
    }];
  }
  else if (state == SearchViewStateResults)
  {
    [locationManager stop:self];
    [self.searchBar.textField resignFirstResponder];
    [UIView animateWithDuration:duration delay:0 damping:damping initialVelocity:0 options:options animations:^{
      self.alpha = 1;
      self.searchBar.cancelButton.alpha = 0;
      self.searchBar.alpha = 1;
      self.topBackgroundView.minY = 0;
      if ([self iPhoneInLandscape])
      {
        self.searchBar.minY = searchBarOffset - 3;
        self.topBackgroundView.height = [self defaultTopBackgroundHeight] - 10;
      }
      else
      {
        self.searchBar.minY = searchBarOffset;
        self.topBackgroundView.height = [self defaultTopBackgroundHeight];
      }
      self.tableView.alpha = 0;
      self.searchBar.fieldBackgroundView.width = textFieldBackgroundWidth;
      self.searchBar.textField.width = textFieldWidth;
      [self.searchBar.clearButton setImage:[UIImage imageNamed:@"SearchBarClearResultsButton"] forState:UIControlStateNormal];
    } completion:nil];
  }
  else if (state == SearchViewStateHidden)
  {
    [locationManager stop:self];
    [self.searchBar.textField resignFirstResponder];
    [UIView animateWithDuration:duration delay:0 damping:damping initialVelocity:0 options:options animations:^{
      self.searchBar.cancelButton.alpha = 1;
      self.searchBar.maxY = 0;
      self.searchBar.alpha = 0;
      self.topBackgroundView.maxY = 0;
      self.tableView.alpha = 0;
      self.searchBar.fieldBackgroundView.width = textFieldBackgroundWidth;
      self.searchBar.textField.width = textFieldWidth;
      [self.searchBar.clearButton setImage:[UIImage imageNamed:@"SearchBarClearButton"] forState:UIControlStateNormal];
    } completion:nil];
  }
  else if (state == SearchViewStateAlpha)
  {
    [UIView animateWithDuration:duration delay:0.1 damping:damping initialVelocity:0 options:options animations:^{
      self.alpha = 0;
    } completion:nil];
    [self.searchBar.textField resignFirstResponder];
  }
  GetFramework().Invalidate();
  _state = state;
  [self.delegate searchViewDidEnterState:state];
}

- (void)onLocationError:(location::TLocationError)errorCode
{
  NSLog(@"Location error %i in %@", errorCode, self);
}

- (void)onLocationUpdate:(location::GpsInfo const &)info
{
  if (self.state == SearchViewStateFullscreen && ![self isShowingCategories])
  {
    search::SearchParams params;
    [self updateSearchParametersWithForce:NO outParams:params andQuery:self.searchBar.textField.text];
    params.SetPosition(info.m_latitude, info.m_longitude);
    GetFramework().Search(params);

    [self recalculateDistances];
    [self.tableView reloadData];
  }
}

- (void)recalculateDistances
{
  LocationManager * locationManager = [MapsAppDelegate theApp].m_locationManager;

  double north = -1.0;
  [locationManager getNorthRad:north];
  double lat, lon;
  if ([locationManager getLat:lat Lon:lon])
  {
    SearchResultsWrapper * wrapper = self.wrapper;
    for (NSInteger position = 0; position < [wrapper count]; position++)
    {
      search::Result const & result = [wrapper resultWithPosition:position];
      if (result.HasPoint())
      {
        string distance;
        double azimut = -1.0;
        GetFramework().GetDistanceAndAzimut(result.GetFeatureCenter(), lat, lon, north, distance, azimut);
        wrapper.distances[@(position)] = [NSString stringWithUTF8String:distance.c_str()];
      }
    }
  }
}

- (void)updateSearchParametersWithForce:(BOOL)force outParams:(search::SearchParams &)sp andQuery:(NSString *)newQuery
{
  sp.m_query = [[newQuery precomposedStringWithCompatibilityMapping] UTF8String];
  sp.m_callback = ^(search::Results const & results)
  {
    if (self.state == SearchViewStateHidden)
      return;
    SearchResultsWrapper * wrapper = [[SearchResultsWrapper alloc] initWithResults:results];
    dispatch_async(dispatch_get_main_queue(), ^
    {
      [self frameworkDidAddSearchResult:wrapper];
    });
  };
  sp.SetInputLocale([[self keyboardInputLanguage] UTF8String]);
  sp.SetForceSearch(force == YES);
}

- (void)search:(NSString *)newTextQuery
{
  self.emptyResultLabel.hidden = YES;
  [self.searchBar setSearching:YES];
  search::SearchParams sp;
  [self updateSearchParametersWithForce:YES outParams:sp andQuery:newTextQuery];
  double lat, lon;
  if ([[MapsAppDelegate theApp].m_locationManager getLat:lat Lon:lon])
    sp.SetPosition(lat, lon);
  GetFramework().Search(sp);
}

- (void)frameworkDidAddSearchResult:(SearchResultsWrapper *)wrapper
{
  // special buben for situation when textfield is empty and we should show categories (cause 'self.wrapper' == nil)
  // but callbacks from previous non-empty search are coming and modifying 'self.wrapper'
  // so, we see previous search results with empty textfield
  if (![self.searchBar.textField.text length])
    return;
  // -----------------------------------------

  if ([wrapper isEndMarker])
  {
    if ([wrapper isEndedNormal])
    {
      [self.searchBar setSearching:NO];
      [self recalculateDistances];
      [self.tableView reloadData];
    }
  }
  else
  {
    self.emptyResultLabel.hidden = [self isShowingCategories] ? YES : ([self rowsCount] > 0);
    self.wrapper = wrapper;
    [self.tableView reloadData];
    [self.tableView setContentOffset:CGPointMake(0, -self.tableView.contentInset.top) animated:YES];
  }
}

- (void)clearSearchResultsMode
{
  Framework & framework = GetFramework();
  framework.GetBalloonManager().RemovePin();
  framework.GetBalloonManager().Dismiss();
  framework.CancelInteractiveSearch();
}

- (void)searchBarDidPressClearButton:(SearchBar *)searchBar
{
  if (self.state == SearchViewStateResults)
    [self setState:SearchViewStateHidden animated:YES];
  else
    [self.searchBar.textField becomeFirstResponder];

  self.searchBar.textField.text = nil;
  [self textFieldTextChanged:nil];
}

- (void)searchBarDidPressCancelButton:(id)searchBar
{
  self.searchBar.textField.text = nil;
  [self textFieldTextChanged:nil];
  [self setState:SearchViewStateHidden animated:YES];
}

// TODO: This code only for demonstration purposes and will be removed soon
- (BOOL)tryChangeMapStyleCmd:(NSString *)cmd
{
  // Hook for shell command on change map style
  BOOL const isDark = [cmd isEqualToString:@"mapstyle:dark"];
  BOOL const isLight = isDark ? NO : [cmd isEqualToString:@"mapstyle:light"];

  if (!isDark && !isLight)
    return NO;

  // change map style
  MapStyle const mapStyle = isDark ? MapStyleDark : MapStyleLight;
  [[MapsAppDelegate theApp] setMapStyle: mapStyle];

  // close Search panel
  [self searchBarDidPressCancelButton:nil];

  return YES;
}

- (BOOL)tryToChangeRoutingModeCmd:(NSString *)cmd
{
  BOOL const isPedestrian = [cmd isEqualToString:@"?pedestrian"];
  BOOL const isVehicle = [cmd isEqualToString:@"?vehicle"];

  if (!isPedestrian && !isVehicle)
    return NO;

  MapsAppDelegate * delegate = [MapsAppDelegate theApp];
  delegate.isPedestrianRoutingMode = isPedestrian;
  [self search:cmd];
  [self searchBarDidPressCancelButton:nil];
  return YES;
}

- (void)textFieldTextChanged:(id)sender
{
  NSString * currentText = self.searchBar.textField.text;
  
  // TODO: This code only for demonstration purposes and will be removed soon
  if ([self tryChangeMapStyleCmd:currentText])
    return;

  // TODO(Vlad): This code only for demonstration purposes and will be removed soon
  if ([self tryToChangeRoutingModeCmd:currentText])
    return;

  if ([currentText length])
  {
    [self search:currentText];
  }
  else
  {
    // nil wrapper means "Display Categories" mode
    self.wrapper = nil;
    [self.searchBar setSearching:NO];
    self.emptyResultLabel.hidden = YES;
    [self.tableView reloadData];
  }
}

- (void)textFieldBegin:(id)sender
{
  if (self.state == SearchViewStateResults)
    [self clearSearchResultsMode];
  [self setState:SearchViewStateFullscreen animated:YES];
}

- (void)showOnMap
{
  Framework & f = GetFramework();
  if (f.ShowAllSearchResults() == 0)
  {
    NSString * secondSentence = @"";
    // Country in the viewport should be downloaded
    if (!f.IsCountryLoaded(f.GetViewportCenter()))
    {
      secondSentence = [NSString stringWithFormat:L(@"download_viewport_country_to_search"),
                        [NSString stringWithUTF8String:f.GetCountryName(f.GetViewportCenter()).c_str()]];
    }
    else
    {
      // Country in the current location should be downloaded
      CLLocation * lastLocation = [[MapsAppDelegate theApp].m_locationManager lastLocation];
      if (lastLocation && !f.IsCountryLoaded(MercatorBounds::FromLatLon(lastLocation.coordinate.latitude,
                                                                        lastLocation.coordinate.longitude)))
      {
        secondSentence = [NSString stringWithFormat:L(@"download_location_country"),
                          [NSString stringWithUTF8String:f.GetCountryName(f.GetViewportCenter()).c_str()]];
      }
    }

    NSString * message = [NSString stringWithFormat:@"%@. %@", L(@"no_search_results_found"), secondSentence];
    ToastView * toastView = [[ToastView alloc] initWithMessage:message];
    [toastView show];
  }

  search::SearchParams params;
  params.m_query = [[self.searchBar.textField.text precomposedStringWithCompatibilityMapping] UTF8String];
  params.SetInputLocale([[self keyboardInputLanguage] UTF8String]);

  f.StartInteractiveSearch(params);

  [self setState:SearchViewStateResults animated:YES];
}

- (BOOL)isShowingCategories
{
  // We are on the categories screen if wrapper is nil
  return self.wrapper == nil;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  if (![self isShowingCategories])
  {
    [self showOnMap];
    return YES;
  }
  return NO;
}

- (CGFloat)defaultSearchBarMinY
{
  return 20.0;
}

- (CGFloat)defaultTopBackgroundHeight
{
  return 64.0;
}

- (BOOL)iPhoneInLandscape
{
  return self.width > self.height && !IPAD;
}

- (void)layoutSubviews
{
  if (self.state == SearchViewStateFullscreen)
    self.searchBar.minY = [self defaultSearchBarMinY];
  self.tableView.contentInset = UIEdgeInsetsMake(self.topBackgroundView.height, 0, 0, 0);
  self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell * cell;
  CellType cellType = [self cellTypeForIndexPath:indexPath];
  switch (cellType)
  {
    case CellTypeCategory:
    {
      SearchCategoryCell * customCell = [tableView dequeueReusableCellWithIdentifier:[SearchCategoryCell className]];
      if (!customCell) // only for iOS 5
        customCell = [[SearchCategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[SearchCategoryCell className]];

      customCell.titleLabel.text = L(self.categoriesNames[indexPath.row]);
      NSString * iconName = [NSString stringWithFormat:@"CategoryIcon%@", [self.categoriesNames[indexPath.row] capitalizedString]];
      customCell.iconImageView.image = [UIImage imageNamed:iconName];
      cell = customCell;
      break;
    }
    case CellTypeShowOnMap:
    {
      SearchShowOnMapCell * customCell = [tableView dequeueReusableCellWithIdentifier:[SearchShowOnMapCell className]];
      if (!customCell) // only for iOS 5
        customCell = [[SearchShowOnMapCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[SearchShowOnMapCell className]];

      customCell.titleLabel.text = L(@"search_on_map");
      cell = customCell;
      break;
    }
    case CellTypeResult:
    {
      SearchResultCell * customCell = [tableView dequeueReusableCellWithIdentifier:[SearchResultCell className]];
      if (!customCell) // only for iOS 5
        customCell = [[SearchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[SearchResultCell className]];

      NSInteger const position = [self searchResultPositionForIndexPath:indexPath];
      search::Result const & result = [self.wrapper resultWithPosition:position];
      NSMutableArray * ranges = [[NSMutableArray alloc] init];
      for (size_t i = 0; i < result.GetHighlightRangesCount(); i++)
      {
        pair<uint16_t, uint16_t> const & pairRange = result.GetHighlightRange(i);
        NSRange range = NSMakeRange(pairRange.first, pairRange.second);
        [ranges addObject:[NSValue valueWithRange:range]];
      }
      NSString * title = [NSString stringWithUTF8String:result.GetString()];
      [customCell setTitle:title selectedRanges:ranges];
      customCell.subtitleLabel.text = [NSString stringWithUTF8String:result.GetRegionString()];
      customCell.iconImageView.image = [UIImage imageNamed:@"SearchCellPinIcon"];
      customCell.distanceLabel.text = self.wrapper.distances[@(position)];
      customCell.typeLabel.text = [NSString stringWithUTF8String:result.GetFeatureType()];
      cell = customCell;
      break;
    }
    case CellTypeSuggest:
    {
      SearchSuggestCell * customCell = [tableView dequeueReusableCellWithIdentifier:[SearchSuggestCell className]];
      if (!customCell) // only for iOS 5
        customCell = [[SearchSuggestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[SearchSuggestCell className]];

      NSInteger const position = [self searchResultPositionForIndexPath:indexPath];
      search::Result const & result = [self.wrapper resultWithPosition:position];

      customCell.titleLabel.text = [NSString stringWithUTF8String:result.GetString()];
      cell = customCell;
      break;
    }
  }
  return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  CellType cellType = [self cellTypeForIndexPath:indexPath];
  switch (cellType)
  {
    case CellTypeCategory:
    {
      return [SearchCategoryCell cellHeight];
    }
    case CellTypeShowOnMap:
    {
      return [SearchShowOnMapCell cellHeight];
    }
    case CellTypeResult:
    {
      NSInteger const position = [self searchResultPositionForIndexPath:indexPath];
      SearchResultsWrapper * wrapper = self.wrapper;
      search::Result const & result = [wrapper resultWithPosition:position];
      NSString * title = [NSString stringWithUTF8String:result.GetString()];
      NSString * subtitle;
      NSString * type;
      if (result.GetResultType() == search::Result::RESULT_FEATURE || result.GetResultType() == search::Result::RESULT_LATLON)
      {
        subtitle = [NSString stringWithUTF8String:result.GetRegionString()];
        type = [NSString stringWithUTF8String:result.GetFeatureType()];
      }
      return [SearchResultCell cellHeightWithTitle:title type:type subtitle:subtitle distance:wrapper.distances[@(position)] viewWidth:tableView.width];
    }
    case CellTypeSuggest:
    {
      return [SearchSuggestCell cellHeight];
    }
    default:
    {
      return 0;
    }
  }
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView deselectRowAtIndexPath:indexPath animated:YES];

  CellType cellType = [self cellTypeForIndexPath:indexPath];

  switch (cellType)
  {
    case CellTypeCategory:
    {
      [[Statistics instance] logEvent:@"Category Selection" withParameters:@{@"Category" : self.categoriesNames[indexPath.row]}];
      NSString * newQuery = [L(self.categoriesNames[indexPath.row]) stringByAppendingString:@" "];
      self.searchBar.textField.text = newQuery;
      [self search:newQuery];

      break;
    }
    case CellTypeShowOnMap:
    {
      [self showOnMap];
      break;
    }
    case CellTypeResult:
    {
      NSInteger const position = [self searchResultPositionForIndexPath:indexPath];
      search::Result const & result = [self.wrapper resultWithPosition:position];
      [self setState:SearchViewStateHidden animated:YES];
      GetFramework().ShowSearchResult(result);
      break;
    }
    case CellTypeSuggest:
    {
      NSInteger const position = [self searchResultPositionForIndexPath:indexPath];
      search::Result const & result = [self.wrapper resultWithPosition:position];
      NSString * newQuery = [NSString stringWithUTF8String:result.GetSuggestionString()];
      self.searchBar.textField.text = newQuery;
      [self search:newQuery];

      break;
    }
  }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  if (!scrollView.decelerating && scrollView.dragging)
    [self.searchBar.textField resignFirstResponder];
}

- (CellType)cellTypeForIndexPath:(NSIndexPath *)indexPath
{
  if ([self isShowingCategories])
  {
    return CellTypeCategory;
  }
  else
  {
    size_t const numSuggests = [self.wrapper suggestsCount];
    if (numSuggests)
      return indexPath.row < numSuggests ? CellTypeSuggest : CellTypeResult;
    else
      return indexPath.row == 0 ? CellTypeShowOnMap : CellTypeResult;
  }
}

- (NSInteger)searchResultPositionForIndexPath:(NSIndexPath *)indexPath
{
  return [self.wrapper suggestsCount] ? indexPath.row : indexPath.row - 1;
}

- (NSInteger)rowsCount
{
  if ([self isShowingCategories])
    return [self.categoriesNames count];
  else
    return [self.wrapper suggestsCount] ? [self.wrapper count] : [self.wrapper count] + 1;
}

- (NSArray *)categoriesNames
{
  if (!_categoriesNames)
    _categoriesNames = @[
        @"food",
        @"hotel",
        @"tourism",
        @"wifi",
        @"transport",
        @"fuel",
        @"parking",
        @"shop",
        @"atm",
        @"bank",
        @"entertainment",
        @"hospital",
        @"pharmacy",
        @"police",
        @"toilet",
        @"post"];
  return _categoriesNames;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
  return CGRectContainsPoint(self.searchBar.frame, point) || self.state == SearchViewStateFullscreen;
}

- (UITableView *)tableView
{
  if (!_tableView)
  {
    _tableView = [[UITableView alloc] initWithFrame:self.bounds];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  }
  return _tableView;
}

- (SolidTouchView *)topBackgroundView
{
  if (!_topBackgroundView)
  {
    _topBackgroundView = [[SolidTouchView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0)];
    _topBackgroundView.backgroundColor = [UIColor colorWithColorCode:@"1F9952"];
    _topBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _topBackgroundView.userInteractionEnabled = YES;
  }
  return _topBackgroundView;
}

- (SearchBar *)searchBar
{
  if (!_searchBar)
  {
    _searchBar = [[SearchBar alloc] initWithFrame:CGRectMake(0, 0, self.width, 44)];
    _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _searchBar.textField.delegate = self;
    _searchBar.delegate = self;
    [_searchBar.textField addTarget:self action:@selector(textFieldTextChanged:) forControlEvents:UIControlEventEditingChanged];
    [_searchBar.textField addTarget:self action:@selector(textFieldBegin:) forControlEvents:UIControlEventEditingDidBegin];
  }
  return _searchBar;
}

- (UILabel *)emptyResultLabel
{
  if (!_emptyResultLabel)
  {
    _emptyResultLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 60)];
    _emptyResultLabel.backgroundColor = [UIColor clearColor];
    _emptyResultLabel.font = [UIFont regular16];
    _emptyResultLabel.text = L(@"no_search_results_found");
    _emptyResultLabel.textColor = [UIColor primary];
    _emptyResultLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _emptyResultLabel.textAlignment = NSTextAlignmentCenter;
  }
  return _emptyResultLabel;
}

- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (NSString *)keyboardInputLanguage
{
  UITextInputMode * mode = [self textInputMode];
  if (mode)
    return mode.primaryLanguage;
  // Use system language as a fall-back
  return [[NSLocale preferredLanguages] firstObject];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  // Prevent super call to stop event propagation
  // [super touchesBegan:touches withEvent:event];
}

#pragma mark - Properties

- (CGRect)infoRect
{
  return [self convertRect:self.topBackgroundView.frame toView:self.superview];
}

@end