//
//  FourVideosViewController.m
//  MiAlerts
//
//  Created by Mahammad on 10/3/14.
//  Copyright (c) 2014 Mahammad. All rights reserved.
//

#import "FourVideosViewController.h"

@interface FourVideosViewController ()

@end

@implementation FourVideosViewController

@synthesize mux_cams_urls0,mux_cams_urls1,mux_cams_urls2,mux_cams_urls3;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
/*
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
*/
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//@end


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    return;
    CGRect viewBounds = self.view.bounds;
    
    //self.subView1.frame = CGRectMake(self.menuItemMidPosition,viewBounds.size.height-SUBVIEW1_HEIGHT,SUBVIEW1_WIDTH,SUBVIEW1_HEIGHT);

    
    
    if (UIDeviceOrientationIsLandscape([self interfaceOrientation]))
    {
        //x,y as you want
        //[ button setFrame:CGRectMake:(x,y,button.width,button.height)];

        
        int nHeight = viewBounds.size.height/2 - 5;
        int nWidth =  viewBounds.size.width/2 - 5;
        mPlaybackView1.frame = CGRectMake(viewBounds.origin.x,viewBounds.origin.y,nWidth,nHeight);
        mPlaybackView2.frame = CGRectMake(viewBounds.origin.x+nWidth,viewBounds.origin.y,nWidth,nHeight);
        mPlaybackView3.frame = CGRectMake(viewBounds.origin.x,viewBounds.origin.y+nHeight,nWidth,nHeight);
        mPlaybackView4.frame = CGRectMake(viewBounds.origin.x+nWidth,viewBounds.origin.y+nHeight,nWidth,nHeight);

        NSLog(@"Rect1: %@", NSStringFromCGRect(mPlaybackView1.frame));
        NSLog(@"Rect2: %@", NSStringFromCGRect(mPlaybackView2.frame));
        NSLog(@"Rect3: %@", NSStringFromCGRect(mPlaybackView3.frame));
        NSLog(@"Rect4: %@", NSStringFromCGRect(mPlaybackView4.frame));

        //[mPlaybackView1 ]
        
    }
    else
    {
        //In potrait
        //x,y as you want
        //      [ button setFrame:CGRectMake:(x,y,button.width,button.height)];
        
        int nHeight = viewBounds.size.height/4 - 5;
        int nWidth =  viewBounds.size.width*2/3 - 5;
        int nXPadding = viewBounds.size.width*1/3 - 5;
        mPlaybackView1.frame = CGRectMake(viewBounds.origin.x,viewBounds.origin.y,nWidth,nHeight);
        mPlaybackView2.frame = CGRectMake(viewBounds.origin.x+nXPadding,viewBounds.origin.y+nHeight,nWidth,nHeight);
        mPlaybackView3.frame = CGRectMake(viewBounds.origin.x,          viewBounds.origin.y+2*nHeight,nWidth,nHeight);
        mPlaybackView4.frame = CGRectMake(viewBounds.origin.x+nXPadding,viewBounds.origin.y+3*nHeight,nWidth,nHeight);
        
        
        NSLog(@"Rect1: %@", NSStringFromCGRect(mPlaybackView1.frame));
        NSLog(@"Rect2: %@", NSStringFromCGRect(mPlaybackView2.frame));
        NSLog(@"Rect3: %@", NSStringFromCGRect(mPlaybackView3.frame));
        NSLog(@"Rect4: %@", NSStringFromCGRect(mPlaybackView4.frame));

    }
    
}

static void *AVPlayerDemoPlaybackViewControllerStatusObservationContext = &AVPlayerDemoPlaybackViewControllerStatusObservationContext;
//@implementation AVFoundation_Test3ViewController
@synthesize mPlayer1, mPlaybackView1, mPlaybackView2, mPlayer2, mPlaybackView3, mPlayer3, mPlaybackView4, mPlayer4;

- (void)dealloc
{
    NSLog(@"dealloc2");

    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 //   NSString* path = [[NSBundle mainBundle] pathForResource:@"NoSignal" ofType:@"jpg"];
  //  NSString* path = [[NSBundle mainBundle] pathForResource:@"nosignal3202" ofType:@"mp4"];
   // NSLog(@"Nosingal path %@", path);
    NSURL *NoSignal = [NSURL URLWithString:@"http://www.theautoi.com/vid_ftp/sdcards/nosignal_ph.mp4"];

    //[NSURL fileURLWithPath:path];
    ;//[[NSBundle mainBundle] URLForResource:@"nosigngal_sound" withExtension:@"mp4"];
    /*
    if(mux_cams_urls1 == nil)
        mux_cams_urls1 = NoSignal;
    if(mux_cams_urls2 == nil)
        mux_cams_urls2 = NoSignal;
    if(mux_cams_urls3 == nil)
        mux_cams_urls3 = NoSignal;
    if(mux_cams_urls0 == nil)
        mux_cams_urls0  = NoSignal;*/
    
    //NSURL *url = [NSURL URLWithString:@"http://www.samkeeneinteractivedesign.com/videos/littleVid3.mp4"];
    NSURL *url1 = NoSignal;//[NSURL URLWithString:@"http://www.theautoi.com/vid_ftp/sdcards/nosignal_ph.mp4"];
    NSURL *url2 = NoSignal;//[NSURL URLWithString:mux_cams_urls1];//@"http://www.theautoi.com/vid_ftp/2914BBBK0026/usb1_20141006-202545.194660.mp4"];
    NSURL *url3 = NoSignal;//[NSURL URLWithString:mux_cams_urls2];//@"http://www.theautoi.com/vid_ftp/2914BBBK0026/usb2_20141006-202545.194660.mp4"];
    NSURL *url4 = NoSignal;//[NSURL URLWithString:mux_cams_urls3];//@"http://www.theautoi.com/vid_ftp/2914BBBK0026/usb3_20141006-202545.194660.mp4"];
    if(mux_cams_urls0 != nil)
    {
        url1 = [self getURL:mux_cams_urls0] ;//]();[NSURL URLWithString:mux_cams_urls0];//@"http://www.theautoi.com/vid_ftp/2914BBBK0026/usb0_20141006-202545.194660.mp4"];
    }
    
    self.mPlayer1 = [AVPlayer playerWithURL:url1];
    [mPlayer1 addObserver:self forKeyPath:@"status" options:0 context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];
    
    if(mux_cams_urls1 != nil)
    {
        url2 = [self getURL:mux_cams_urls1];//[NSURL URLWithString:mux_cams_urls1];//@"http://www.theautoi.com/vid_ftp/2914BBBK0026/usb1_20141006-202545.194660.mp4"];
    }
    
    self.mPlayer2 = [AVPlayer playerWithURL:url2];
    [mPlayer2 addObserver:self forKeyPath:@"status" options:0 context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];
    
    if(mux_cams_urls2 != nil)
    {
        url3 = [self getURL:mux_cams_urls2] ;//[NSURL URLWithString:mux_cams_urls2];//@"http://www.theautoi.com/vid_ftp/2914BBBK0026/usb2_20141006-202545.194660.mp4"];
    }
    
    self.mPlayer3 = [AVPlayer playerWithURL:url3];
    [mPlayer3 addObserver:self forKeyPath:@"status" options:0 context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];
    
    if(mux_cams_urls3 != nil)
    {
        url4 = [self getURL:mux_cams_urls3] ;//[NSURL URLWithString:mux_cams_urls3];//@"http://www.theautoi.com/vid_ftp/2914BBBK0026/usb3_20141006-202545.194660.mp4"];
    }
    
    self.mPlayer4 = [AVPlayer playerWithURL:url4];
    [mPlayer4 addObserver:self forKeyPath:@"status" options:0 context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];
    
    [self viewWillLayoutSubviews];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    
    
  /*  self.mPlayer2 = [AVPlayer playerWithURL:url2];
    
    self.mPlayer3 = [AVPlayer playerWithURL:url3];
    self.mPlayer4 = [AVPlayer playerWithURL:url4];
    */
}
-(NSURL*)getURL:(NSString*)path
{
    //[self getURL:mux_cams_urls0[ ;//]();
    
    if([path rangeOfString:@"file:"].length!=0)
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
        NSURL *documentsDirectory = [URLs objectAtIndex:0];
        NSURL *destinationURL = [documentsDirectory URLByAppendingPathComponent:[path lastPathComponent]];
        return destinationURL;
    }
    return [NSURL URLWithString:path];
                                
}

-(NSURL*)getShortURL:(NSString*) videoURL
{
    NSString* local_file = [self getLocalFilePath: videoURL];
    if( [self isFilePathExists:local_file] )
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
        NSURL *documentsDirectory = [URLs objectAtIndex:0];
        NSURL *destinationURL = [documentsDirectory URLByAppendingPathComponent:[local_file lastPathComponent]];
        return destinationURL;
    }
    
    return [NSURL URLWithString:videoURL];
}

-(Boolean)isFilePathExists:(NSString*) local_filename
{
    NSFileManager* fileMgr = [NSFileManager defaultManager];
    BOOL fileExists = [fileMgr fileExistsAtPath:local_filename];
    return fileExists;
}

-(NSString*)getLocalFileName:(NSString*) videoURL
{
    if(videoURL == nil)
        return nil;
    
    NSString* local_filename = [[videoURL lowercaseString] stringByReplacingOccurrencesOfString:@"http://www.theautoi.com/vid_ftp/" withString:@"_"];
    local_filename = [local_filename stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    //local_filename = [local_filename lowercaseString];
    NSLog(@"Local filename is %@", local_filename);
    
    return local_filename;
}

-(NSString*)getLocalFilePath:(NSString*) videoURL
{
    if(videoURL == nil)
        return nil;
    NSString* filename = [self getLocalFileName:videoURL];
    return [self getFullFilePath:filename];
}

-(NSString*)getFullFilePath:(NSString*) filename
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    if([paths count] > 0)
    {
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* local_filename = [documentsDirectory stringByAppendingPathComponent:filename];
        
        NSLog(@"Video full path: %@", local_filename);
        return local_filename;
    }
    
    return filename;
}

- (void)observeValueForKeyPath:(NSString*) path ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{

    
    if(object == mPlayer1)
    if (mPlayer1.status == AVPlayerStatusReadyToPlay) {
        [self.mPlaybackView1 setPlayer:self.mPlayer1];
        [self.mPlayer1 play];
    }
 
    if(object == mPlayer2)
        if (mPlayer2.status == AVPlayerStatusReadyToPlay) {
        [self.mPlaybackView2 setPlayer:self.mPlayer2];
        [self.mPlayer2 play];
    }
 
    if(object == mPlayer3)
        if (mPlayer3.status == AVPlayerStatusReadyToPlay) {
        [self.mPlaybackView3 setPlayer:self.mPlayer3];
        [self.mPlayer3 play];
    }
 
    if(object == mPlayer4)
        if (mPlayer4.status == AVPlayerStatusReadyToPlay) {
        [self.mPlaybackView4 setPlayer:self.mPlayer4];
        [self.mPlayer4 play];
    }
    
    CGRect viewBounds = self.view.bounds;
   
    int nMargin = 3;
    int nXPadding = 0;//viewBounds.size.width/3;// 5;
    int nHeight = viewBounds.size.height/4;
    int nWidth =  (viewBounds.size.width-nXPadding-2*nMargin);
 
    
    mPlaybackView1.frame = CGRectMake(viewBounds.origin.x+nMargin,          viewBounds.origin.y+nMargin,nWidth,nHeight-nMargin);
    mPlaybackView2.frame = CGRectMake(viewBounds.origin.x+nMargin+nXPadding,viewBounds.origin.y+nMargin+nHeight,nWidth,nHeight-nMargin);
    mPlaybackView3.frame = CGRectMake(viewBounds.origin.x+nMargin,          viewBounds.origin.y+nMargin+2*nHeight,nWidth,nHeight-nMargin);
    mPlaybackView4.frame = CGRectMake(viewBounds.origin.x+nMargin+nXPadding,viewBounds.origin.y+nMargin+3*nHeight,nWidth,nHeight-nMargin);
    
    [ mPlaybackView1 setVideoFillMode:AVLayerVideoGravityResize];
    [ mPlaybackView2 setVideoFillMode:AVLayerVideoGravityResize];
    [ mPlaybackView3 setVideoFillMode:AVLayerVideoGravityResize];
    [ mPlaybackView4 setVideoFillMode:AVLayerVideoGravityResize];
    
    
    NSLog(@"Rect1: %@", NSStringFromCGRect(mPlaybackView1.frame));
    NSLog(@"Rect2: %@", NSStringFromCGRect(mPlaybackView2.frame));
    NSLog(@"Rect3: %@", NSStringFromCGRect(mPlaybackView3.frame));
}


@end