//
//  OtherViewController.m
//  Demo
//
//  Created by ls on 2019/7/19.
//  Copyright © 2019 super. All rights reserved.
//

#import "OtherViewController.h"
#import "CYPlayBackViewController.h"
#ifdef THREESDK
#import "HomeViewController.h"
#endif
#import "UserArchive.h"
@interface OtherViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *lightSwitch;
@property (weak, nonatomic) IBOutlet UIButton *logoutBtn;

@end

@implementation OtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor]};
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    UIButton* backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 20, 20);
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    self.title = @"其他";
    
    self.logoutBtn.layer.cornerRadius = 20;
    self.logoutBtn.layer.masksToBounds = YES;
    
    if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"IDLETIMER"] isEqualToString:@"off"])
    {
        [self.lightSwitch setOn:YES];
    }
    else
    {
        [self.lightSwitch setOn:NO];
    }
    // Do any additional setup after loading the view from its nib.
}

-(void)backAction
{
    //停止记录
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)lastReportAction:(id)sender {
    
#ifdef THREESDK
    CYLastFileInfo* lastInfo = [CYEcgKit sharedInstance].lastFileInfo;
#else
    CYLastFileInfo* lastInfo = [CYBaseKit sharedInstance].lastFileInfo;
#endif
    if(lastInfo.filePath.length <= 0)
    {
        [XDTopHud showWarning:@"暂无心电记录"];
        return;
    }
    
    CYPlayBackViewController* controller = [[CYPlayBackViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)loginOutAction:(id)sender {
    
    if(self.isRecording)
    {
        [XDTopHud showWarning:@"当前正在记录，请稍后退出"];
        return;
    }
    #ifdef THREESDK
    [UserArchive encodeUser:nil];
    //断开设备
    [[CYEcgKit sharedInstance] cancelPeripheral];
    [[CYEcgKit sharedInstance] endRedcord];
    HomeViewController* controller = [[HomeViewController alloc] init];
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
#endif
}

//设置屏幕是否常亮
- (IBAction)switchAction:(id)sender {
    UISwitch* lightSwitch = (UISwitch*)sender;
    
    if(lightSwitch.isOn)
    {
         [[NSUserDefaults standardUserDefaults] setObject:@"on" forKey:@"IDLETIMER"];
//        [UIApplication sharedApplication].idleTimerDisabled = YES;
    }
    else
    {
         [[NSUserDefaults standardUserDefaults] setObject:@"off" forKey:@"IDLETIMER"];
//        [UIApplication sharedApplication].idleTimerDisabled = NO;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
