//
//  YingDaoViewController.m
//  VclustersGemini
//
//  Created by APPLE on 2020/5/6.
//  Copyright © 2020 APPLE. All rights reserved.
//

#import "SetUsePWDViewController.h"
#import "PwdInPutView.h"
#import "SceneDelegate.h"
#import "HWWebViewController.h"

@interface SetUsePWDViewController ()<PwdInPutViewDelegate>
@property (strong, nonatomic) UIButton *okBtn;
@property (strong, nonatomic) PwdInPutView *pwdInPutView;
@end

@implementation SetUsePWDViewController
@synthesize delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self drawAnyView];
}

- (void)drawAnyView
{
    UIImageView *bgView = [[UIImageView alloc] init];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    [bgView setImage:[UIImage imageNamed:@"set_pwd_bj"]];
    
    /*提示文字*/
    UILabel *upLabel = [[UILabel alloc] init];
    [upLabel setText:NSLocalizedString(@"set_launch_pwd_tips",nil)];
    [upLabel setTextColor:HWFFFFFFColor];
    [upLabel setFont:[UIFont systemFontOfSize:16]];
    [upLabel setTextAlignment:(NSTextAlignmentCenter)];
    [self.view addSubview:upLabel];
    [upLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(22);
        make.top.mas_equalTo((SCREEN_H/812.f)*419.f);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
    /*密码输入框*/
    _pwdInPutView = [[PwdInPutView alloc] init];
    _pwdInPutView.delegate = self;
    [self.view addSubview:_pwdInPutView];
    [_pwdInPutView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(48);
        make.top.equalTo(upLabel.mas_bottom).offset(25.f);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(237.f);
    }];
    
    _okBtn = [[UIButton alloc] init];

    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,160,48);
    gl.startPoint = CGPointMake(0, 0.5);
    gl.endPoint = CGPointMake(0.97, 0.5);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:12/255.0 green:222/255.0 blue:253/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:5/255.0 green:141/255.0 blue:251/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
    _okBtn.layer.cornerRadius = 24;
    _okBtn.clipsToBounds = YES;
    [_okBtn.layer addSublayer:gl];
    [_okBtn setTitle:NSLocalizedString(@"other_confirm",nil) forState:(UIControlStateNormal)];
    [_okBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_okBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
    [self.view addSubview:_okBtn];
    [_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_pwdInPutView.mas_bottom).offset(50.f);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(48);
    }];
    
    [_okBtn addTarget:self
               action:@selector(okBtnPressed)
     forControlEvents:(UIControlEventTouchUpInside)];
    [_okBtn setEnabled:NO];
    [_okBtn setAlpha:0.5];
}

- (void)okBtnPressed{
    HLog(@"\nokBtnPressed------");
    /*记录密码进入登录注册界面*/
    NSString *launchAppPWDPath = [self documentPathForAccount:@"" fileFolder:@"LaunchPWD"];
    [_pwdInPutView.pwd writeToFile:launchAppPWDPath atomically:YES];
    
    if ([delegate respondsToSelector:@selector(setPwdOk)]){
        [delegate setPwdOk];
    }
}

- (void)enterMainVC {
    
//    [AppDelegate sharedAppDelegate].window.rootViewController = nav;
}

- (void)pwdInPutViewPWDIsOk:(BOOL)isOk{
    if (isOk){
        [_okBtn setEnabled:YES];
        [_okBtn setAlpha:1];
    }else{
        [_okBtn setEnabled:NO];
        [_okBtn setAlpha:0.5];
    }
}
- (NSString *)documentPathForAccount:(NSString *)account fileFolder:(NSString *)fileFolder {
    
    NSString *path = DocumentPath;
    if (account.length != 0) {
        path = [NSString stringWithFormat:@"%@/%@/",DocumentPath,account];
    }
        
    if ([fileFolder containsString:@"/"]) {
        NSString *path1 = [path stringByAppendingPathComponent:fileFolder];
        path1 = [path1 stringByDeletingLastPathComponent];
        NSError *error;
        if (![[NSFileManager defaultManager] fileExistsAtPath:path1]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:&error];
        }
    }else{
        NSError *error;
        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        }
    }
    
    return [path stringByAppendingPathComponent:fileFolder];
}

@end
