//
//  RuleRegisterTypeViewController.m
//  kneet
//
//  Created by Jason Lee on 4/21/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//

#import "RuleRegisterTypeViewController.h"
#import "RuleModel.h"
#import "RulesRegisterViewController.h"
#import "CustomLabel.h"
#import "ImageUtil.h"

@implementation RuleTypeInfoTableViewCell
- (void)awakeFromNib {
    _lblDesc.text = NSLocalizedString(@"정해둔 조건이 만족하는 순간! 홈 규칙이 자동 실행됩니다\n\n만들기 방식을 선택하세요", @"정해둔 조건이 만족하는 순간! 홈 규칙이 자동 실행됩니다\n\n만들기 방식을 선택하세요");
}
@end

@implementation RuleTypeSampleTableViewCell
- (void)awakeFromNib {
    _lblTitle.text = NSLocalizedString(@"샘플로 만들기", @"샘플로 만들기");
    _lblDesc.text = NSLocalizedString(@"무엇을 만들어야 할지 고민 되세요?", @"무엇을 만들어야 할지 고민 되세요?");
}
@end

@implementation RuleTypeCustomTableViewCell
- (void)awakeFromNib {
    _lblTitle.text = NSLocalizedString(@"자유롭게 만들기", @"자유롭게 만들기");
    _lblDesc.text = NSLocalizedString(@"제한없이 만들고 싶으세요?", @"제한없이 만들고 싶으세요?");
}
@end

@interface RuleRegisterTypeViewController () <UITableViewDataSource, UITableViewDelegate> {
    UIImage *_bgCellImage1, *_bgCellImage2;
}
@end


#pragma mark - Class Definition

@implementation RuleRegisterTypeViewController


- (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 {
    

    

    //initialize tableview
    _tableView.dataSource = self;
    _tableView.delegate = self;

    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];

    _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;

    UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
    _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
    _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
}

- (void)prepareViewDidLoad {

}

#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 110.0f;
    if (indexPath.row == 0) {
        height = 210.0f;
    }
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellIdentifier = nil;
    if (indexPath.row == 0) {
        cellIdentifier = @"TopCellIdentifier";
    } else if (indexPath.row == 1) {
        cellIdentifier = @"SampleCellIdentifier";
    } else if (indexPath.row == 2) {
        cellIdentifier = @"CustomCellIdentifier";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


    //set background image
    if (indexPath.row == 0) {
        cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
    } else {
        cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

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

    if (indexPath.row == 1) {//사전 정의 룰 이동
        [self gotoPredfinedRuleRegister];

    } else if (indexPath.row == 2) {//자유 선택
        [self gotoCustomRuleRegister];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (void)gotoPredfinedRuleRegister {
    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesRegisterPredefineViewController" storyboardName:@"Rules"];
    [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
}

- (void)gotoCustomRuleRegister {
    RulesRegisterViewController *vc = (RulesRegisterViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesRegisterViewController" storyboardName:@"Rules"];

    PredefinedRuleDetailModel *tmpRuleDetail = [[PredefinedRuleDetailModel alloc] initWithDictionary:
                                                @{@"triggers":@[
                                                          @{@"pred_rule_id":@"0", @"pred_item_sequence": @"1", @"item_sub_type_code": ksItemSubTypeCodeTimer, @"item_type_code": @"01", @"item_name":@"특정 시간에"},
                                                          @{@"pred_rule_id":@"0", @"pred_item_sequence": @"1", @"item_sub_type_code": ksItemSubTypeCodeGeoFencing, @"item_type_code": @"01", @"item_name":@"귀가 또는 외출 시"},
                                                          @{@"pred_rule_id":@"0", @"pred_item_sequence": @"1", @"item_sub_type_code": ksItemSubTypeCodeMode, @"item_type_code": @"01", @"item_name":@"홈 모드가 바뀔 때"},
                                                          @{@"pred_rule_id":@"0", @"pred_item_sequence": @"1", @"item_sub_type_code": ksItemSubTypeCodeDevice, @"item_type_code": @"01", @"item_name":@"집에 변화가 일어날 때"}
                                                          ]}  error:nil];

    [vc setPredefinedRuleDetail:tmpRuleDetail];
    [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UI Events
- (IBAction)btnSampleTouched:(id)sender {
    [self gotoPredfinedRuleRegister];
}

- (IBAction)btnCustomTouched:(id)sender {
    [self gotoCustomRuleRegister];
}


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


@end
