//
//  CloudStorageBar.m
//  zhuxun
//
//  Created by winsoft on 17/6/20.
//
//

#import "CloudStorageBar.h"
#import "CustomSearchBar.h"
#import "CustomButton.h"
#import <FTPopOverMenu/FTPopOverMenu.h>
#define MarginX 20
#define BtnWH 22

@interface CloudStorageBar()<UISearchBarDelegate>
@property (nonatomic, weak)CustomButton *folderCreateBtn;
@property (nonatomic, weak)CustomButton *fileSortBtn;
@property (nonatomic, weak)CustomSearchBar *searchBar;
//@property (nonatomic, weak)UISegmentedControl *segmentControl;
@end
@implementation CloudStorageBar
- (instancetype)init
{
    if(self = [super init])
    {
        CustomButton *folderCreateBtn = [[CustomButton alloc]initCustomBtnWithImageDirection:ImagePostionTop imageViewSize:CGSizeMake(BtnWH, BtnWH) imageLabelMargin:0 titleLabelFont:nil imageIcon:@"file_newfolder_n" selectIcon:@"file_newfolder_p" title:nil btnWidth:BtnWH];
  
        [folderCreateBtn addTarget:self action:@selector(createNewFolder) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:folderCreateBtn];
        self.folderCreateBtn = folderCreateBtn;
        
        CustomButton *fileSortBtn = [[CustomButton alloc]initCustomBtnWithImageDirection:ImagePostionTop imageViewSize:CGSizeMake(BtnWH, BtnWH) imageLabelMargin:0 titleLabelFont:nil imageIcon:@"file_sort_n" selectIcon:@"file_sort_p" title:nil btnWidth:BtnWH];
        
        [fileSortBtn addTarget:self action:@selector(sortFile) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:fileSortBtn];
        self.fileSortBtn = fileSortBtn;
        
        
        //WithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SearchBarHeight)
        CustomSearchBar *searchBar = [[CustomSearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CloudStorageBarHeight)];
        [searchBar setImage:nil forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        searchBar.placeholder = @"搜索";
        searchBar.delegate  = self;
        [self addSubview:searchBar];
        // 添加 searchbar 到 headerview
        self.searchBar = searchBar;
        
//        NSArray *segtitiles = @[@"全部",@"图片",@"文档",@"视频",@"音频",@"其他"];
//        UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:segtitiles];
//        segmentControl.apportionsSegmentWidthsByContent = NO;
//        segmentControl.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
//        segmentControl.backgroundColor = [UIColor colorWithRed:0.969 green:0.969 blue:0.961 alpha:1.000];
//        NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
//                                                 NSForegroundColorAttributeName: [UIColor colorWithRed:0.000 green:0.475 blue:1.000 alpha:1.000]};
//        [segmentControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
//        NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
//                                                   NSForegroundColorAttributeName: [UIColor colorWithWhite:0.510 alpha:1.000]};
//        [segmentControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
//
//        segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
//        [segmentControl addTarget:self action:@selector(segmentaction:) forControlEvents:UIControlEventValueChanged];
//        
//
//        //[self.segmentControl addTarget:self action:@selector(segmentaction:) forControlEvents:UIControlEventValueChanged];
//        segmentControl.selectedSegmentIndex = 0;
//        [self addSubview:segmentControl];
//        self.segmentControl = segmentControl;
        
        
    }
    return self;
}

- (void)createNewFolder
{
    if (self.barNewFolderBlock) {
        self.barNewFolderBlock();
    }
}

- (void)sortFile
{
    __weak typeof(self)weakSelf = self;
    [FTPopOverMenu showForSender:self.fileSortBtn withMenuArray:@[@"按文件名称排序",@"按时间倒序排序"] doneBlock:^(NSInteger selectedIndex) {
        if (weakSelf.barSortBlock) {
            weakSelf.barSortBlock((SortType)selectedIndex);
        }
    } dismissBlock:^{
        
    }];
//    [FTPopOverMenu showForSender:self.fileSortBtn withMenuArray:@[@"按文件名称排序",@"按时间倒序排序"] imageNameArray:nil doneBlock:^(NSInteger selectedIndex) {
    

}

- (void)segmentaction:(UISegmentedControl *)segController
{
    if (self.fileClassBlock) {
        self.fileClassBlock((CloudStorageBarFileType)segController.selectedSegmentIndex);
    }
}

- (void)giveUpSearch
{
    [self.searchBar resignFirstResponder];
}
#pragma mark --
#pragma mark --searchbardelegate


- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    //以后隐藏
    
    if ([self.delegate respondsToSelector:@selector(searchBarPrepareToSearch)]) {
        [self.delegate searchBarPrepareToSearch];
    }
    
    searchBar.showsCancelButton = YES;
    
    return YES;
}
//
//- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
//{
//    
//}
//
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    
    if ([self.delegate respondsToSelector:@selector(searchBarStartSearch:withSearchContent:)]) {
        [self.delegate searchBarStartSearch:searchBar withSearchContent:searchBar.text];
    }
}


- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    if ([self.delegate respondsToSelector:@selector(searchBarCancerSearch)]) {
        [self.delegate searchBarCancerSearch];
    }
    searchBar.showsCancelButton = NO;
    [self.searchBar resignFirstResponder];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.folderCreateBtn.frame = CGRectMake(MarginX,(SearchBarH - BtnWH )/2.0f, BtnWH, BtnWH);
    self.fileSortBtn.frame = CGRectMake(CGRectGetMaxX(self.folderCreateBtn.frame) + MarginX, (SearchBarH - BtnWH )/2.0f,BtnWH, BtnWH);
    self.searchBar.frame = CGRectMake(CGRectGetMaxX(self.fileSortBtn.frame), 0, self.frame.size.width - self.folderCreateBtn.frame.size.width - self.fileSortBtn.frame.size.width - 2 * MarginX, SearchBarH);
//    self.segmentControl.frame = CGRectMake(0, CGRectGetMaxY(self.searchBar.frame), self.frame.size.width, SegmentControllH);
}

@end
