IVTMeGiftViewController.m 12.7 KB
//
//  IVTMeGiftViewController.m
//  IVTMeLive
//
//  Created by XRG on 16/6/6.
//  Copyright © 2016年 Joky. All rights reserved.
//

#import "IVTMeGiftViewController.h"
#import "IVTMeGiftCell.h"
#import "IVTMyAccountViewController.h"




@interface IVTMeGiftViewController ()<UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>
{
    NSIndexPath *selectedIndexPath;
    NSInteger currentChineseCoinNumber;
}
@property(nonatomic,strong)UIButton *recharge;
@property(nonatomic,strong)UILabel *money;
@property(nonatomic,strong)UIImageView *chineseCoin;
@property(nonatomic,strong)UIButton *chongZhi;
@property(nonatomic,strong)UIButton *continueTo;
@property(nonatomic,strong)UIButton *send;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong) IVTMeGiftCell *giftCell;
@property (nonatomic,strong)NSArray *image;
@end

@implementation IVTMeGiftViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    currentChineseCoinNumber = [[IVTAccountTool getChineseCoinTotal] integerValue]/100;

    [self initBottom];
    [self creatCollectionView];

    self.view.frame =CGRectMake(0, kScreenHeight-240, kScreenWidth, 240);
    self.view.backgroundColor = [UIColor YXColorWithHexCode:@"1e1e1e" alpha:0.68];

    [self doRequest];
}

- (void)updataCurrentMiTang:(NSInteger)currentTotal{
    currentChineseCoinNumber = currentTotal;
    _money.text = [NSString stringWithFormat:@"%ld",(long)currentChineseCoinNumber];
}

- (void)doRequest
{
    [[IVTNetworkAPIClient sharedClient] getGiftList:nil success:^(NSDictionary *successData) {
        NSMutableArray *gifDataArray = [[successData valueForKey:@"data"] mutableCopy];
        for (NSInteger i=0; i<gifDataArray.count; i++) {
            NSMutableDictionary *dict = [[gifDataArray objectAtIndex:i] mutableCopy];
            [dict setObject:@"0" forKey:@"isSelected"];
            [gifDataArray replaceObjectAtIndex:i withObject:dict];
        }
        self.image = [gifDataArray copy];
        [self.collectionView reloadData];
    } error:^(NSDictionary *successData) {

    } failure:^(NSError *failureError) {
        LRLog(@"fialureError:%@",failureError);
    }];
    
    
}

#pragma mark - 初始化底部
-(void)initBottom{
    
    __unsafe_unretained typeof (self) wself = self;
    _recharge = [UIButton buttonWithType:UIButtonTypeCustom];
    [_recharge setBackgroundColor:[UIColor clearColor]];
    [_recharge setTitle:@"充值" forState:UIControlStateNormal];
    [_recharge setTitleColor:[UIColor IVTLightRedColor] forState:UIControlStateNormal];
    _recharge.titleLabel.font = [UIFont IVTH3Font];
    if ([_recharge.titleLabel respondsToSelector:@selector(setAdjustsFontForContentSizeCategory:)]) {
        [_recharge.titleLabel setAdjustsFontForContentSizeCategory:YES];
    }
    [self.view addSubview:_recharge];
    [_recharge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(wself.view.mas_bottom).with.offset(-11);
        make.left.equalTo(wself.view).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(40, 17));
    }];
    
    
    _money = [[UILabel alloc]init];
    _money.text = [NSString stringWithFormat:@"%ld",(long)currentChineseCoinNumber];
    _money.textColor = [UIColor YXColorWithHexCode:@"bcbcbc"];
    _money.font = [UIFont YXFontOfSize:16];
    [self.view addSubview:_money];
    [_money mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_recharge.mas_right).with.offset(8);
        make.bottom.equalTo(wself.view.mas_bottom).with.offset(-12);
        make.size.mas_equalTo(CGSizeMake([IVTThirdFuncTool widthOfLabelWithString:_money.text sizeOfFont:16 height:17 ] + 10, 17));
    }];
    
    _chineseCoin = [[UIImageView alloc]init];
    _chineseCoin.image = [UIImage imageNamed:@"小中国币(礼物下面)"];
    [self.view addSubview:_chineseCoin];
    [_chineseCoin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_money.mas_right).with.offset(6);
        make.bottom.equalTo(wself.view.mas_bottom).with.offset(-13);
        make.size.mas_equalTo(CGSizeMake(13, 13));
        
    }];
    
    _chongZhi = [UIButton buttonWithType:UIButtonTypeCustom];
    [_chongZhi setBackgroundColor:[UIColor clearColor]];
    [_chongZhi setBackgroundImage:[UIImage imageNamed:@"更多键"] forState:UIControlStateNormal];
    [_chongZhi addTarget:self action:@selector(clickRechargeButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_chongZhi];
    [_chongZhi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(wself.view.mas_bottom).with.offset(-11);
        make.left.equalTo(_chineseCoin.mas_right).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(9, 15));
    }];
    
    UIButton *clickButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [clickButton addTarget:self action:@selector(clickRechargeButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clickButton];
    [clickButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_recharge.mas_left);
        make.top.equalTo(_money.mas_top);
        make.right.equalTo(_chineseCoin.mas_right);
        make.bottom.equalTo(_recharge.mas_bottom);
    }];
    
    
    
    _send = [UIButton buttonWithType:UIButtonTypeCustom];
    [_send setTitle:@"发送" forState:UIControlStateNormal];
    [_send setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_send setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [_send setBackgroundImage:[UIImage imageNamed:@"发送按钮"] forState:UIControlStateNormal];
    [_send setBackgroundImage:[UIImage imageNamed:@"发送按钮-灰色"] forState:UIControlStateSelected];
    _send.titleLabel.font = [UIFont YXFontOfSize:17];
    [self.view addSubview:_send];
    [_send mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(wself.view.mas_bottom);
        make.right.equalTo(wself.view.mas_right);
        make.size.mas_equalTo(CGSizeMake(90, 40));
        
        
    }];
    [_send addTarget:self action:@selector(sendClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _continueTo = [UIButton buttonWithType:UIButtonTypeCustom];
    [_continueTo setTitle:@"x10" forState:UIControlStateNormal];
    [_continueTo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_continueTo setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [_continueTo setBackgroundImage:[UIImage imageNamed:@"x10 未选中"] forState:UIControlStateNormal];
    [_continueTo setBackgroundImage:[UIImage imageNamed:@"x10 选中-0"] forState:UIControlStateSelected];
     _continueTo.titleLabel.font = [UIFont YXFontOfSize:16];
    [_continueTo addTarget:self action:@selector(lianFaClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_continueTo];
    [_continueTo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(wself.view.mas_bottom).with.offset(-8);
        make.right.equalTo(_send.mas_left).with.offset(-22);
        make.size.mas_equalTo(CGSizeMake(35, 25));
    }];
}

- (void)lianFaClick{
    _continueTo.selected = !_continueTo.selected;
}

-(void)creatCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,  190.0)collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor redColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor  = [UIColor clearColor];
    _collectionView.contentSize = CGSizeMake(kScreenWidth*4, 190.0);
    [self.view addSubview:_collectionView];
    [_collectionView  registerClass:[IVTMeGiftCell class] forCellWithReuseIdentifier:@"giftCell"];
    
}




#pragma mark - delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.image.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    IVTMeGiftCell *giftCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"giftCell" forIndexPath:indexPath];

    giftCell.entity = self.image[indexPath.row];

    return giftCell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *gifDataArray = [self.image mutableCopy];
    for (NSInteger i=0; i<gifDataArray.count; i++) {
        if (indexPath.row != i) {
            NSMutableDictionary *dict = [[gifDataArray objectAtIndex:i] mutableCopy];
            [dict setObject:@"0" forKey:@"isSelected"];
            [gifDataArray replaceObjectAtIndex:i withObject:dict];
        }
    }
    self.image = [gifDataArray copy];

    NSMutableDictionary *dict = [[self.image objectAtIndex:indexPath.row] mutableCopy];
    if ([dict[@"isSelected"] integerValue] == 0) {
        [dict setObject:@"1" forKey:@"isSelected"];
        selectedIndexPath = indexPath;
    }
    else{
        [dict setObject:@"0" forKey:@"isSelected"];
        selectedIndexPath = nil;
    }
    NSMutableArray *gifImageArray = [self.image mutableCopy];
    [gifImageArray replaceObjectAtIndex:indexPath.row withObject:[dict copy]];
    self.image = [gifImageArray copy];

    _continueTo.selected = NO;
    if ([dict[@"type"] integerValue] == 0) {
        _continueTo.hidden = NO;
//        _continueTo.userInteractionEnabled = YES;
    }
    else{
        _continueTo.hidden = YES;
//        _continueTo.userInteractionEnabled = NO;
    }
    [_collectionView reloadData];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = [UIScreen mainScreen].bounds.size.width / 4.0;
    CGFloat itemHeight = 190.0/2.0;
    
    return CGSizeMake(itemWidth, itemHeight);
    
}

//这个方法是两个之间的间距(同一行cell的间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


-(void)clickRechargeButton
{
    NSLog(@"充值");
    
    IVTMyAccountViewController *account = [[IVTMyAccountViewController alloc]init];
    [self.navigationController pushViewController:account animated:YES];
    
}



#pragma mark - 发送点击
-(void)sendClick:(UIButton *)sender{
    if (selectedIndexPath) {
        NSInteger price = [[[self.image objectAtIndex:selectedIndexPath.row] valueForKey:@"price"] integerValue];
        if (_continueTo.selected && _continueTo.hidden == NO) {
            price = 10*price;
        }
        if (currentChineseCoinNumber > price) {

            if (self.delegate && [self.delegate respondsToSelector:@selector(sendGiftWithData:isTenTimes:)]) {
                [self.delegate sendGiftWithData:[self.image objectAtIndex:selectedIndexPath.row] isTenTimes:_continueTo.selected];
            }
        }
        else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"余额不足,请充值!" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"去充值" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self recharge];
            }]];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    }
    else{
        UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择礼物" preferredStyle:UIAlertControllerStyleAlert];
        [sheet addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:sheet animated:YES completion:^{
            
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end