//
//  SelectStoreViewController.m
//  redstar
//
//  Created by admin on 15/12/13.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "SelectStoreViewController.h"
#import <RATreeView.h>

#import "AreaModel.h"
#import "ProvinceModel.h"
#import "StoreModel.h"

@interface SelectStoreViewController () <RATreeViewDelegate, RATreeViewDataSource>
@property (nonatomic, strong) RATreeView *treeView;
@property (nonatomic, strong) NSMutableArray *allAreaData;
@end

@implementation SelectStoreViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"选择商场"];
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
   
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    [self loadData];
    
    
    self.treeView.delegate = self;
    self.treeView.dataSource = self;
    [self.treeView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    self.treeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.treeView reloadData];
}

- (void)doBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

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

- (void)loadData
{
    StoreModel *notebook1 = [StoreModel dataObjectWithName:@"南京中央路店"];
    StoreModel *notebook2 = [StoreModel dataObjectWithName:@"镇江新区店"];
    StoreModel *notebook3 = [StoreModel dataObjectWithName:@"南京卡子门店"];
    
    ProvinceModel *phone1 = [ProvinceModel dataObjectWithName:@"江苏" children:[NSArray arrayWithObjects:notebook1, notebook2, notebook3,nil]];
    ProvinceModel *phone2 = [ProvinceModel dataObjectWithName:@"苏北" children:nil];
    ProvinceModel *phone3 = [ProvinceModel dataObjectWithName:@"皖南" children:nil];
    ProvinceModel *phone4 = [ProvinceModel dataObjectWithName:@"皖北" children:nil];
    
    AreaModel *huadong = [AreaModel dataObjectWithName:@"华东大区"
                                                  children:[NSArray arrayWithObjects:phone1, phone2, phone3, phone4, nil]];
    
    
    
    ProvinceModel *computer1 = [ProvinceModel dataObjectWithName:@"黑龙江"
                                                      children:nil];
    ProvinceModel *computer2 = [ProvinceModel dataObjectWithName:@"吉林" children:nil];
    ProvinceModel *computer3 = [ProvinceModel dataObjectWithName:@"哈尔滨" children:nil];
    
    AreaModel *huabei = [AreaModel dataObjectWithName:@"华北东北大区"
                                                     children:[NSArray arrayWithObjects:computer1, computer2, computer3, nil]];
    AreaModel *jinghu = [AreaModel dataObjectWithName:@"京沪西南"
                                               children:[NSArray arrayWithObjects:computer1, computer2, computer3, nil]];
    
    self.allAreaData = [NSMutableArray arrayWithObjects:huadong ,huabei, jinghu, nil];
    
}


#pragma mark - RAtreeView Delegate/DataSource
- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
{
    NSInteger level = [treeView levelForCellForItem:item];
    NSLog(@"11level = %ld", level);
    if (level == -1) {
        return [self.allAreaData count];
    } else if (level == 0) {
        AreaModel *data = item;
        return data.provinceArray.count;
    } else if (level == 1) {
        ProvinceModel *data = item;
        return data.storeArray.count;
    } else {
        return 0;
    }
}


- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
{
    NSInteger level = [treeView levelForCellForItem:item];
NSLog(@"22level = %ld", level);
    if (level == -1) {
        return [self.allAreaData objectAtIndex:index];
    } else if (level == 0) {
        AreaModel *data = item;
        return data.provinceArray[index];
    } else if (level == 1) {
        ProvinceModel *data = item;
        return data.storeArray[index];
    } else {
        return nil;
    }
}

- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item
{
    NSInteger level = [self.treeView levelForCellForItem:item];
    NSLog(@"33level = %ld", level);

    if (level == 0) {
        AreaModel *data = item;
        NSInteger numberOfChildren = [data.provinceArray count];
        NSString *detailText = [NSString localizedStringWithFormat:@"Number of children %@  level = %ld", [@(numberOfChildren) stringValue], level];
        
        UITableViewCell *cell = [self.treeView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.contentView.backgroundColor = [UIColor brownColor];
        cell.textLabel.text = data.name;
        cell.detailTextLabel.text = detailText;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (level == 1) {
        ProvinceModel *data = item;
        NSInteger numberOfChildren = [data.storeArray count];
        NSString *detailText = [NSString localizedStringWithFormat:@"Number of children %@  level = %ld", [@(numberOfChildren) stringValue], level];
        
        UITableViewCell *cell = [self.treeView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.contentView.backgroundColor = [UIColor yellowColor];

        cell.textLabel.text = data.name;
        cell.detailTextLabel.text = detailText;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        StoreModel *data = item;
        NSInteger numberOfChildren = 0;
        NSString *detailText = [NSString localizedStringWithFormat:@"Number of children %@  level = %ld", [@(numberOfChildren) stringValue], level];
        
        UITableViewCell *cell = [self.treeView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.contentView.backgroundColor = [UIColor orangeColor];

        cell.textLabel.text = data.name;
        cell.detailTextLabel.text = detailText;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
    
    
    
    
}

- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item
{
    return 44;
}

- (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item
{
    return NO;
}


#pragma mark - Lazy loading
- (RATreeView *)treeView
{
    if (!_treeView) {
        _treeView = [[RATreeView alloc] initWithFrame:self.view.bounds];
        _treeView.treeFooterView = [UIView new];
        [self.view addSubview:_treeView];
    }
    return _treeView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
