//
//  RDSInputWiFiVC.m
//  Temperature
//
//  Created by RD on 2022/11/21.
//

#import "RDSInputWiFiVC.h"
#import "NSDictionary+WiFiInfo.h"
#import "RDSBindDeviceVC.h"
#import <CoreLocation/CoreLocation.h>

@interface RDSInputWiFiVC ()<UITextFieldDelegate,CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *wifiAccountTF;
@property (weak, nonatomic) IBOutlet UITextField *wifiPwdTF;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation RDSInputWiFiVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.locationManager startUpdatingLocation];
    
    [self p_initUI];
    
    // APP由后台进入
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(p_reloadWiFi)name:UIApplicationWillEnterForegroundNotification object:nil];
}


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

- (void) p_reloadWiFi{
    
    if(![self.wifiAccountTF.text isEqualToString:[NSDictionary rds_fetchSSIDInfo][@"SSID"]]){
        self.wifiAccountTF.text = [NSDictionary rds_fetchSSIDInfo][@"SSID"];
        self.wifiPwdTF.text = [kUserDefaults objectForKey:self.wifiAccountTF.text];
    }
    
}

#pragma mark - UI
- (void)p_initUI {
    
    self.title = @"选择设备工作Wi-Fi";
    self.wifiAccountTF.delegate = self;
    self.wifiPwdTF.delegate = self;
    
}

- (IBAction)onChangeWifiClick {
    
    // 跳转系统设置
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}

- (IBAction)onNextBtnClick:(UIButton *)sender {
    
    if ([NSDictionary rds_fetchSSIDInfo][@"SSID"] == nil) {
        [RDSHudShower showCenterToast:@"请把手机连接到WiFi"];
        return;
    }
    if (self.wifiAccountTF.text.length < 1) {
        [RDSHudShower showCenterToast:@"请输入WiFi信息"];
        return;
    }
    
    if (self.wifiPwdTF.text.length < 1) {
        [RDSHudShower showCenterToast:@"请输入WiFi信息"];
        return;
    }
    
    [self.view endEditing:YES];
    
    [kUserDefaults setObject:self.wifiPwdTF.text forKey:self.wifiAccountTF.text];
    
    RDSBindDeviceVC *vc = [[RDSBindDeviceVC alloc] init];
    vc.roomName = _roomName;
    vc.deviceID = _deviceID;
    vc.wifiName = self.wifiAccountTF.text;
    vc.pwd = self.wifiPwdTF.text;
    vc.deviceTypeModel = self.deviceTypeModel;
    [self pushViewController:vc animated:YES];
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    [self p_reloadWiFi];
}

-(CLLocationManager*)locationManager{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = kCLLocationAccuracyThreeKilometers;
        [_locationManager requestWhenInUseAuthorization];
    }
    return _locationManager;
}


@end
