//
//  HomeViewController.m
//  Demo
//
//  Created by super on 2019/7/5.
//  Copyright © 2019 super. All rights reserved.
//

#import "HomeViewController.h"
#import "RegisterViewController.h"
#import "EcgViewController.h"
#import "UserModel+CoreDataProperties.h"
#import "UserModel+CoreDataClass.h"
#import "MagicalRecord.h"
#import "RegisterViewController.h"
#import "UserArchive.h"
#import "XDTopHud.h"
@interface HomeViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *autoImage;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    self.navigationController.navigationBarHidden = YES;

    [super viewDidLoad];
    [self setUpUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [self fetchData];
}
- (void)setUpUI{
    self.textFieldName.layer.borderColor = [UIColor colorWithRed:271.0/255.0f green:248.0/255.0f blue:250.0f/255.0f alpha:1].CGColor;
     self.textFieldName.layer.borderWidth =1.0;
    self.textFieldName.layer.cornerRadius = 15;
    
    self.textFieldPassword.layer.borderColor = [UIColor colorWithRed:271.0/255.0f green:248.0/255.0f blue:250.0f/255.0f alpha:1].CGColor;
    self.textFieldPassword.layer.borderWidth =1.0;
    self.textFieldPassword.layer.cornerRadius = 15;
    
    self.buttonRegister.layer.cornerRadius = 20;
    self.buttonLog.layer.cornerRadius = 20;
    
    self.bottomView.layer.cornerRadius = 5;
    
    BOOL isAuto = [UserArchive aDecoderUser].isAutoLogin;
    if(isAuto)
    {
        [self.autoImage setImage:[UIImage imageNamed:@"ic-check-48px"]];
    }
    else
    {
        [self.autoImage setImage:[UIImage imageNamed:@"unauto"]];
    }
}

- (void)fetchData{
    NSArray * arr = [UserModel MR_findAll];
    if (arr.count>0) {
        UserModel * model = [arr lastObject];
        self.textFieldName.text = model.userName;
        self.textFieldPassword.text = model.userPassword;
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textFieldName resignFirstResponder];
    [self.textFieldPassword resignFirstResponder];
}

- (IBAction)registerAction:(id)sender {
    RegisterViewController* conttroller = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:conttroller animated:YES];
}

- (IBAction)autoLoginBtn:(id)sender {
    UserArchive* user = [UserArchive aDecoderUser];
    
    if(user.isAutoLogin)
    {
        user.isAutoLogin = NO;
        [UserArchive encodeUser:user];
         [self.autoImage setImage:[UIImage imageNamed:@"unauto"]];
    }
    else
    {
        user.isAutoLogin = YES;
        [UserArchive encodeUser:user];
        [self.autoImage setImage:[UIImage imageNamed:@"ic-check-48px"]];
    }
    
}

- (IBAction)login:(id)sender {
    
    //查找所有age属性为3的Person记录。
    NSArray *personsAgeEuqals3 = [UserModel MR_findByAttribute:@"userName" withValue:self.textFieldName.text];
    if(personsAgeEuqals3.count == 0)
    {
        [XDTopHud showWarning:@"输入的帐号不存在"];
        return;
    }
    UserModel* user = personsAgeEuqals3.firstObject;
    if([self.textFieldPassword.text isEqualToString:user.userPassword])
    {
        NSString* appId = CYAppid;
        //初始化SDK 设置用户信息 以保证心电数据精确性
        CYUserConfig* config = [[CYUserConfig alloc] init];
        config.height = user.userHeight.intValue;
        config.weight = user.userWeight.floatValue;
        config.phone = user.userPhone;
        config.age = user.userAge.intValue;
        if([user.userSex isEqualToString:@"男"])
        {
            config.sex = MAN;
        }
        else
        {
            config.sex = WOMAN;
        }
        
        //初始化SDK
        [CYEcgKit initWithAppID:appId userName:user.userName userConfig:config];
        EcgViewController* controller = [[EcgViewController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
        
         UserArchive* autoUser = [UserArchive aDecoderUser];
        if(autoUser.isAutoLogin)
        {
            autoUser.username = user.userName;
        }
        [UserArchive encodeUser:autoUser];
    }
    else
    {
        [XDTopHud showWarning:@"账号密码错误"];
    }

}

@end
