//
//  SideMenuVC.m
//  Gilliatte
//
//  Created by Lead on 11/08/16.
//  Copyright © 2016 Lead infosoft. All rights reserved.
//

#import "SideMenuVC.h"
#import "ApiUtillity.h"
#import "AppDelegate.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "EnterDailyReportMainVC.h"
#import "SlideMenuController.h"

#import "EnterDailyReportVC.h"
#import "SettingVC.h"
#import "FaqVC.h"
#import "AboutusVC.h"

@interface SideMenuVC (){
    NSArray *imageArray_white;
    NSArray *imageArray_Black;
    NSArray *nameArray;
    
    UITapGestureRecognizer *imageTap;
    
    EnterDailyReportVC *EnterDailyReport;
    FaqVC *Faq;
    AboutusVC *Aboutus;
    SettingVC *Setting;
}
@end

@implementation SideMenuVC

#pragma mark- ViewDidload
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    self.imageview_User.layer.cornerRadius = self.imageview_User.frame.size.width / 2.0f;
    self.imageview_User.layer.borderWidth = 1.0f;
    self.imageview_User.layer.borderColor = [UIColor redColor].CGColor;
    self.imageview_User.clipsToBounds = YES;
    
    NSData *GetUserData;
    if ((GetUserData = [[NSUserDefaults standardUserDefaults] objectForKey:@"USER_PROFILE"])) {
        NSMutableArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:GetUserData];
        NSMutableArray *UserArray = [[NSMutableArray alloc]init];
        UserArray = savedArray;
        
        NSLog(@"%@",UserArray);
        NSString *UserImage = [NSString stringWithFormat:@"%@",[UserArray valueForKey:@"profile_pic"]];
        NSString *UserName = [NSString stringWithFormat:@"%@",[UserArray valueForKey:@"name"]];
        _lbl_UserName.text = UserName;
        
        NSString *capitalizedUserName = [UserName capitalizedString];
        NSLog(@"capitalizedUserName :-- %@",capitalizedUserName);
        
        //        NSString *userPic;
        //        if ([UserImage rangeOfString:@"/"].location == NSNotFound) {
        //            userPic = [@"http://new.mywebdemo.in/gilliatte_app/images/uploads/" stringByAppendingString:UserImage];
        //        }
        //        else{
        //            userPic = UserImage;
        //        }
        [_ActivityIndicator startAnimating];
        [_imageview_User sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",UserImage]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [_ActivityIndicator stopAnimating];
        }];
        //_lbl_UserName.text = capitalizedUserName;
    }
    
    imageArray_Black = [[NSArray alloc]initWithObjects:@"S_Home_black.png",@"S_FAQ_black.png",@"S_Settings_black.png",nil];
    imageArray_white = [[NSArray alloc]initWithObjects:@"S_Home_white.png",@"S_FAQ_white.png",@"S_Settings_white.png",nil];
    nameArray = [[NSArray alloc]initWithObjects:@"Home",@"Faq",@"Settings",nil];
    
    
//    [ApiUtillity putView:_imageview_User insideShadowWithColor:[UIColor whiteColor] andRadius:20 andOffset:CGSizeMake(0, 0) andOpacity:0.2];
}

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


#pragma mark- All Methode
- (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
    cell.contentView.backgroundColor = color;
    cell.backgroundColor = color;
}
- (void)reloadRootViewController
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    UIStoryboard *storybaord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    delegate.window.rootViewController = [storybaord instantiateInitialViewController];
}

#pragma mark- Tableview Methode
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return nameArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //MenuCell
    static NSString *Indentifier = @"MenuCell";
    UITableViewCell *cell = [_tableview_Menu dequeueReusableCellWithIdentifier:Indentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Indentifier];
    }
    
    UIImageView *imageView = [cell viewWithTag:1];
    imageView.image = [UIImage imageNamed:imageArray_Black[indexPath.row]];
    
    UILabel *label = [cell viewWithTag:2];
    label.text = nameArray[indexPath.row];
    
    UIView *colorView = [cell viewWithTag:3];
    colorView.backgroundColor = [UIColor colorWithRed:161.0/255.0 green:161.0/255.0 blue:161.0/255.0 alpha:1.0f];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = (UITableViewCell *)[_tableview_Menu cellForRowAtIndexPath:indexPath];
    
    UIImageView *imageView = [cell viewWithTag:1];
    imageView.image = [UIImage imageNamed:imageArray_white[indexPath.row]];
    
    UILabel *label = [cell viewWithTag:2];
    label.text = nameArray[indexPath.row];
    label.textColor = [UIColor whiteColor];
    
    UIView *colorView = [cell viewWithTag:3];
    colorView.backgroundColor = [UIColor colorWithRed:243.0/255.0 green:13.0/255.0 blue:41.0/255.0 alpha:1.0f];
    
    [self setCellColor:[UIColor colorWithRed:243.0/255.0 green:13.0/255.0 blue:41.0/255.0 alpha:1.0f] ForCell:cell];  //highlight colour
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = (UITableViewCell *)[_tableview_Menu cellForRowAtIndexPath:indexPath];
    
    UIImageView *imageView = [cell viewWithTag:1];
    imageView.image = [UIImage imageNamed:imageArray_Black[indexPath.row]];
    
    UILabel *label = [cell viewWithTag:2];
    label.text = nameArray[indexPath.row];
    label.textColor = [UIColor blackColor];
    
    UIView *colorView = [cell viewWithTag:3];
    colorView.backgroundColor = [UIColor colorWithRed:161.0/255.0 green:161.0/255.0 blue:161.0/255.0 alpha:1.0f];
    
    [self setCellColor:[UIColor colorWithRed:161.0/255.0 green:161.0/255.0 blue:161.0/255.0 alpha:1.0f] ForCell:cell];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0)
    {
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"PROJECT_ID"];
        
//        [self.slideMenuController closeLeft];
        //FAQ
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            EnterDailyReport = (EnterDailyReportVC *)[storyboard instantiateViewControllerWithIdentifier:@"EnterDailyReportVC"];
            self.mainViewControler = [[UINavigationController alloc] initWithRootViewController: EnterDailyReport];
            [self.slideMenuController changeMainViewController:self.mainViewControler close:YES];
        }
        else{
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStory_iPad" bundle:nil];
            EnterDailyReport = (EnterDailyReportVC *)[storyboard instantiateViewControllerWithIdentifier:@"EnterDailyReportVC"];
            self.mainViewControler = [[UINavigationController alloc] initWithRootViewController: EnterDailyReport];
            [self.slideMenuController changeMainViewController:self.mainViewControler close:YES];
            
        }
    }
    else if (indexPath.row == 1){
        //FAQ
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            Faq = (FaqVC *)[storyboard instantiateViewControllerWithIdentifier:@"FaqVC"];
            self.mainViewControler = [[UINavigationController alloc] initWithRootViewController: Faq];
            [self.slideMenuController changeMainViewController:self.mainViewControler close:YES];
        }
        else{
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStory_iPad" bundle:nil];
            Faq = (FaqVC *)[storyboard instantiateViewControllerWithIdentifier:@"FaqVC"];
            self.mainViewControler = [[UINavigationController alloc] initWithRootViewController: Faq];
            [self.slideMenuController changeMainViewController:self.mainViewControler close:YES];
            
        }
    }
//    else if (indexPath.row == 2){
//        //About Us
//        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
//            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//            Aboutus = (AboutusVC *)[storyboard instantiateViewControllerWithIdentifier:@"AboutusVC"];
//            self.mainViewControler = [[UINavigationController alloc] initWithRootViewController: Aboutus];
//            [self.slideMenuController changeMainViewController:self.mainViewControler close:YES];
//        }
//        else{
//            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStory_iPad" bundle:nil];
//            Aboutus = (AboutusVC *)[storyboard instantiateViewControllerWithIdentifier:@"AboutusVC"];
//            self.mainViewControler = [[UINavigationController alloc] initWithRootViewController: Aboutus];
//            [self.slideMenuController changeMainViewController:self.mainViewControler close:YES];
//        }
//    }
    else if (indexPath.row == 2){
        //SettingVC
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            Setting = (SettingVC *)[storyboard instantiateViewControllerWithIdentifier:@"SettingVC"];
            self.mainViewControler = [[UINavigationController alloc] initWithRootViewController: Setting];
            [self.slideMenuController changeMainViewController:self.mainViewControler close:YES];
        }
        else{
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStory_iPad" bundle:nil];
            Setting = (SettingVC *)[storyboard instantiateViewControllerWithIdentifier:@"SettingVC"];
            self.mainViewControler = [[UINavigationController alloc] initWithRootViewController: Setting];
            [self.slideMenuController changeMainViewController:self.mainViewControler close:YES];
            
        }
    }
}

@end