//
//  CYAddressSelectedView.m
//  Open
//
//  Created by 雷俊博 on 2019/10/8.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CYAddressSelectedView.h"

@interface CYAddressSelectedView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *picker;    //选择器
@property (copy, nonatomic) NSString *title;

@property (nonatomic, assign)AddressType type;
@property (strong, nonatomic) CYAddressResultBlock resultBlock;

@property (strong, nonatomic) CYAddressResultBlock2 resultBlock2;

@property (copy, nonatomic) NSDictionary *selectValue;   //选择的值
@property (copy, nonatomic) NSString *selectValue2;   //选择的值
@property (strong, nonatomic) NSArray<NSDictionary *> *data;
@property (strong, nonatomic) NSArray<NSString *> *data2;

@end

@implementation CYAddressSelectedView
#pragma mark - life cycle
- (instancetype)initWithTitle:(NSString *)title data:(NSArray *)data addressType:(AddressType)type resultBlock:(CYAddressResultBlock)resultBlock
{
    self = [super init];
    if (self) {
        _title = title;
        _type = type;
        _data = data;
        _resultBlock = resultBlock;
        
        if (_data.count > 0) {
            _selectValue = _data[0];
        }
        
         [self initUI];
    }
    return self;
}

- (void)initUI
{
    [super initUI];
    self.titleLabel.text = _title;
    
    //添加时间选择器
    [self.alertView addSubview:self.picker];
}

#pragma mark - life cycle2
- (instancetype)initWithTitle2:(NSString *)title data:(NSArray *)data addressType:(AddressType)type resultBlock:(CYAddressResultBlock2)resultBlock
{
    self = [super init];
    if (self) {
        _title = title;
        _type = type;
        _data2 = data;
        _resultBlock2 = resultBlock;
        
        if (_data2.count > 0) {
            _selectValue2 = _data2[0];
        }
        
         [self initUI];
    }
    return self;
}




#pragma mark - public methods
+ (void)showAddressPickerWithTitle:(NSString *)title data:(NSArray *)data addressType:(AddressType)type resultBlock:(nonnull CYAddressResultBlock)resultBlock
{
    CYAddressSelectedView *pickerView = [[CYAddressSelectedView alloc]initWithTitle:title data:data addressType:type resultBlock:resultBlock];
    [pickerView show];
}



+ (void)show2AddressPickerWithTitle:(NSString *)title data:(NSArray *)data addressType:(AddressType)type resultBlock2:(CYAddressResultBlock2)resultBlock
{
    CYAddressSelectedView *pickerView = [[CYAddressSelectedView alloc]initWithTitle2:title data:data addressType:type resultBlock:resultBlock];
    [pickerView show];
}

- (void)show
{
    [self showWithAnimation:YES];
}

#pragma mark - UIPickerViewDataSource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (self.type == AddressTypeNational) {
        return self.data2.count;
    }
    
    return self.data.count;
}

//每一行的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if (self.type == AddressTypeNational) {
        return [NSString stringWithFormat:@"%@",self.data2[row]];
    }
    
    NSDictionary *dic = self.data[row];
    if (self.type == AddressTypeProvince) {
        
        return [NSString stringWithFormat:@"%@",dic[@"provice_name"]];
    }else if (self.type == AddressTypeCity){
         return [NSString stringWithFormat:@"%@",dic[@"city_name"]];
    }else if (self.type == AddressTypeCounties){
        return [NSString stringWithFormat:@"%@",dic[@"county_name"]];
    }else if (self.type == AddressTypeTown){
        return [NSString stringWithFormat:@"%@",dic[@"town_name"]];
    }else if (self.type == AddressTypeVillage){
        return [NSString stringWithFormat:@"%@",dic[@"village_name"]];
    } else if (self.type == AddressTypeSex){
        return [NSString stringWithFormat:@"%@",dic[@"name"]];
    }else if (self.type == AddressTypeAuthenticationType){
        return [NSString stringWithFormat:@"%@",dic[@"value"]];
    }else if (self.type == AddressTypeAuthenticationResult){
        return [NSString stringWithFormat:@"%@",dic[@"name"]];
    }
    return [NSString stringWithFormat:@"%@",self.data[row]];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if (self.type == AddressTypeNational) {
         self.selectValue2 = self.data2[row];
    }else{
         self.selectValue = self.data[row];
    }
    
   
}
//返回高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 35.0f;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return SCREEN_WIDTH;
}

#pragma mark - event response
- (void)clickLeftBtn
{
    [self dismissWithAnimation:YES];
}
- (void)clickRightBtn
{
    NSLog(@"点击确定按钮后,执行block回调");
    [self dismissWithAnimation:YES];
    
    NSLog(@"%@",[NSThread currentThread]);
    if (_resultBlock && _selectValue) {
        _resultBlock(_selectValue);
    }
    
    if (_resultBlock2 && _selectValue2) {
        _resultBlock2(_selectValue2);
    }
}
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender
{
    [self dismissWithAnimation:NO];
}


#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation
{
    
    //1.获取当前应用的主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview: self];
    if (animation) {
        //动画前初始位置
        CGRect rect = self.alertView.frame;
        rect.origin.y = SCREEN_HEIGHT;
        self.alertView.frame = rect;
        
        //浮现动画
        [UIView animateWithDuration:0.3 animations:^{
            CGRect rect = self.alertView.frame;
            rect.origin.y -= kDatePicHeight + kTopViewHeight;
            self.alertView.frame = rect;
        }];
    }
}

#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation
{
    
    //关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += kDatePicHeight + kTopViewHeight;
        self.alertView.frame = rect;
        
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.leftBtn removeFromSuperview];
        [self.rightBtn removeFromSuperview];
        [self.titleLabel removeFromSuperview];
        [self.lineView removeFromSuperview];
        [self.topView removeFromSuperview];
        [self.picker removeFromSuperview];
        [self.alertView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        [self removeFromSuperview];
        
        self.leftBtn = nil;
        self.rightBtn = nil;
        self.titleLabel  = nil;
        self.lineView = nil;
        self.topView = nil;
        self.picker = nil;
        self.alertView = nil;
        self.backgroundView = nil;
    }];
}

#pragma mark - getters and setters
- (UIPickerView *)picker
{
    if (!_picker) {
        _picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, kTopViewHeight + 0.5, SCREEN_WIDTH, kDatePicHeight)];
        _picker.showsSelectionIndicator = YES;
        _picker.delegate = self;
        _picker.dataSource = self;
    }
    return _picker;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
