//
//  LocationDetailViewController.m
//  Apex Mobile
//
//  Created by Ray on 14-3-18.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//

#import "LocationDetailViewController.h"

@interface LocationDetailViewController ()

@end

@implementation LocationDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (BOOL)shouldAutorotate
{
    return YES;
}
//- (void)viewWillAppear:(BOOL)animated
//{
//    
//    [self.navigationController setNavigationBarHidden:false];
//}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (IBAction)addUnknown:(UIButton *)sender {
    
    
    
    //    NSString* area = [self.params valueForKey:@"area"];
    NSString* company = [self.params valueForKey:@"company"];
    NSString* city = [self.params valueForKey:@"city"];
    //    double longitude = [[self.params valueForKey:@"longitude"] doubleValue];
    //    double latitude = [[self.params valueForKey:@"latitude"] doubleValue];
    NSString* address = [self.params valueForKeyPath:@"address"];
    NSString* telephone = [self.params valueForKeyPath:@"telephone"];
    NSString* fax = [self.params valueForKeyPath:@"fax"];
    //    NSString* contact = [self.params valueForKeyPath:@"contact"];
    NSString* email = [self.params valueForKeyPath:@"email"];
    
    
    
    
    
    
    
    CFStringRef cfscompany = CFStringCreateWithCString( kCFAllocatorDefault, [company UTF8String], kCFStringEncodingUTF8);
    //    CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
    //    CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
    //    CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
    //    CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
    
    CFErrorRef error =NULL;
    ABRecordRef personRef=ABPersonCreate();
    
    
    NSData *dataRef = UIImagePNGRepresentation([UIImage imageNamed:@"apex512"]);
    ABPersonSetImageData(personRef, CFBridgingRetain(dataRef), &error);
    //    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    
    ABRecordSetValue(personRef, kABPersonOrganizationProperty, cfscompany, &error);
    ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue,@"http://www.apexshipping.com", kABPersonHomePageLabel, NULL);
    ABRecordSetValue(personRef, kABPersonURLProperty, urlMultiValue, &error);
    
    
    ABMutableMultiValueRef multiaddress=ABMultiValueCreateMutable(kABMultiStringPropertyType);
    //电话号码属于具有多个值的项（除此还有email、地址类）
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
    [addressDictionary setObject:address forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary setObject:city forKey:(NSString *)kABPersonAddressCityKey];
    //    [addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
    //    [addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];
    ABMultiValueAddValueAndLabel(multiaddress, CFBridgingRetain(addressDictionary), kABWorkLabel, NULL);
    
    
    // CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
    //        ABMultiValueAddValueAndLabel(multiaddress, cfsaddress, kABWorkLabel, NULL);
    ABRecordSetValue(personRef, kABPersonAddressProperty, multiaddress, &error);
    
    
    ABMutableMultiValueRef multiemail=ABMultiValueCreateMutable(kABMultiStringPropertyType);
    //电话号码属于具有多个值的项（除此还有email、地址类）
    NSArray* arremail = [email componentsSeparatedByString:NSLocalizedString(@"\r\n", nil)];
    for(int i=0;i<arremail.count;i++)
    {
        ABMultiValueAddValueAndLabel(multiemail, CFStringCreateWithCString( kCFAllocatorDefault, [arremail[i] UTF8String], kCFStringEncodingUTF8), kABPersonPhoneMainLabel, NULL);
    }
    ABRecordSetValue(personRef, kABPersonEmailProperty, multiemail, &error);
    
    ABMutableMultiValueRef multiphone=ABMultiValueCreateMutable(kABMultiStringPropertyType);
    //电话号码属于具有多个值的项（除此还有email、地址类）
    NSArray* arrphone = [telephone componentsSeparatedByString:NSLocalizedString(@"\r\n", nil)];
    for(int i=0;i<arrphone.count;i++)
    {
        ABMultiValueAddValueAndLabel(multiphone, CFStringCreateWithCString( kCFAllocatorDefault, [arrphone[i] UTF8String], kCFStringEncodingUTF8), kABPersonPhoneMainLabel, NULL);
    }
    
    NSArray* arrfax = [fax componentsSeparatedByString:NSLocalizedString(@"\r\n", nil)];
    for(int i=0;i<arrfax.count;i++)
    {
        ABMultiValueAddValueAndLabel(multiphone, CFStringCreateWithCString( kCFAllocatorDefault, [arrfax[i] UTF8String], kCFStringEncodingUTF8), kABPersonPhoneWorkFAXLabel, NULL);
    }
    ABRecordSetValue(personRef, kABPersonPhoneProperty, multiphone, &error);
    
    
    
    ABUnknownPersonViewController *unknown=[[ABUnknownPersonViewController alloc]init];
    unknown.displayedPerson = personRef;
    unknown.unknownPersonViewDelegate = self;
    unknown.allowsActions = true;
    
    ABAddressBookRef addressBook = ABAddressBookCreate();
//    CFStringRef cfName = CFSTR("小明");
    NSArray *people = (__bridge NSArray *)(ABAddressBookCopyPeopleWithName(addressBook, cfscompany));
    
   if(people.count>0)
       unknown.allowsAddingToAddressBook = false;
//    unknown.displayedPerson=person;
//    unknown.allowsAddingToAddressBook=YES;//允许添加
    
//    ABNewPersonViewController *npvc = [[ABNewPersonViewController alloc] init] ;
//    //  npvc.navigationItem.leftBarButtonItem = BARBUTTON(@"取消", @selector(addNewBackAction:));
//    //  self.aBNewPersonNav = [[UINavigationController alloc] initWithRootViewController:npvc] ;
//    npvc.displayedPerson = personRef;
//    npvc.newPersonViewDelegate = self;
    // [self presentModalViewController:self.aBNewPersonNav animated:YES];
    
    [self.navigationController pushViewController:unknown animated:YES];
    
    
    
}
- (IBAction)addContact:(UIButton *)sender {
    
    
    
    //    NSString* area = [self.params valueForKey:@"area"];
    NSString* company = [self.params valueForKey:@"company"];
        NSString* city = [self.params valueForKey:@"city"];
    //    double longitude = [[self.params valueForKey:@"longitude"] doubleValue];
    //    double latitude = [[self.params valueForKey:@"latitude"] doubleValue];
    NSString* address = [self.params valueForKeyPath:@"address"];
    NSString* telephone = [self.params valueForKeyPath:@"telephone"];
    NSString* fax = [self.params valueForKeyPath:@"fax"];
//    NSString* contact = [self.params valueForKeyPath:@"contact"];
    NSString* email = [self.params valueForKeyPath:@"email"];
    
   
    

    
    
    
    CFStringRef cfscompany = CFStringCreateWithCString( kCFAllocatorDefault, [company UTF8String], kCFStringEncodingUTF8);
//    CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
//    CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
//    CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
//    CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
    
    CFErrorRef error =NULL;
    ABRecordRef personRef=ABPersonCreate();

    
    NSData *dataRef = UIImagePNGRepresentation([UIImage imageNamed:@"apex512"]);
    ABPersonSetImageData(personRef, CFBridgingRetain(dataRef), &error);
//    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    
    ABRecordSetValue(personRef, kABPersonOrganizationProperty, cfscompany, &error);
    ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue,@"http://www.apexshipping.com", kABPersonHomePageLabel, NULL);
    ABRecordSetValue(personRef, kABPersonURLProperty, urlMultiValue, &error);
    
    
    ABMutableMultiValueRef multiaddress=ABMultiValueCreateMutable(kABMultiStringPropertyType);
    //电话号码属于具有多个值的项（除此还有email、地址类）
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
    [addressDictionary setObject:address forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary setObject:city forKey:(NSString *)kABPersonAddressCityKey];
//    [addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
//    [addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];
    ABMultiValueAddValueAndLabel(multiaddress, CFBridgingRetain(addressDictionary), kABWorkLabel, NULL);
    
    
// CFStringRef cfsaddress = CFStringCreateWithCString( kCFAllocatorDefault, [address UTF8String], kCFStringEncodingUTF8);
//        ABMultiValueAddValueAndLabel(multiaddress, cfsaddress, kABWorkLabel, NULL);
    ABRecordSetValue(personRef, kABPersonAddressProperty, multiaddress, &error);
    
    
    ABMutableMultiValueRef multiemail=ABMultiValueCreateMutable(kABMultiStringPropertyType);
    //电话号码属于具有多个值的项（除此还有email、地址类）
    NSArray* arremail = [email componentsSeparatedByString:NSLocalizedString(@"\r\n", nil)];
    for(int i=0;i<arremail.count;i++)
    {
        ABMultiValueAddValueAndLabel(multiemail, CFStringCreateWithCString( kCFAllocatorDefault, [arremail[i] UTF8String], kCFStringEncodingUTF8), kABPersonPhoneMainLabel, NULL);
    }
    ABRecordSetValue(personRef, kABPersonEmailProperty, multiemail, &error);
    
    ABMutableMultiValueRef multiphone=ABMultiValueCreateMutable(kABMultiStringPropertyType);
    //电话号码属于具有多个值的项（除此还有email、地址类）
    NSArray* arrphone = [telephone componentsSeparatedByString:NSLocalizedString(@"\r\n", nil)];
    for(int i=0;i<arrphone.count;i++)
    {
    ABMultiValueAddValueAndLabel(multiphone, CFStringCreateWithCString( kCFAllocatorDefault, [arrphone[i] UTF8String], kCFStringEncodingUTF8), kABPersonPhoneMainLabel, NULL);
    }
    
    NSArray* arrfax = [fax componentsSeparatedByString:NSLocalizedString(@"\r\n", nil)];
    for(int i=0;i<arrfax.count;i++)
    {
        ABMultiValueAddValueAndLabel(multiphone, CFStringCreateWithCString( kCFAllocatorDefault, [arrfax[i] UTF8String], kCFStringEncodingUTF8), kABPersonPhoneWorkFAXLabel, NULL);
    }
    ABRecordSetValue(personRef, kABPersonPhoneProperty, multiphone, &error);
    
    
    
    
    
    ABNewPersonViewController *npvc = [[ABNewPersonViewController alloc] init] ;
  //  npvc.navigationItem.leftBarButtonItem = BARBUTTON(@"取消", @selector(addNewBackAction:));
  //  self.aBNewPersonNav = [[UINavigationController alloc] initWithRootViewController:npvc] ;
    npvc.displayedPerson = personRef;
    npvc.newPersonViewDelegate = self;
   // [self presentModalViewController:self.aBNewPersonNav animated:YES];
    
    [self.navigationController pushViewController:npvc animated:YES];
    
    
   
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(addUnknown:)];
//    addButton.title = @"Add Contact";
    
    self.navigationItem.rightBarButtonItem = addButton;
    
    
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
//    closeButton.title = @"Close";
    
    self.navigationItem.leftBarButtonItem = closeButton;
    
    self.navigationItem.title =[self.params valueForKey:@"company"];
    
    
 //   NSString* sss=[self.params valueForKey:@"telephone"];
  //  self.nameText.text = [self.params valueForKey:@"company"];
    
    NSString* content = [NSString stringWithFormat:@"%@\n\nAddress\n%@\n\nTel\n%@\n\nFax\n%@\n\nContact\n%@\n\nEmail\n%@",[self.params valueForKey:@"company"],[self.params valueForKey:@"address"],[self.params valueForKey:@"telephone"],[self.params valueForKey:@"fax"],[self.params valueForKey:@"contact"],[self.params valueForKey:@"email"]];
    self.contentText.text = content;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onCloseClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:^{
        ;
    }];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark -ABNewPersonViewControllerDelegate
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
    [self.navigationController popViewControllerAnimated:true];
    
//    [self.aBNewPersonNav dismissViewControllerAnimated:true completion:^{
//        ;
//    }];
    //    newPersonViewController.navigationController navigationItem
}
@end
