//
//  LiveViewController.m
//  Yoni Mordechay
//
//  Created by Nguyen Viet Minh on 5/3/13.
//  Copyright (c) 2013 Hieu Nguyen. All rights reserved.
//

#import "LiveViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface LiveViewController ()

@end

@implementation LiveViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        
        // getting an NSString
        NSString *myString = [prefs stringForKey:@"language"];
        
        if([myString isEqualToString:@"عربي"])
        {
            UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"فعلت" style:UIBarButtonItemStylePlain
                                                                             target:self action:@selector(dismissView:)];
            self.navigationItem.rightBarButtonItem = anotherButton;
        }
        else
        {
            UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain
                                                                             target:self action:@selector(dismissView:)];
            self.navigationItem.rightBarButtonItem = anotherButton;
        }

    }
    return self;
}

-(IBAction)dismissView:(id)sender
{
    [videoPlayer pause];

    CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [[self navigationController] popViewControllerAnimated:NO];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationController.navigationBar setHidden:NO];
    
    NSURL *URL=[NSURL URLWithString:@"http://79.143.186.149:1935/live/Play/playlist.m3u8"];
  	videoPlayer=[[MPMoviePlayerController alloc] initWithContentURL:URL];
	//videoPlayer.view.frame = CGRectMake(0,0,320,420);
    if (IS_IPHONE_5) {
        
        [videoPlayer.view setFrame:CGRectMake(0,115,320,290)];
    }
    else
    {
        [videoPlayer.view setFrame:CGRectMake(0,115,320,270)];
        
    }
	//videoPlayer.view.frame = CGRectMake(50,50,200,200);
    
	[videoPlayer play];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:)name:MPMoviePlayerContentPreloadDidFinishNotification object:nil] ;
	[self.view addSubview: videoPlayer.view];

}
-(void) moviePreloadDidFinish:(id)sender {
	NSLog(@"preload");
 	
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    
    [[self navigationController] setNavigationBarHidden:NO animated:NO];
    [self.navigationItem setHidesBackButton:YES animated:NO];
    
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    [self setTitle:delegate.stringTitle];
    
}

@end
