//
//  CZQAddressPicker.m
//  Open
//
//  Created by 雷俊博 on 2019/12/24.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CZQAddressPicker.h"

@interface CZQAddressPicker ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    PayCityModel*selectedProvinceModel;
    PayCityModel*selectedCityModel;
}

@property (nonatomic, strong) UIPickerView *addressPicker;

@property (copy, nonatomic) NSString *title;//标题

@property (nonatomic,strong)NSArray<PayCityModel *>*citys;

@property (nonatomic,strong) NSMutableArray * dataSouce;
@property (nonatomic,strong) NSMutableArray * cityDataSouce;

@end

@implementation CZQAddressPicker
#pragma mark - life cyce
- (instancetype)initWithCitys:(NSArray *)citys
{
    self = [super init];
    if (self) {
        self.citys = citys;
        [self initUI];
    }
    return self;
}
- (void)initUI
{
    [super initUI];
    [self.alertView addSubview:self.addressPicker];
    
    if (self.citys.count > 0) {
        [self.addressPicker.delegate pickerView:self.addressPicker didSelectRow:0 inComponent:0];
    }
}

#pragma mark - UIPickerViewDataSource
/**
 *  一共多少咧
 *
 *  @param pickerView
 *
 *  @return
 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

/**
 *  返回每一列的行数
 *
 *  @param pickerView
 *  @param component
 *
 *  @return
 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.dataSouce.count;
    }
    else {
        
        
        return self.cityDataSouce.count;
    }
}

//每一行的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //第一列返回所有的省份
    if (component == 0) {
        PayCityModel*model = self.dataSouce[row];
        
        return model.name;
    }
    else {
//        self.ci = [self.cityNames valueForKey:self.selectedProvince];
        PayCityModel*model = self.cityDataSouce[row];
        return model.name;
    }
   
}

/**
 *  选中某一行后回调 联动的关键
 *
 *  @param pickerView
 *  @param row        用户选择的省份
 *  @param component
 */
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if (component == 0) {
        
        //选中的省份
        selectedProvinceModel = self.dataSouce[row];
        self.cityDataSouce = [self getCityDataSourceWithModel:self.dataSouce[row]];
        //重新加载第二列的数据
        [pickerView reloadComponent:1];
        //让第二列归位
        [pickerView selectRow:0 inComponent:1 animated:YES];
         
         selectedCityModel = self.cityDataSouce[0];
    }else{
        selectedCityModel = self.cityDataSouce[row];
    }
}
//返回高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 35.0f;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0) {
        return SCREEN_WIDTH/2;
    }
    return SCREEN_WIDTH/2;
}


#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender
{
    [self dismissWithAnimation:YES];
}

#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn
{
    [self dismissWithAnimation:YES];
}
#pragma mark - 确定按钮的点击事件
- (void)clickRightBtn
{
   [self dismissWithAnimation:YES];
    
    if (self.block && selectedProvinceModel && selectedCityModel) {
        self.block(selectedProvinceModel, selectedCityModel);
    }
}


#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 removeFromSuperview];
    }];
}

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

//省级别数据源
- (NSMutableArray *)dataSouce{
    
    if (_dataSouce == nil) {
        _dataSouce = [[NSMutableArray alloc]init];
        [self.citys enumerateObjectsUsingBlock:^(PayCityModel* obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.pid == 0 && obj.isCity == NO) {
                [_dataSouce addObject:obj];
            }
        }];
    }
    return _dataSouce;
}

- (NSMutableArray *)getCityDataSourceWithModel:(PayCityModel *)model
{
    
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    [self.citys enumerateObjectsUsingBlock:^(PayCityModel* obj, NSUInteger idx, BOOL * _Nonnull stop) {
               if (obj.pid == model.Id && obj.isCity == YES) {
                   obj.isSelected = NO;
                   [dataArray addObject:obj];
               }
           }];
    
    return dataArray;
}



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

@end
