//

//  MoviePlayerView.m

//  Project1--Movie

//

//  Created by imac on 15/9/4.

//  Copyright (c) 2015年 Hong. All rights reserved.

//


#import "MoviePlayerView.h"

#import "UIView+ViewController.h"


@implementation MoviePlayerView


- (void)awakeFromNib{

    //[super awakeFromNib];

    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:nil];

    

    //播放界面

    _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    //关闭子视图的自适应

   // _moviePlayer.backgroundView.autoresizesSubviews = NO;

    

    _moviePlayer.view.frame = self.bgView.bounds;

    _moviePlayer.controlStyle = MPMovieControlStyleNone;

    [_moviePlayer play];

    [self.bgView insertSubview:_moviePlayer.view atIndex:0];

    

    

//    //创建音量滑动条

//    _volumnSlider = [[MPVolumeView alloc]initWithFrame:CGRectMake(-75.f, 150.f, 160.f, 30.f)];

//    //!!MPVolumnView只有在真机才显示

//    _volumnSlider.transform = CGAffineTransformMakeRotation(M_PI_2);

//    _volumnSlider.layer.anchorPoint = CGPointMake(0, 0);

//

//    [self.volumnView addSubview:_volumnSlider];

    

    //自定义滑动条

    _volumeSlider = [[UISlider alloc]initWithFrame:CGRectMake(-35.f, 50.f, 110.f, 30.f)];

    _volumeSlider.transform = CGAffineTransformMakeRotation(-M_PI_2);

    _volumeSlider.maximumValue = 10;

    _volumeSlider.minimumValue = 0;

    _volumeSlider.value = 0;


    //音量条的滑块样式

    [_volumeSlider setThumbImage:[UIImage p_w_picpathNamed:@"point"] forState:UIControlStateNormal];

    [_volumeView addSubview:_volumeSlider];

    [_volumeSlider addTarget:self action:@selector(volumeChange:) forControlEvents:UIControlEventValueChanged];

    

    //进度条上滑块样式

    [_processSlider setThumbImage:[UIImage p_w_picpathNamed:@"progress_blue_bar@2x"] forState:UIControlStateNormal];

    [_processSlider setThumbImage:[UIImage p_w_picpathNamed:@"point"] forState:UIControlStateHighlighted];

    

    //添加通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(duration) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createProcessTimer) name:MPMoviePlayerNowPlayingMovieDidChangeNotification object:nil];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];


    //如果没有URl就播放本地视频

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];

    _moviePlayer.contentURL = [NSURL fileURLWithPath:filePath];

    [_moviePlayer play];

    

    //缩放按钮

    [_zoomBtn setImage:[UIImage p_w_picpathNamed:@"zoomin"] forState:UIControlStateNormal];

    [_zoomBtn setImage:[UIImage p_w_picpathNamed:@"zoomout"] forState:UIControlStateSelected];


    //锁定按钮

    [_lockBtn setTitle:@"Lock" forState:UIControlStateNormal];

    [_lockBtn setTitle:@"Unlock" forState:UIControlStateSelected];

    

    _leftTimeLabel.text = @"00:00:00";

}

#pragma mark--layoutSubviews

- (void)layoutSubviews{

    

    //获取屏幕方向

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    

    if (orientation == UIInterfaceOrientationPortrait) {

        self.bgView.size = CGSizeMake(kWidth, 250.f);

        self.frame = self.bgView.bounds;

    }else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){

        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

            self.bgView.size = CGSizeMake(kWidth, kHeight);


        }else{

            self.bgView.size = CGSizeMake(kHeight, kHeight);


        }

        self.frame = self.bgView.bounds;

    }else{

        NSLog(@"test");

    }

    

    

}

#pragma mark--Notification

- (void)duration{

    //视频时长

    _processSlider.maximumValue = _moviePlayer.duration;

    _rightTimeLabel.text = [self calculateTime:_moviePlayer.duration];

    

}

- (void)createProcessTimer{

    _processTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setProcess:) userInfo:nil repeats:YES];

}


- (void)movieFinish{

    [_processTimer invalidate];

    _processTimer = nil;

}

- (void)volumeChange:(UISlider *)slider{

    

    //[MPMusicPlayerController applicationMusicPlayer].volume = slider.value;

    

    

}


//设置进度

- (void)setProcess:(NSTimer *)timer{

    _second++;

    self.leftTimeLabel.text = [self calculateTime:_second];

    

    [_processSlider setValue:_second animated:YES];

}


- (NSString *)calculateTime:(NSTimeInterval)time{

    //小时

    int h = time / 3600;

    int retain = (int)time % 3600;

    

    //分钟

    int m = retain / 60;

    //秒

    int s = retain % 60;

    

    return [NSString stringWithFormat:@"%02d:%02d:%02d",h,m,s];

}

//- (void)setUrl:(NSString *)url{

//    _url = url;

//    

//    if (url.length > 0) {

//        _moviePlayer.contentURL = [NSURL URLWithString:_url];

//        _moviePlayer.contentURL = [NSURL URLWithString:url];

//        [_moviePlayer play];

//

//    }else{

//        //如果没有URl就播放本地视频

//        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];

//        _moviePlayer.contentURL = [NSURL fileURLWithPath:filePath];

//        [_moviePlayer play];

//    }

//}


#pragma mark-Touch Method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    self.upView.hidden = !self.upView.hidden;

    self.volumeView.hidden = !self.volumeView.hidden;

    self.bottomView.hidden = !self.bottomView.hidden;

}


- (IBAction)lockClick:(UIButton *)sender {

    sender.selected = !sender.selected;

}

- (IBAction)playClick:(UIButton *)sender {

    [_moviePlayer pause];

    sender.selected = !sender.selected;

    if (sender.selected) {

        [sender setImage:[UIImage p_w_picpathNamed:@"play"] forState:UIControlStateNormal];

        [_moviePlayer pause];

        [_processTimer invalidate];

        _processTimer = nil;

    }else{

        [sender setImage:[UIImage p_w_picpathNamed:@"pause"] forState:UIControlStateNormal];

        [_moviePlayer play];

        [self createProcessTimer];

    }

}


//进度条滑动触发事件

- (IBAction)processChange:(UISlider *)sender {

    self.leftTimeLabel.text = [self calculateTime:sender.value];

    //调整播放进度

    [_moviePlayer setCurrentPlaybackTime:sender.value];

}

- (IBAction)zoomClick:(UIButton *)sender {

    sender.selected = !sender.selected;

    if (sender.selected) {

//        self.bgView.transform = CGAffineTransformMakeRotation(-M_PI_2);

        //self.bgView.origin = CGPointMake(0 , 300);

        //self.bgView.transform = CGAffineTransformMakeScale(kHeight/self.bgView.size.width, kWidth/self.bgView.size.height);

        //self.bgView.frame =[UIScreen mainScreen].bounds;


//        self.bgView.transform = CGAffineTransformMakeTranslation(0, (kHeight-_moviePlayer.view.size.height)/2);

//        self.viewController.view.backgroundColor = [UIColor blackColor];

    }else{

//        self.bgView.transform = CGAffineTransformMakeTranslation(0, 0);

//        self.viewController.view.backgroundColor = [UIColor whiteColor];

    }

}

- (IBAction)columeClick:(UIButton *)sender {

    sender.selected = !sender.selected;

    if (sender.selected) {

        [sender setImage:[UIImage p_w_picpathNamed:@"volume_mute"] forState:UIControlStateNormal];

        _volumeSlider.value = 0;

        

    }else{

        [sender setImage:[UIImage p_w_picpathNamed:@"volume.png"] forState:UIControlStateNormal];

    }

}



@end