//
//  VideoView.m
//  Yoni
//
//  Created by Hieu Nguyen on 4/22/13.
//  Copyright (c) 2013 Hieu Nguyen. All rights reserved.
//

#import "VideoView.h"
#import <QuartzCore/QuartzCore.h>
#import "ASIHTTPRequest.h"
#import "TFHpple.h"
#import <MediaPlayer/MediaPlayer.h>

@interface VideoView ()

@end

@implementation VideoView

@synthesize arrayPhotoView, stringURL;

- (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;
}


-(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];

}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(IBAction)dismissView:(id)sender
{
    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) Gallery_Internet
{
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:stringURL]];
    [request setDelegate:self];
    [request setRequestMethod:@"GET"];
    [request setDidFinishSelector:@selector(Load_Datasuccess_Internet:)];
    [request setDidFailSelector:@selector(Load_DataFaild:)];
    [request startAsynchronous];
}
-(void)Load_Datasuccess_Internet:(ASIHTTPRequest *)request
{
    [arrayPhotoView removeAllObjects];
    
     [loading dismissWithClickedButtonIndex:0 animated:YES];
    
    if([request responseData]){
        TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:[request responseData]];
  
        NSArray *as  = [xpathParser search:@"//entry/group/player"];
        
        NSLog(@"count: %d",[as count]);

        if (as != nil && [as count] > 0) {
            
            int numbOfAlbums = [as count];
            
            for(int i = 0 ; i < numbOfAlbums;i++){
                TFHppleElement *a = [as objectAtIndex:i];
                [arrayPhotoView addObject:[a objectForKey:@"url"]];
            }
        }
    }
    
    [TBL_Photo reloadData];
}
-(void)Load_DataFaild:(ASIHTTPRequest *)request
{
     [loading dismissWithClickedButtonIndex:0 animated:YES];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    loading = [[UILoading alloc]initWithTitle:@"" message:@"Loaing..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [loading show];
    
    arrayPhotoView = [[[NSMutableArray alloc] init] retain];
    
    [self Gallery_Internet];
    
 }


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([arrayPhotoView count] != 0)
    {
        int row_return = 0;
        row_return = [arrayPhotoView count] / 3;
        if(row_return*3 < [arrayPhotoView count])
        {
            row_return = row_return + 1;
        }
        //[self setrow_count_scr:row_return];
        return row_return;
    }
    else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (IS_IPHONE_5) {
        return 105;
    }else
    {
        return 105;
    }
}

-(void)play_video:(id)value{
    CustomButton *btn = (CustomButton *)value;

    NSLog(@"%d %@",btn.tag2, [arrayPhotoView objectAtIndex:btn.tag2]);
    
    NSURL *url = [NSURL URLWithString:[arrayPhotoView objectAtIndex:btn.tag2]];
    
    NSDictionary *qualities = [Utility Get_DirectionLink:url];
    MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[qualities objectForKey:@"medium"]]];
    [self presentModalViewController:mp animated:YES];
    
    mp = nil;
    [mp release];
    
   }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellIdentifier = @"Cell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
        
        //img 1 ------  ------  ------  ------  ------  ------  ------  ------  ------  
        UIView *vi1 = [[UIView alloc]initWithFrame:CGRectZero];
        UIImageView *img_body1 = [[UIImageView alloc]initWithFrame:CGRectZero];
        CustomButton *btn = [CustomButton buttonWithType:UIButtonTypeCustom];
        
        if (IS_IPHONE_5) {
            [vi1 setFrame:CGRectMake(7, 1, 100, 100)];
            [img_body1 setFrame:CGRectMake(0, 0,100,100)];
            [btn setFrame:CGRectMake(7, 1, 100, 100)];
        }else
        {
            [vi1 setFrame:CGRectMake(7, 1, 100, 100)];
            [img_body1 setFrame:CGRectMake(0, 0,100,100)];
            [btn setFrame:CGRectMake(7, 1, 100, 100)];
        }
        
        [vi1 setTag:1];
        [cell.contentView addSubview:vi1];
        
        [img_body1 setBackgroundColor:[UIColor clearColor]];
        [img_body1 setTag:11];
        [vi1 addSubview:img_body1];
        
        [btn setTag:15];
        [btn addTarget:self action:@selector(play_video:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn];
        
        //vi2 ------  ------  ------  ------  ------  ------  ------  ------  ------
        UIView *vi2 = [[UIView alloc]initWithFrame:CGRectZero];
        UIImageView *img_body2 = [[UIImageView alloc]initWithFrame:CGRectZero];
        CustomButton *btn2 = [CustomButton buttonWithType:UIButtonTypeCustom];
        
        if (IS_IPHONE_5) {
            [vi2 setFrame:CGRectMake(112, 1, 100, 100)];
            [img_body2 setFrame:CGRectMake(0, 0,100,100)];
            [btn2 setFrame:CGRectMake(112, 5, 100, 100)];

        }else
        {
            [vi2 setFrame:CGRectMake(112, 1, 100, 100)];
            [img_body2 setFrame:CGRectMake(0, 0,100,100)];
            [btn2 setFrame:CGRectMake(112, 5, 100, 100)];
        }
                
        [vi2 setTag:2];
        [cell.contentView addSubview:vi2];

        
        [img_body2 setBackgroundColor:[UIColor clearColor]];
        [img_body2 setTag:21];
        [vi2 addSubview:img_body2];
        
        [btn2 setTag:25];
        [btn2 addTarget:self action:@selector(play_video:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn2];
        
        //View 3 ------  ------  ------  ------  ------  ------  ------  ------  ------  
        UIView *vi3 = [[UIView alloc]initWithFrame:CGRectZero];
        UIImageView *img_body3 = [[UIImageView alloc]initWithFrame:CGRectZero];
        CustomButton *btn3 = [CustomButton buttonWithType:UIButtonTypeCustom];

        if (IS_IPHONE_5) {
            [vi3 setFrame:CGRectMake(217, 1, 100, 100)];
            [img_body3 setFrame:CGRectMake(0, 0,100,100)];
            [btn3 setFrame:CGRectMake(217, 1, 100, 100)];
        }else
        {
            [vi3 setFrame:CGRectMake(217, 1, 100, 100)];
            [img_body3 setFrame:CGRectMake(0, 0,100,100)];
            [btn3 setFrame:CGRectMake(217, 1, 100, 100)];
        }
        
        [vi3 setTag:3];
        [cell.contentView addSubview:vi3];
        
        [img_body3 setBackgroundColor:[UIColor clearColor]];
        [img_body3 setTag:31];
        [vi3 addSubview:img_body3];
        
        [btn3 setTag:35];
        [btn3 addTarget:self action:@selector(play_video:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn3];
        
//        //View 4 ------  ------  ------  ------  ------  ------  ------  ------  ------  
//        UIView *vi4 = [[UIView alloc]initWithFrame:CGRectZero];
//        UIImageView *img_body4 = [[UIImageView alloc]initWithFrame:CGRectZero];
//        CustomButton *btn4 = [CustomButton buttonWithType:UIButtonTypeCustom];
//        if (IS_IPHONE_5) {
//            [vi4 setFrame:CGRectMake(242, 1, 75, 75)];
//            [img_body4 setFrame:CGRectMake(0, 0,75,75)];
//            [btn4 setFrame:CGRectMake(242, 5, 75, 75)];
//        }else
//        {
//            [vi4 setFrame:CGRectMake(242, 1, 75, 75)];
//            [img_body4 setFrame:CGRectMake(0, 0,75,75)];
//            [btn4 setFrame:CGRectMake(242, 5, 75, 75)];
//        }
//
//        [vi4 setTag:4];
//        [cell.contentView addSubview:vi4];
//        
//        [img_body4 setBackgroundColor:[UIColor clearColor]];
//        [img_body4 setTag:41];
//        [vi4 addSubview:img_body4];
//        
//        [btn4 setTag:45];
//        [btn4 addTarget:self action:@selector(play_video:) forControlEvents:UIControlEventTouchUpInside];
//        [cell.contentView addSubview:btn4];
     }
    
    //--------------------------------------------------------------------------------------------------
    
    [((CustomButton*)[cell.contentView viewWithTag:15]) setTag2:indexPath.row*3];
    [((UIImageView*)[cell.contentView viewWithTag:11]) setImageWithURL:[NSURL URLWithString:[arrayPhotoView objectAtIndex:indexPath.row*3]] placeholderImage:[UIImage imageNamed:@"youtube-icon.png"]];
    
    
    if([[[arrayPhotoView objectAtIndex:indexPath.row*3] componentsSeparatedByString:@"youtube"] count]!=1)
    {
        [((UIImageView*)[cell.contentView viewWithTag:11]) setImageWithURL:[NSURL URLWithString:[Utility Get_Youtube_Image:[arrayPhotoView objectAtIndex:indexPath.row*3]]] placeholderImage:[UIImage imageNamed:@"youtube-icon.png"]];
    }
    else {
        [((UIImageView*)[cell.contentView viewWithTag:11]) setImageWithURL:[NSURL URLWithString:[arrayPhotoView objectAtIndex:indexPath.row*3]] placeholderImage:[UIImage imageNamed:@""]];
    }
    if(indexPath.row*3 + 1 < [arrayPhotoView count])
    {
        [((CustomButton*)[cell.contentView viewWithTag:25]) setHidden:NO];
        [((UIImageView*)[cell.contentView viewWithTag:21]) setHidden:NO];
        [((CustomButton*)[cell.contentView viewWithTag:25]) setTag2:indexPath.row*3+1];
        
        [((UIImageView*)[cell.contentView viewWithTag:21]) setImageWithURL:[NSURL URLWithString:[Utility Get_Youtube_Image:[arrayPhotoView objectAtIndex:indexPath.row*3+1]]] placeholderImage:[UIImage imageNamed:@"youtube-icon.png"]];
    }
    else {
        [((CustomButton*)[cell.contentView viewWithTag:25]) setHidden:YES];
        [((UIImageView*)[cell.contentView viewWithTag:21]) setHidden:YES];
    }
    
    if(indexPath.row*3 + 2 < [arrayPhotoView count])
    {
        [((CustomButton*)[cell.contentView viewWithTag:35]) setHidden:NO];
        [((UIImageView*)[cell.contentView viewWithTag:31]) setHidden:NO];
        [((CustomButton*)[cell.contentView viewWithTag:35]) setTag2:indexPath.row*3+2];
        
        [((UIImageView*)[cell.contentView viewWithTag:31]) setImageWithURL:[NSURL URLWithString:[Utility Get_Youtube_Image:[arrayPhotoView objectAtIndex:indexPath.row*3+2]]] placeholderImage:[UIImage imageNamed:@"youtube-icon.png"]];
    }
    else
    {
        [((CustomButton*)[cell.contentView viewWithTag:35]) setHidden:YES];
        [((UIImageView*)[cell.contentView viewWithTag:31]) setHidden:YES];
    }
    
    if(indexPath.row*3 + 3 < [arrayPhotoView count])
    {
        [((CustomButton*)[cell.contentView viewWithTag:45]) setHidden:NO];
        [((UIImageView*)[cell.contentView viewWithTag:41]) setHidden:NO];
        [((CustomButton*)[cell.contentView viewWithTag:45]) setTag2:indexPath.row*3+3];
        
        [((UIImageView*)[cell.contentView viewWithTag:41]) setImageWithURL:[NSURL URLWithString:[Utility Get_Youtube_Image:[arrayPhotoView objectAtIndex:indexPath.row*3+3]]] placeholderImage:[UIImage imageNamed:@"youtube-icon.png"]];
    }
    else
    {
        [((CustomButton*)[cell.contentView viewWithTag:45]) setHidden:YES];
        [((UIImageView*)[cell.contentView viewWithTag:41]) setHidden:YES];
    }
    
//    if(indexPath.row*3 + 4 < [arrayPhotoView count])
//    {
//        [((CustomButton*)[cell.contentView viewWithTag:55]) setHidden:NO];
//        [((UIImageView*)[cell.contentView viewWithTag:51]) setHidden:NO];
//        [((CustomButton*)[cell.contentView viewWithTag:55]) setTag2:indexPath.row*4+4];
//        
//        [((UIImageView*)[cell.contentView viewWithTag:51]) setImageWithURL:[NSURL URLWithString:[Utility Get_Youtube_Image:[arrayPhotoView objectAtIndex:indexPath.row*4+4]]] placeholderImage:[UIImage imageNamed:@"youtube-icon.png"]];
//    }
//    else
//    {
//        [((CustomButton*)[cell.contentView viewWithTag:55]) setHidden:YES];
//        [((UIImageView*)[cell.contentView viewWithTag:51]) setHidden:YES];
//    }
    
     return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
