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

#import "ICContactsRepeatHeader.h"
#import "ICContactListModel.h"
#import <Contacts/Contacts.h>
#import "ICContactsTool.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height


@interface ICContactsRepeatHeader ()

@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIView *contView;
@property(nonatomic,strong)UIView *baseView;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *phoneLabel;

@end

@implementation ICContactsRepeatHeader

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.contView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, kWidth - 32, 120)];
        self.contView.backgroundColor = kMassColorAlpha(0.1);
        [self.contentView addSubview:self.contView];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.contView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(8, 8)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.contView.bounds;
        maskLayer.path = maskPath.CGPath;
        self.contView.layer.mask = maskLayer;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.contView addGestureRecognizer:tap];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.text = ICLocalized(@"MergedContact");
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.contView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@16);
        }];
        
         self.selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
         [self.selectBtn setTitle:ICLocalized(@"ChooseAll") forState:UIControlStateNormal];
        [self.selectBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [self.selectBtn setTitleColor:kMassColor forState:UIControlStateNormal];
        [self.contView addSubview:self.selectBtn];
        [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(-16));
            make.top.equalTo(@14);
        }];
         
         
        self.baseView = [[UIView alloc] init];
        self.baseView.backgroundColor = kMassColorAlpha(0.3);
        [self.contView addSubview:self.baseView];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@16);
            make.top.equalTo(@46);
            make.right.equalTo(@(-16));
            make.height.equalTo(@60);
        }];
        self.baseView.layer.cornerRadius = 8;
        
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.text = ICLocalized(@"Name");
        self.nameLabel.font = [UIFont systemFontOfSize:14];
        [self.baseView addSubview:self.nameLabel];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@12);
        }];
        
        self.phoneLabel = [[UILabel alloc] init];
        self.phoneLabel.text = ICLocalized(@"Phone");
        self.phoneLabel.font = [UIFont systemFontOfSize:14];
        [self.baseView addSubview:self.phoneLabel];
        [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@12);
            make.top.equalTo(self.nameLabel.mas_bottom).offset(3);
        }];
        
    }
    
    return self;
}

-(void)setContacts:(NSArray *)contacts
{
    _contacts = contacts;
    
    NSArray *array = contacts;
      NSString *name = @"";
      NSString *phone = @"";
      NSString *subPhone = @"";
      NSInteger idx = 0;
      [self.selectBtn setTitle:ICLocalized(@"CancelAll") forState:UIControlStateNormal];
      for (ICContactListModel *model in array) {
          subPhone = [subPhone stringByAppendingFormat:@"%@ ",model.contact.familyName];
          if (model.contact.phoneNumbers.count) {
              NSArray *phones = model.contact.phoneNumbers;
              CNLabeledValue *labeledValue = phones[0];
              CNPhoneNumber *phoneNumer = labeledValue.value;
              NSString *phoneNumber = phoneNumer.stringValue;
              phone = [NSString stringWithFormat:@"%@", phoneNumber];
          }
          if (model.choose) {
              idx ++;
              name = [name stringByAppendingFormat:@"%@ ",model.contact.familyName];
          }else{
              [self.selectBtn setTitle:ICLocalized(@"ChooseAll") forState:UIControlStateNormal];
          }
      }
      if (idx > 1) {
          [self updateName:name updatePhone:phone];
      }else{
          [self updateName:subPhone updatePhone:phone];
      }
      
}

-(void)updateName:(NSString *)name updatePhone:(NSString *)phone
{
    self.nameLabel.text = name;
    self.phoneLabel.text = phone;
}

-(void)tapAction
{
    NSLog(@"click");
    if (self.contacts.count == 0) return ;
    ICContactListModel *model = self.contacts[0];
    [ICContactsTool shareInstance].note = self.nameLabel.text;
    [[ICContactsTool shareInstance] enterContactsDetail:model.contact controller:[self getViewController] push:NO];
}

- (UIViewController *)getViewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}




@end
