//
//  HJPlayerView.m
//  2clip
//
//  Created by mac on 16/1/3.
//  Copyright 漏 2016骞?huijimuhe. All rights reserved.
//

#import "HJMHPlayerView.h"

@interface HJMHPlayerView ()

@property (nonatomic, strong) HJMHPlayerProgressView* progressView;
@property (nonatomic, strong) UIButton* playButton;
@property (nonatomic, strong) NSDateFormatter* formatter;
@property (nonatomic, assign) NSInteger allTime;
@property (nonatomic, strong) NSString* allFormatterTime;
@property (nonatomic, strong) NSTimer* timer;
@property (nonatomic, strong) UIImageView* thumbImage;
@end

@implementation HJMHPlayerView

- (instancetype)initWithVideo:(Video*)video videoTime:(NSInteger)videoTime{
    if (self = [super init]) {
       // self.data=script;
        self.fadeTime = 0.5;
        //鍒濆鍖栨挱鏀炬寜閽?//        [self addSubview:self.playButton];
//        [self.playButton makeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(self.center);
//            make.size.equalTo(50);
//        }];
        //鍒濆鍖栭瑙堝浘
        //[self addSubview:self.thumbImage];
        //鍒濆鍖栬繘搴︽潯
        [self addSubview: self.progressView];
        [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.equalTo(self);
            make.width.equalTo(self.width);
            make.height.mas_equalTo(10);
        }];
    }
    return self;
}

-(void)didMoveToSuperview{
    
}

#pragma mark - 鏂规硶

- (void)showPlayer{
    self.hidden = NO;
    [self.timer invalidate];
    [UIView animateWithDuration:self.fadeTime animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        //鏄剧ず涓夌鍚庤嚜鍔ㄩ殣钘?        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoHiddenPlayer) userInfo:nil repeats:NO];
    }];
}

- (void)autoHiddenPlayer{
    self.returnBlock();
    [self hiddenPlayer];
}

- (void)hiddenPlayer{
    [self.timer invalidate];
    [UIView animateWithDuration:self.fadeTime animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}
- (void)updateCurrentTime:(CGFloat)currentTime bufferTime:(CGFloat)bufferTime allTime:(NSInteger)allTime{
    
    //鏇存柊鏃堕棿
    self.allTime = allTime;
    self.allFormatterTime = [self.formatter stringFromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:allTime]];
    
    //鏇存柊杩涘害鏉?    [self.progressView updateCurrentTime: currentTime / self.allTime];
    [self.progressView updateBufferTime: bufferTime / self.allTime];
}

- (void)updateValue:(handle) block{
    self.returnBlock = block;
}

#pragma mark - 鍗忚
- (void)playButtonTouchDown{
    self.playButton.selected = !self.playButton.isSelected;
    if([self.delegate respondsToSelector:@selector(playerTouchPlayerButton:)]){
        [self.delegate playerTouchPlayerButton: self];
    }
}

#pragma mark - 鎳掑姞杞?
-(UIButton*)playButton{
    if(_playButton==nil){
        _playButton=[[UIButton alloc]init];
        _playButton.tintColor = [UIColor whiteColor];
        UIImage *buttonNorImg = [[UIImage imageNamed:@"asmedia_play"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        UIImage *buttonSelImg = [[UIImage imageNamed:@"asmedia_pause"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [_playButton setBackgroundImage:buttonNorImg forState:UIControlStateNormal];
        [_playButton setBackgroundImage:buttonSelImg forState:UIControlStateSelected];
     
        [_playButton addTarget:self action:@selector(playButtonTouchDown) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playButton;
}

- (NSDateFormatter *)formatter{
    if(_formatter == nil) {
        _formatter = [[NSDateFormatter alloc] init];
        [_formatter setDateFormat:@"mm:ss"];
    }
    return _formatter;
}

- (HJMHPlayerProgressView *)progressView{
    if(_progressView == nil) {
        _progressView = [[HJMHPlayerProgressView alloc] initWithLineWidth:10 currentTimeColor:[UIColor redColor] bufferTimeColor:[UIColor grayColor] lineBackGroundColor:[UIColor whiteColor] thumbImg:nil];
    }
    return _progressView;
}

@end
// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com