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

#import "SelectStoreViewController.h"
#import <RATreeView.h>
#import "SelectAreaTableCell.h"
#define kSelectAreaTableCell @"selectAreaTableCell"

#import "SelectProvinceTableCell.h"
#define kSelectProvinceTableCell @"selectProvinceTableCell"

#import "SelectStoreTableCell.h"
#define kSelectStoreTableCell @"selectStoreTableCell"

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

#import "SelectStoreHeadView.h"
#import "CheckBoxButton.h"

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

@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];
    
}

- (void)areaClick:(CheckBoxButton *)sender
{
    sender.selected = !sender.selected;
    SelectAreaTableCell *cell = (SelectAreaTableCell *)sender.superview.superview;
    NSArray *provinceArray = cell.areaModel.provinceArray;
    if (sender.selected) {
        
        for (ProvinceModel *proVin in provinceArray) {
            proVin.isChoose = YES;
        }
        [sender setTitleColor:kLightBlack forState:UIControlStateNormal];
    } else {
        for (ProvinceModel *proVin in provinceArray) {
            proVin.isChoose = NO;
        }
        [sender setTitleColor:kSelectStoreColor forState:UIControlStateNormal];

    }
    [_treeView reloadRows];
}


#pragma mark - RAtreeView Delegate/DataSource
- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
{
    NSInteger level = [treeView levelForCellForItem:item];
    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];
    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];
    if (level == 0) {
        AreaModel *data = item;
        SelectAreaTableCell *cell = [self.treeView dequeueReusableCellWithIdentifier:kSelectAreaTableCell];
        cell.areaModel = data;
        [cell.checkBoxButton setTitle:data.name forState:UIControlStateNormal];
        [cell.checkBoxButton addTarget:self action:@selector(areaClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.iconImageView.image = [UIImage imageNamed:@"arrow_down"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (level == 1) {
        ProvinceModel *data = item;
        SelectProvinceTableCell *cell = [self.treeView dequeueReusableCellWithIdentifier:kSelectProvinceTableCell];
        cell.provinceModel = data;
        if (data.isChoose) {
            cell.checkBoxButton.selected = YES;
        } else {
            cell.checkBoxButton.selected = NO;
        }
        [cell.checkBoxButton setTitle:data.name forState:UIControlStateNormal];

        cell.iconImageView.image = [UIImage imageNamed:@"grey-trilateral_down"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        StoreModel *data = item;
        
        SelectStoreTableCell *cell = [self.treeView dequeueReusableCellWithIdentifier:kSelectStoreTableCell];
        cell.storeModel = data;
        [cell.checkBoxButton setTitle:data.name forState:UIControlStateNormal];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}



- (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];
        _treeView.rowHeight = UITableViewAutomaticDimension;
        _treeView.estimatedRowHeight = 150.0;
        _treeView.treeHeaderView = self.headerView;
        [_treeView registerClass:[SelectAreaTableCell class] forCellReuseIdentifier:kSelectAreaTableCell];
        [_treeView registerClass:[SelectProvinceTableCell class] forCellReuseIdentifier:kSelectProvinceTableCell];
        [_treeView registerClass:[SelectStoreTableCell class] forCellReuseIdentifier:kSelectStoreTableCell];
        [self.view addSubview:_treeView];
    }
    return _treeView;
}

- (SelectStoreHeadView *)headerView
{
    if (!_headerView) {
        _headerView = [[SelectStoreHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    }
    return _headerView;
}

/*
#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
