//
//  DetailTabBarController.m
//  Apex Mobile
//
//  Created by Ray on 14-3-7.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//
 
#import "DetailTabBarController.h"

@interface DetailTabBarController ()

@end

@implementation DetailTabBarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}
-(id) init :(NSString*) function_name actions:(NSArray*)actions params:(NSMutableDictionary*)params
{
    self = [super init];
    if (self) {
         DebugLog(@"DetailTabBarController init.................");

        
        
//        UITabBar *tabBar = self.tabBar;
//        [tabBar setTintColor:[UIColor redColor]];
        
//        [tabBar setTintColor:[UIColor colorWithRed:0.0
//                                             green:176.0/255.0
//                                              blue:226.0/255.0
//                                             alpha:1.0]];
//        tabBar.selectedImageTintColor = [UIColor clearColor];
//        UITabBarItem *item = [tabBar.items objectAtIndex:0];
//        NSString *homePath = [[NSBundle mainBundle] pathForResource:@"btn_home_highlight" ofType:@"png"];
//        if(item.tag == 1)
//        {
//            [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
//                                          [UIColor lightGrayColor], UITextAttributeTextColor, nil]
//                                forState:UIControlStateNormal];
//            item.selectedImage = [UIImage imageWithContentsOfFile:homePath];
//        }
        
        self.actions = actions;
        self.function_name = function_name;
        self.params = params;
        
        
        NSString* title=@"";
        if([self.function_name isEqualToString:@"Ocean Booking"])
            title = @"Booking Detail";
        else if([self.function_name isEqualToString:@"Ocean B/L info."])
            title = @"B/L info. Detail";
        else if([self.function_name isEqualToString:@"Container detail"])
            title = @"Container Detail";
        else if([self.function_name isEqualToString:@"Download Document"])
            title = @"Document Detail";
        else if([self.function_name isEqualToString:@"Cargo Tracking"])
            title = @"Cargo Detail";
        
        self.navigationItem.title = title;
        
        NSMutableArray* controllers = [[NSMutableArray alloc]init];
        
        if(self.actions.count==1)
             self.tabBar.hidden = true;
        for(int i=0;i<self.actions.count;i++)
        {
            
            
            NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
            
            
            [params setValue:@"handset_search" forKey:@"action"];
            [params setValue:self.actions[i] forKey:@"action_type"];
            [params setValue:self.function_name forKey:@"module_name"];
            [params setValue:[self.params valueForKey:@"id"] forKey:@"id"];
            [params setValue:[self.params valueForKey:@"sessionid"] forKey:@"sessionid"];
            
            if([self.function_name isEqualToString:@"Cargo Tracking"])
            {

                if([[self.params valueForKey:@"criterion_type"] isEqualToString:@"0"])
                   [params setValue:[self.params valueForKey:@"cargo_criterion"] forKey:@"hbol"];
                else
                   [params setValue:[self.params valueForKey:@"cargo_criterion"] forKey:@"container_no"];
               
             
            }
            
//            testViewController * one = [[testViewController alloc] init];
            
            UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
            DetailPageViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"DetailPageViewController"];
            
            

//            DetailPageViewController *one = [[DetailPageViewController alloc]init];
            one.function_name = self.function_name;
            one.params = params;
            [one setTitle:self.actions[i]];
            one.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.actions[i] image:nil tag:i];//[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
            [one.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont systemFontOfSize:12.0], NSFontAttributeName, nil]
                                forState:UIControlStateNormal];
         //   one.tabBarItem set
//            [one.tabBarItem setSelectedImage:[UIImage imageNamed:@"tag-3.png"]];
            UIOffset offset;
            offset.horizontal = 0;
            offset.vertical = -15;
            [one.tabBarItem setTitlePositionAdjustment:offset];
//            one.tabBarItem set
            
            [controllers addObject:one];
        }
        
        self.viewControllers = controllers;
        UIImage* img = [UIImage imageNamed:@"test1.png"];
        UIImage * idimage=[img resizableImageWithCapInsets:UIEdgeInsetsMake(1.0f, 30.0f, 300.0f, 30.0f)];
        //tabBarController.tabBar.selectionIndicatorImage = idicImage;
//        [self.tabBar setTintColor:[UIColor redColor]];
//        [self.tabBar setItemSpacing:0];
//        [self.tabBar setBarTintColor:[UIColor greenColor]];
//        [self.tabBar setSelectedImageTintColor:[UIColor purpleColor]];
        self.tabBar.selectionIndicatorImage =idimage;
        
        
        //        NSMutableArray* controllers = [[NSMutableArray alloc]init];
        //
        //        for(int i=0;i<self.actions.count;i++)
        //        {
        //            DetailPageViewController *tab = [[DetailPageViewController alloc]init];
        //            [tab setTitle:self.actions[i]];
        //            tab.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:i];
        //            [controllers addObject:tab];
        //
        //        }
        //        self.viewControllers = controllers;
        //
        
        //        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"asdfsadf" image:[UIImage imageNamed:@"first.png"] tag:1];
        //        self.tabBarItem = item;
    }
    return self;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if(buttonIndex!=alertView.cancelButtonIndex)
    {
        UITextField * txt=[alertView textFieldAtIndex:0];
        NSString* name = txt.text;
        NSString * actions = [self.actions componentsJoinedByString:@","];
//        NSString* param = [self.params description];
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self.params
                                                           options:0
                                                             error:nil];
        NSString* param = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//        NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:self.params];
//        
//        NSString* param = [[NSString alloc] initWithData:myData
//                                                encoding:NSUTF8StringEncoding];
        ApexMobileAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        NSString* user = appDelegate.user;
        
        sqlite3 *db =[ApexMobileDB get_db];
        
        [ApexMobileDB execSql: [NSString stringWithFormat: @"insert into favorites(params,name,module_name,user,action) values('%@','%@','%@','%@','%@')",param,name,self.function_name,user,actions] db:db];
        
        sqlite3_close(db);
        
     
        
    }
    
    
}

-(void)saveDetail:(UIBarButtonItem*)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_create_tag", nil) message:NSLocalizedString(@"alert_msg_savehint", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"cancel", nil) otherButtonTitles:NSLocalizedString(@"save", nil),nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    UITextField * txt = [[UITextField alloc] init];
    
    txt.backgroundColor = [UIColor yellowColor];
    
    txt.frame = CGRectMake(alert.center.x+65,alert.center.y+48, 150,23);
    //        [alert addSubview:txt];
    [alert show];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"save", nil) style:UIBarButtonItemStylePlain target:self action:@selector(saveDetail:)];
//    saveButton.title = @"Save";
    
    self.navigationItem.rightBarButtonItem = saveButton;
    
    // self.navigationController.view.frame.size.height;
    
    //    NSMutableArray * items = [[NSMutableArray alloc]init];
    //    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] init];
    //    item1.title = @"item1";
    //    item1.image = [UIImage imageNamed:@"rect_setting"];
    //    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] init];
    //    item2.image = [UIImage imageNamed:@"rect_about"];
    //    item2.title = @"item2";
    //    [items addObject:item1];
    //    [items addObject:item2];
    //    self.navigationItem.rightBarButtonItems=items;
//    self.navigationItem.backBarButtonItem = backButton;


}

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

@end
