//
//  PreferencesViewController.m
//  WSCloudStorage
//
//  Created by JonathanH on 2017/8/2.
//  Copyright © 2017年 wswinsoft. All rights reserved.
//

#import "PreferencesViewController.h"
#import "BAKit_BAGridView.h"
static NSString * const kCellID = @"PreferencesViewControllerCellID";
#define AvatarWH 60
#define ProgressH 8
#define MaxX(view) CGRectGetMaxX(view.frame)
#define EqualY(view) view.frame.origin.y
//#define MaxXPlus(MaxX(v),plusnumber) (MaxX(v) + plusnumber)
//Avatar frame
#define ScreenMarginTBLR 20


//gridView
#define kGridView_rowCount   4
#define kGridView_itemHeight 100
#define kGridView_H          BAKit_getColumnCountWithArrayAndRowCount_pod(self.gridDataArray, kGridView_rowCount) * kGridView_itemHeight
@interface PreferencesViewController()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)UIImageView *avatarview;
@property(nonatomic,weak)UIProgressView *progressview;
@property(nonatomic, weak)BAGridView *gridView;
@property(nonatomic,strong)NSMutableArray *gridDataArray;

@property(nonatomic,weak)UITableView *tableview;
@property(nonatomic,strong)NSArray *tableDataArray;
@end
@implementation PreferencesViewController
- (NSMutableArray <BAGridItemModel *> *)gridDataArray
{
    if (!_gridDataArray)
    {
        _gridDataArray = @[].mutableCopy;
        
        // 可以为本地图片
        //        NSArray *imageNameArray = @[@"tabbar_mainframeHL", @"tabbar_mainframeHL", @"tabbar_mainframeHL", @"tabbar_mainframeHL", @"tabbar_mainframeHL"];
        // 也可以是网络图片
        NSArray *imageNameArray = @[@"gridviewtest.png", @"gridviewtest.png", @"gridviewtest.png", @"gridviewtest.png", @"gridviewtest.png"];
        
        NSArray *titleArray = @[@"测试", @"测试", @"测试", @"测试", @"测试"];
        
        for (NSInteger i = 0; i < titleArray.count; i++)
        {
            BAGridItemModel *model = [BAGridItemModel new];
            model.imageName = imageNameArray[i];
            model.placdholderImageName = @"tabbar_mainframeHL";
            model.titleString = titleArray[i];
            
            [self.gridDataArray addObject:model];
        }
    }
    return _gridDataArray;
}
-(NSArray *)tableDataArray{
    if (!_tableDataArray) {
        _tableDataArray = [[NSArray alloc]initWithObjects:
  @{
    @"title":@"设置",
    @"class":@"PreferencesSettingVC"
  },
  @{
    @"title":@"用户反馈",
    @"class":@"feedBackVC"},
  @{
    @"title":@"关于",
    @"class":@"aboutUsVC"},
                           nil];
    }
    return _tableDataArray;
}
-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = RGB(243, 243, 245);
    self.navigationController.navigationBar.hidden = YES;
    [self initSubViews];
    [self initData];
}
-(void)initSubViews{
    //|----|namelabel
    //|头像 |progress
    //|____|descritionlabel
    CGFloat screenW = SCREEN_WIDTH;
    CGFloat screenH = SCREEN_HEIGHT;
    CGFloat residualRightW;
    CGFloat residualRightH;//可用户布局  剩余高度
    UIImageView *avatarview = [[UIImageView alloc]init];
    avatarview.layer.cornerRadius = AvatarWH/2;
    avatarview.layer.masksToBounds = YES;
    avatarview.frame = CGRectMake(ScreenMarginTBLR, 2*ScreenMarginTBLR, AvatarWH, AvatarWH);
    avatarview.backgroundColor = [UIColor grayColor];
    [self.view addSubview:avatarview];
    self.avatarview = avatarview;
    
    residualRightW = screenW - MaxX(avatarview) - 10 - 20;//10 为何下一个控件的x距离 20为右边距
    
    UILabel *namelab = [[UILabel alloc]init];
    namelab.backgroundColor = [UIColor grayColor];
    namelab.textColor = [UIColor blackColor];
    namelab.font = [UIFont systemFontOfSize:16];
    namelab.text = [userDefaults objectForKey:@"displayName"]==nil?@"显示名称":[userDefaults objectForKey:@"displayName"];
    namelab.frame = CGRectMake(MaxX(avatarview)+ 10, avatarview.frame.origin.y, 200, 30);//临时写死
    [self.view addSubview:namelab];
    
    UIProgressView *progressview = [[UIProgressView alloc]initWithFrame:CGRectMake(namelab.origin.x,avatarview.centerY -ProgressH/2 , residualRightW, ProgressH)];
    progressview.progressTintColor = UIColorFromRGB(0x0DA32E);
    progressview.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:progressview];
    self.progressview = progressview;
    
    UILabel *descritionlab = [[UILabel alloc]init];
    descritionlab.text = @"容量 (23.12G/35.00G 66%)";
    descritionlab.font = [UIFont systemFontOfSize:12];
    descritionlab.backgroundColor = [UIColor grayColor];
    descritionlab.frame = CGRectMake(progressview.origin.x, CGRectGetMaxY(progressview.frame), residualRightW, 30);
    [self.view addSubview:descritionlab];
    

    BAGridView *gridview = [BAGridView ba_creatGridViewWithGridViewType:BAGridViewTypeImageTitle dataArray:self.gridDataArray configurationBlock:^(BAGridView *tempView) {
        
        // 是否显示分割线
        tempView.showLineView = YES;
        // item：分割线颜色，默认：BAKit_Color_Gray_11【BAKit_Color_RGB(248, 248, 248)】
        tempView.ba_gridView_lineColor = RGB(240, 240, 242);
        // item：每行 item 的个数，默认为4个
        tempView.ba_gridView_rowCount = kGridView_rowCount;
        // item：高度
        tempView.ba_gridView_itemHeight = kGridView_itemHeight;
        // item：图片与文字间距（或者两行文字类型的间距），默认：0
        //            tempView.ba_gridView_itemImageInset = 10;
        //  item：title 颜色，默认：BAKit_Color_Black【[UIColor blackColor]】
        //            tempView.ba_gridView_titleColor = BAKit_Color_Black;
        // item：title Font，默认：图文样式下 16，两行文字下（上25，下12）
        tempView.ba_gridView_titleFont = [UIFont boldSystemFontOfSize:15];
        // item：背景颜色，默认：BAKit_Color_White
        tempView.ba_gridView_backgroundColor = [UIColor whiteColor];
        // item：背景选中颜色，默认：无色
        tempView.ba_gridView_selectedBackgroundColor = BAKit_Color_Red_pod;
        
        self.gridView = tempView;
        
    } block:^(BAGridItemModel *model, NSIndexPath *indexPath) {
        
        //            BAKit_ShowAlertWithMsg_ios8(model.titleString);
    }];
    gridview.frame = CGRectMake(0, CGRectGetMaxY(avatarview.frame)+20, screenW, kGridView_H);
    [self.view addSubview:gridview];
    self.gridView = gridview;
    
    residualRightH =screenH - CGRectGetMaxY(gridview.frame) +20;
    
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(gridview.frame)+20, screenW, residualRightH) style:UITableViewStylePlain];
    //footview
    UIView *footview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 54)];
    UIButton *exitBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [exitBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [exitBtn setTitle:@"退出账户" forState:UIControlStateNormal];
    exitBtn.backgroundColor = [UIColor whiteColor];
    exitBtn.frame = CGRectMake(0, 10, screenW, 44);
    
    [footview addSubview:exitBtn];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.tableFooterView = footview;
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    self.tableview = tableView;
}
-(void)initData{
    [self.progressview setProgress:.34 animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellID];
    }
    cell.textLabel.text = [self.tableDataArray[indexPath.row]objectForKey:@"title"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"tap %@",indexPath);
    NSLog(@"cell text: %@",[[[tableView cellForRowAtIndexPath:indexPath]textLabel]text]);
    
}
@end
