//
//  HomeLocationViewController.m
//  kneet
//
//  Created by Jason Lee on 4/23/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"

#import "RequestHandler.h"

#import "CustomButton.h"

#import "ValidateUtil.h"
#import "CustomTextField.h"
#import "CustomAlertView.h"
#import "CustomRadioGroup.h"
#import "PopTableView.h"

#import "HomeLocationViewController.h"

@interface HomeLocationViewController () <CLLocationManagerDelegate, MKMapViewDelegate, PopTableViewDelegate, CustomTextFieldDelegate, CustomRadioGroupDelegate> {
}
@end


#pragma mark - Class Definition

@implementation HomeLocationViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initUI];
    [self prepareViewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.title = NSLocalizedString(@"홈의 위치를 등록하세요", nil);
}

- (void)initUI {
    
    _txtKeyword.delegate = self;
    _txtKeyword.keyboardType = UIKeyboardTypeDefault;
    _txtKeyword.returnKeyType = UIReturnKeySearch;

    _rdoRadius100.value = @100;
    _rdoRadius300.value = @300;
    _rdoRadius500.value = @500;
    _rdoRadius1000.value = @1000;

    _rgroup = [[CustomRadioGroup alloc] initWithRadioButtons:_rdoRadius100, _rdoRadius300, _rdoRadius500, _rdoRadius1000, nil];
    _rgroup.delegate = self;
    
    //Localization
    _txtKeyword.placeholder = NSLocalizedString(@"주소 검색", @"주소 검색");
    [_btnRegister setTitle:NSLocalizedString(@"다음", @"다음") forState:UIControlStateNormal];
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if (!_isEnableLocatonService) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)prepareViewDidLoad {

}

#pragma mark - Main Logic


#pragma mark - UI Events

- (IBAction)btnCurrentLocationTouched:(id)sender {

}

//구글 지오코딩 요청.
- (void)btnSearchAddrTouched:(id)sender {

#ifdef DEBUG_MODE
    if ([_txtKeyword.text isEmptyString]) {
        _txtKeyword.text = @"삼성동";
    }
#endif

    [_txtKeyword resignFirstResponder];

    if (![ValidateUtil validateTextfiled:_txtKeyword type:ValidateTypeNull title:NSLocalizedString(@"주소", @"주소")]) {
        return;
    }
}



- (void)checkNewMobileDevice {
    //새로운 단말인 경우,
    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"NewMobileViewController" storyboardName:@"SignUp"];
    [[JDFacade facade] presentViewControllerByPush:vc pvc:self];
}


- (IBAction)btnRegisterTouched:(id)sender {

    JDGeofenceAnnotation *pin = nil;
    for (id annotation in _mapView.annotations) {
        if (annotation != _mapView.userLocation) {
            pin = annotation;
            break;
        }
    }

    if (!pin) {//선택된 곳이 있는지 확인
        [[JDFacade facade] alert:NSLocalizedString(@"홈 위치를 먼저 설정해 주세요", @"홈 위치를 먼저 설정해 주세요")];
        return;
    }

//    //1. validate
//    if (!_pinAnnotation || [_pinAnnotation.title isEmptyString] || (_pinAnnotation.coordinate.latitude == 0 || _pinAnnotation.coordinate.longitude == 0)) {
//        [[JDFacade facade] alert:@"홈 위치를 먼저 설정해 주세요"];
//        return;
//    }
}

- (IBAction)btnRegisterLaterTouched:(id)sender {

    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SignUpCompleteViewController" storyboardName:@"SignUp"];
    [[JDFacade facade] presentViewControllerByPush:vc pvc:self];
}

#pragma mark - CustomTextField

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self btnSearchAddrTouched:nil];

    return YES;
}

#pragma mark - MemoryWarning
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
