//
//  KKMusicPlayListCell.m
//  Open
//
//  Created by 雷俊博 on 2020/6/17.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "KKMusicPlayListCell.h"
#import "UIView+frameAdjust.h"
@interface KKMusicPlayListCell ()

@property (nonatomic, strong)NSTimer* timer;

@end
@implementation KKMusicPlayListCell{
    UILabel *_indexLabel;
    UIImageView *_playImageView;
    UILabel *_titleLable;
    UILabel *_countLabel;
    UILabel *_freeLable;
    UIButton *_playBtn;
    int imageIndex;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initView];
        imageIndex = 1;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
        
        [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
    }
    return self;
}

- (void)initView
{
    //索引
    _indexLabel = [UILabel new];
    _indexLabel.font = [UIFont boldSystemFontOfSize:16];
    _indexLabel.textColor = [UIColor colorWithHex:0xaaaaaa];
    [self.contentView addSubview:_indexLabel];
    [_indexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.equalTo(self.contentView);
        make.width.height.offset(40);
    }];
    
    _playImageView = [UIImageView new];
    [self.contentView addSubview:_playImageView];
    [_playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.offset(0);
        make.width.height.offset(27);
    }];
    
    _titleLable = [UILabel new];
    _titleLable.font = [UIFont systemFontOfSize:16];
    _titleLable.textAlignment = NSTextAlignmentLeft;
    _titleLable.textColor = [UIColor colorWithHex:0x4d4d4d];
    _titleLable.numberOfLines = 2;
    [self.contentView addSubview:_titleLable];
    
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(14);
        make.left.equalTo(self->_indexLabel.mas_right).offset(4);
        make.width.offset(ScreenWidth - 107);
    }];
    
    _playBtn = [UIButton new];
    [_playBtn setImage:[UIImage imageNamed:@"playlist_pause"] forState:UIControlStateNormal];
    [_playBtn setImage:[UIImage imageNamed:@"playlist_on"] forState:UIControlStateSelected];
    [_playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_playBtn];
    [_playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.equalTo(self.contentView);
        make.width.height.offset(27);
    }];
    
    //耳机图片
    UIImageView* lisenImg = [UIImageView new];
    lisenImg.image = [UIImage imageNamed:@"playHeadset"];
    [self.contentView addSubview:lisenImg];
    [lisenImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_titleLable.mas_bottom).offset(14);
        make.left.equalTo(self->_titleLable);
        make.width.height.offset(12);
    }];
    
    _countLabel = [UILabel new];
    _countLabel.font = [UIFont systemFontOfSize:12];
    _countLabel.textColor = [UIColor colorWithHex:0x808080];
    [self.contentView addSubview:_countLabel];
    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lisenImg);
        make.height.offset(18);
        make.left.equalTo(lisenImg.mas_right).offset(4);
    }];
    
    //免费
    _freeLable = [UILabel new];
    _freeLable.font = [UIFont systemFontOfSize:12];
    _freeLable.textColor = [UIColor whiteColor];
    _freeLable.text = @"免费";
    _freeLable.textAlignment = NSTextAlignmentCenter;
    _freeLable.layer.cornerRadius = 4;
    _freeLable.clipsToBounds = YES;
    _freeLable.backgroundColor = [UIColor colorWithHex:0x34c759];
    [self.contentView addSubview:_freeLable];
    [_freeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_countLabel.mas_right).offset(14);
        make.width.offset(40);
        make.height.offset(15);
        make.centerY.equalTo(self->_countLabel);
    }];
    
    UIView*lineView = [UIView new];
    lineView.backgroundColor = [UIColor colorWithHex:0xcccccc];
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_titleLable);
        make.bottom.equalTo(self.contentView);
        make.right.offset(0);
        make.height.offset(0.5);
    }];
    
    
    
}


- (void)setIndex:(NSInteger)index
{
    _index = index;
    
    _indexLabel.text = [NSString stringWithFormat:@"%ld",index + 1];
}

- (void)setPlayModel:(XMTrack *)playModel
{
    if (playModel == nil) {
        return;
    }
    _playModel = playModel;
    _titleLable.text = playModel.trackTitle;
    _countLabel.text = [self getStrWithCount:playModel.playCount];
}

- (void)setIsPlaying:(BOOL)isPlaying {
    _isPlaying = isPlaying;
    if (_isPlaying) {
        _playImageView.hidden = NO;
        _indexLabel.hidden = YES;
        NSMutableArray *imageArr = @[].mutableCopy;
        for (int i = 1; i <= 3; i++) {
            NSString *imageName = [NSString stringWithFormat:@"fullView_list_ playBtn_play_%d",i];
            UIImage *image = [UIImage imageNamed:imageName];
            [imageArr addObject:image];
        }

        if (self.playingState == XMSDKPlayerStatePlaying) {
            _playBtn.selected = YES;
            if (self.timer == nil) {
                self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
                           
                [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
            }
           
            [self.timer fire];
                
           
        }else if (self.playingState == XMSDKPlayerStatePaused){
            if (self.timer) {
                [self.timer invalidate];
                self.timer = nil;
            }
            _playBtn.selected = NO;
            _playImageView.image = [UIImage imageNamed:@"fullView_list_ playBtn_play_1"];
            
        }else if (self.playingState == XMSDKPlayerStateStop){
            if (self.timer) {
                [self.timer invalidate];
                self.timer = nil;
            }
            _playImageView.hidden = YES;
            _playBtn.selected = NO;
            _indexLabel.hidden = NO;
        }
       
    }else {
        if (self.timer) {
            [self.timer invalidate];
            self.timer = nil;
        }
        _playImageView.hidden = YES;
        _playBtn.selected = NO;
        _indexLabel.hidden = NO;
    }
    
    
}

#pragma mark - event response
- (void)playBtnClick:(UIButton *)sender
{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(btnClickWithCell:Btn:model:)]) {
        [self.delegate btnClickWithCell:self Btn:sender model:self.playModel];
    }
}


#pragma mark - private methods
- (NSString *)getStrWithCount:(NSInteger)count
{
    if (count < 10000) {
        return [NSString stringWithFormat:@"%ld",count];
    }else if (count < 100000000&& count>= 10000 ){
        return [NSString stringWithFormat:@"%ld万",count/10000];
    }else{
         return [NSString stringWithFormat:@"%ld亿",count/100000000];
    }
}

- (void)changeImage{
    imageIndex += 1;
    NSString *imageName = [NSString stringWithFormat:@"fullView_list_ playBtn_play_%d",imageIndex];
    /*
     imageNamed:(NSString *)name;会产生缓存
     imageWithContentsOfFile:(NSString *)path;不会产生缓存
     */
    _playImageView.image = [UIImage imageNamed:imageName];
    if (imageIndex == 3) {
        imageIndex = 1;
    }
}

- (void)dealloc
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
