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

#import "ICContactsBackupHeader.h"
#import "ICContactsTool.h"

@interface ICContactsBackupHeader ()

@property (strong, nonatomic) UISwitch *switchButton;


@end

@implementation ICContactsBackupHeader


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupSubviews];
    }
    
    return self;
}

-(void)setupSubviews
{
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"#E5E7EB"];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@30);
        make.left.right.equalTo(@0);
        make.height.equalTo(@0.5);
    }];
        
    UILabel *label = [[UILabel alloc] init];
    label.text = ICLocalized(@"AutomaticBackup");
    label.textColor = [UIColor colorWithHexString:@"#212121"];
    label.font = [UIFont systemFontOfSize:16];
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@16);
        make.height.equalTo(@44);
        make.top.equalTo(line.mas_bottom).offset(0);
    }];

    self.switchButton = [[UISwitch alloc] init];
    [self addSubview:self.switchButton];
    self.switchButton.on = NO;
    self.switchButton.onTintColor = kMassColor;
    [self.switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label);
        make.right.equalTo(@(-16));
    }];
    
    NSString *value = [[NSUserDefaults standardUserDefaults] objectForKey:@"auto_icloud_backup"];
    if ([value isEqualToString:@"auto_backup"]) {
        self.switchButton.on = YES;
    }
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor colorWithHexString:@"#E5E7EB"];
    [self addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(label.mas_bottom).offset(0);
        make.height.equalTo(@0.5);
    }];
    
    UILabel *subLabel = [[UILabel alloc] init];
    subLabel.text = ICLocalized(@"BackupDescription");
    subLabel.numberOfLines = 2;
    subLabel.textColor = [UIColor colorWithHexString:@"#858B9C"];
    subLabel.textAlignment = NSTextAlignmentCenter;
    subLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:subLabel];
    [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@30);
        make.top.equalTo(bottomLine.mas_bottom).offset(0);
    }];
    
}

- (void)switchAction:(UISwitch *)sender {
    
//    sender.on = !sender.on;
    if (sender.on) {
        NSLog(@"自动备份");
        [[NSUserDefaults standardUserDefaults] setObject:@"auto_backup" forKey:@"auto_icloud_backup"];
        ICContactsTool *tools = [ICContactsTool shareInstance];
        NSString *time = [tools getNowTimeTimestamp];
        [tools synciCloudContactsFileName:time];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"auto_icloud_backup"];
        NSLog(@"取消");
    }
}


@end
