//
//  ICContactsBackupSubController.m
//  superCleaner
//
//  Created by GY.Z on 2020/7/8.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICContactsBackupSubController.h"
#import <ContactsUI/ContactsUI.h>
#import "BMChineseSort.h"
#import "ICContactsListCell.h"
#import "ICContactListModel.h"

static NSString * const ICContactsListSubCellID = @"ICContactsListSubCellID";

@interface ICContactsBackupSubController ()<UITableViewDataSource, UITableViewDelegate, CNContactViewControllerDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation ICContactsBackupSubController

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"备份";
    self.tableView = [[UITableView alloc] initWithFrame: self.view.bounds style: UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerNib:[UINib nibWithNibName:@"ICContactsListCell" bundle:nil] forCellReuseIdentifier:ICContactsListSubCellID];
    
    for (CNContact *contact in self.contacts) {
        ICContactListModel *model = [ICContactListModel new];
        model.contact = contact;
        [self.dataArray addObject:model];
    }
    
    NSLog(@"%@",self.contacts);
    
    [self.tableView reloadData];
}

//section行数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//每组section个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

//返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICContactsListCell *cell = [tableView dequeueReusableCellWithIdentifier:ICContactsListSubCellID];

    if (self.contacts.count) {
        ICContactListModel *model = self.dataArray[indexPath.row];
        cell.model = model;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    ICContactListModel *model = self.dataArray[indexPath.row];

    //1.创建Contact对象，必须是可变的
    CNContactViewController *contactController = [CNContactViewController viewControllerForContact:model.contact];
    contactController.delegate = self;
    /// 是否显示打电话、发短信等按钮
    contactController.allowsActions = YES;
    /// 能否修改数据
    contactController.allowsEditing = YES;
    /// 是否显示联系人的关联联系人
    contactController.shouldShowLinkedContacts = true;
    [contactController viewDidLoad];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: contactController];
    UIButton *cancelBtn = [UIButton buttonWithType: UIButtonTypeSystem];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [cancelBtn setTitle:@"取消" forState: UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(dismissContactController) forControlEvents:UIControlEventTouchUpInside];
    contactController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: cancelBtn];
    nav.modalPresentationStyle =  UIModalPresentationFormSheet;
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)dismissContactController {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}




@end
