//
//  playerShowSecretkeyView.m
//  隐私保护
//
//  Created by xd h on 2023/11/6.
//

#import "playerShowSecretkeyView.h"
#import "RSATool.h"
#import "connectDeviceManager.h"

@interface playerShowSecretkeyView()

@property(nonatomic,strong)UIView *whiteBgView;
@property(nonatomic,strong)UIButton *closeBut;
@property(nonatomic,strong)NSTimer *curTimer;
@property(nonatomic,assign) NSInteger coundDownNumber;
@end

@implementation playerShowSecretkeyView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor hwColor:@"000000" alpha:0.6];
    [self drawAnyView];
    
    return self;
}

- (void)drawAnyView{
    _whiteBgView = [[UIView alloc] init];
    _whiteBgView.layer.cornerRadius = 8;
    _whiteBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_whiteBgView];
    
    [_whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(260);
        make.width.mas_equalTo(300.f);
        make.center.mas_equalTo(0.f);
    }];
    
    
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.text = NSLocalizedString(@"player_Secret_key_title",nil);
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont boldSystemFontOfSize:18];
    [_whiteBgView addSubview:titleLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.top.mas_equalTo(30.f);
    }];
    
    
    UILabel *tipLab = [[UILabel alloc] init];
    tipLab.text = NSLocalizedString(@"player_Secret_key_tip",nil);
    tipLab.textAlignment = NSTextAlignmentCenter;
    tipLab.font = [UIFont systemFontOfSize:14];
    tipLab.numberOfLines = 0;
    tipLab.textColor = [UIColor hwColor:@"#FF5353" alpha:1.0];
    [_whiteBgView addSubview:tipLab];
    
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(35);
        make.left.mas_equalTo(10.f);
        make.right.mas_equalTo(-0.f);
        make.top.mas_equalTo(titleLab.mas_bottom).offset(5.f);
    }];
    
    
    
    UIView*grayBgView = [[UIView alloc] init];
    grayBgView.layer.cornerRadius = 2;
    grayBgView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
    [_whiteBgView addSubview:grayBgView];
    
    [grayBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(56);
        make.left.mas_equalTo(30.f);
        make.right.mas_equalTo(-30.f);
        make.top.mas_equalTo(tipLab.mas_bottom).offset(20.f);
    }];
    
    [grayBgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(copyKeyFun)]];
    
    
    NSString *snStr = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
    NSString *secretkey  = [RSATool sha256_8:snStr];

    UILabel *secretKeyLab = [[UILabel alloc] init];
    secretKeyLab.text = secretkey;
    //secretKeyLab.textAlignment = NSTextAlignmentCenter;
    secretKeyLab.font = [UIFont boldSystemFontOfSize:16];
    secretKeyLab.textColor = [UIColor hwColor:@"#3B7FFF" alpha:1.0];
    [grayBgView addSubview:secretKeyLab];

    [secretKeyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0.f);
        make.left.mas_equalTo(10.f);
        make.right.mas_equalTo(0.f);
        make.top.mas_equalTo(0.f);
    }];
    
    
    NSString*curStr = NSLocalizedString(@"player_Secret_key_copy",nil);
    
    UILabel *rightLab = [[UILabel alloc] init];
    rightLab.text = [[NSString alloc] initWithFormat:@"|   %@",curStr];
    //@"|  复制密钥";
    rightLab.textAlignment = NSTextAlignmentRight;
    rightLab.font = [UIFont systemFontOfSize:16];
    rightLab.textColor = [UIColor hwColor:@"#666666" alpha:1.0];
    [grayBgView addSubview:rightLab];

    [rightLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0.f);
        make.left.mas_equalTo(10.f);
        make.right.mas_equalTo(-10.f);
        make.top.mas_equalTo(0.f);
    }];
    
    
    _closeBut = [[UIButton alloc] init];
    
    
    NSString *butTitle = [[NSString alloc] initWithFormat:@"%@ %@",NSLocalizedString(@"player_Secret_key_close",nil), @"(10s)"];
    /*确认按钮*/
    _closeBut = [[UIButton alloc] init];
    CGFloat okBtn_W= 240;
    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,okBtn_W,40);
    gl.startPoint = CGPointMake(0, 0.5);
    gl.endPoint = CGPointMake(1, 0.5);
    gl.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
    gl.locations = @[@(0), @(1.0f)];
    _closeBut.layer.cornerRadius = 8;
    _closeBut.clipsToBounds = YES;
    [_closeBut.layer addSublayer:gl];
    [_closeBut setTitle:butTitle forState:(UIControlStateNormal)];
    [_closeBut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_closeBut addTarget:self action:@selector(closeViewFun) forControlEvents:UIControlEventTouchUpInside];
    
//    NSString* curLanguage = [DDYLanguageTool ddy_AppLanguage];
//    if(!curLanguage || curLanguage.length == 0){
//        curLanguage = [DDYLanguageTool ddy_SystemLanguage];
//    }
//
//    // "zh-Hant-HK", "zh-Hans-US",
//    if([curLanguage rangeOfString:@"zh-Han"].location != NSNotFound){//中文和繁体
//        [okBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
//    }
//    else{//en-US
//        [okBtn.titleLabel setFont:[UIFont systemFontOfSize:14.f]];
//    }
   
    [_whiteBgView addSubview:_closeBut];
    [_closeBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-30.f);
        make.centerX.mas_equalTo(0.f);
        make.width.mas_equalTo(okBtn_W);
        make.height.mas_equalTo(40);
    }];
    
    [_closeBut setEnabled:NO];
    [_closeBut setAlpha:0.5];
    
    [_curTimer invalidate];
    _curTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerHandleFun) userInfo:nil repeats:YES];
    
    _coundDownNumber = 10;
}

#pragma mark  timer fun
- (void)timerHandleFun
{
    _coundDownNumber -- ;
    if(_coundDownNumber > 0){
        NSString *butTitle = [[NSString alloc] initWithFormat:@"%@ (%lds)",NSLocalizedString(@"player_Secret_key_close",nil), _coundDownNumber];
        [_closeBut setTitle:butTitle forState:(UIControlStateNormal)];
    }
    else{
        NSString *butTitle  = NSLocalizedString(@"player_Secret_key_close",nil);
        [_closeBut setTitle:butTitle forState:(UIControlStateNormal)];
        
        [_closeBut setEnabled:YES];
        [_closeBut setAlpha:1];
    }
    
}

#pragma mark 复制
- (void)copyKeyFun
{
    NSString *snStr = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
    NSString *secretkey  = [RSATool sha256_8:snStr];
    
    UIPasteboard * Pasteboard = [UIPasteboard generalPasteboard];
    
    Pasteboard.string = secretkey;
    
    NSString *tipStr  = NSLocalizedString(@"player_Secret_key_copy_suc",nil);
    [[iToast makeText:tipStr] show];
    
}

#pragma mark 关闭按钮
- (void)closeViewFun
{
    [self removeFromSuperview];
  
    if(_didCloseSecretKeyViewTip){
        _didCloseSecretKeyViewTip();
    }
}

@end
