//
//  privacyModeViewController.m
//  隐私保护
//
//  Created by xd h on 2023/11/28.
//

#import "privacyModeViewController.h"
#import "connectDeviceManager.h"
#import "MineViewCell.h"
#import "ModifyPWDFirstViewController.h"
#import "ChangeLogoViewController.h"
#import "inputPWDViewController.h"

@interface privacyModeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView* headView;//
@property(nonatomic, strong) UISwitch *privacySwitch;
@property (nonatomic, strong, nullable) UITableView *tableView;
@end

@implementation privacyModeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:HWF5F7FAColor];
    [self.toolBar setHidden:YES];
    [self.navigationBar setHidden:YES];
    [self.navBarBGView setHidden:NO];
    [self.titleLabel setText:NSLocalizedString(@"my_set_Privacy_Model",nil)];
    
    [self drawAnyView];
}

- (void)drawAnyView{
    [self initHeadUIFun];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.top.equalTo(_headView.mas_bottom).offset(20.f);
    }];
}

- (void)initHeadUIFun
{
    _headView = [[UIView alloc] init];
    _headView.backgroundColor = [UIColor whiteColor];
    _headView.layer.cornerRadius = 8;
    [self.view addSubview:_headView];
    
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(108);
        make.top.equalTo(self.navBarBGView.mas_bottom).offset(16.f);
    }];
    
    UIImageView *topImageV = [[UIImageView alloc] init];
    topImageV.image = [UIImage imageNamed:@"icon_Privacy_model"];
    [_headView addSubview:topImageV];
    
    [topImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.width.mas_equalTo(28);
        make.height.mas_equalTo(28);
    }];
    
    UILabel *topLab = [[UILabel alloc] init];
    topLab.text = NSLocalizedString(@"my_set_Privacy_Model",nil);
    topLab.font = [UIFont boldSystemFontOfSize:14.0];
    topLab.textColor = [UIColor hwColor:@"#0A132B" alpha:1.0];
    [_headView addSubview:topLab];
    
    [topLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(topImageV.mas_right).offset(5);
        make.top.mas_equalTo(topImageV.mas_top).offset(0);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(28);
    }];
    
    _privacySwitch = [[UISwitch alloc] init];
    [_headView addSubview:_privacySwitch];
    [_privacySwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15.f);
        make.centerY.equalTo(topImageV.mas_centerY);
    }];
    [_privacySwitch setOnTintColor:HW13B2EBColor];
    [_privacySwitch addTarget:self action:@selector(maskSwitchPressed:) forControlEvents:(UIControlEventValueChanged)];

    [self setPrivacySwitchStateFun];
    
    UILabel *tipLab = [[UILabel alloc] init];
    tipLab.text = NSLocalizedString(@"my_set_Privacy_Model_tip",nil);
    tipLab.font = [UIFont systemFontOfSize:12.0];
    tipLab.textColor = [UIColor hwColor:@"#6A6A6A" alpha:1.0];
    tipLab.numberOfLines = 0;
    [_headView addSubview:tipLab];
    
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topImageV.mas_bottom).offset(10);
        make.left.mas_equalTo(topImageV.mas_left).offset(0);
        make.right.mas_equalTo(_privacySwitch.mas_right).offset(0);
    }];
}

#pragma mark - 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - TABBARHEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
//        _tableView.contentInset = UIEdgeInsetsMake(-H_STATE_BAR, 0, 0, 0);
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
        [_tableView setSeparatorColor:[UIColor clearColor]];
        [_tableView setBackgroundColor:[UIColor clearColor]];
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setBounces:YES];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        
    }
    
    return _tableView;
}

#pragma mark - 列表委托
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (MineViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    static NSString *identifier = @"MineViewCell";
    
    MineViewCell * cell =  [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell){
        cell = [[MineViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setAccessoryType:(UITableViewCellAccessoryNone)];
    }
    [cell.bgViewLayer removeFromSuperlayer];
    [cell.titleLabel2 setHidden:YES];
    [cell.rightImage setHidden:NO];
    [cell.lineView setHidden:NO];
    [cell.maskSwitch setHidden:YES];
    if (row == 0){
        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_modify_pwd",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"mdoify_pwd"]];
        
        /*上圆角*/
        //设置部分圆角  贝塞尔曲线
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_W-30 , 60)
                                                       byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                                             cornerRadii:CGSizeMake(8, 8)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = cell.cellBgView.bounds;
        maskLayer.path = maskPath.CGPath;
        cell.bgViewLayer = maskLayer;
        cell.cellBgView.layer.mask = cell.bgViewLayer;
    }else if (row == 1){
        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_change_login",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"icon_change_logo"]];
        //    }else if (row == 2){
        //        [cell.titleLabel setText:NSLocalizedString(@"mask_view_hiden_set_hiden",nil)];
        //        [cell.mImageView setImage:[UIImage imageNamed:@"icon-jincheng"]];
        //    }else if (row == 3){
    }else if (row == 2){
        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_fanzhuan_exit",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"fanzhuan"]];
        [cell.rightImage setHidden:YES];
        [cell.maskSwitch setHidden:NO];

        /*下圆角*/
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_W-30 , 60)
                                                       byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                                                             cornerRadii:CGSizeMake(8, 8)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = cell.cellBgView.bounds;
        maskLayer.path = maskPath.CGPath;
        cell.bgViewLayer = maskLayer;
        cell.cellBgView.layer.mask = cell.bgViewLayer;
        
        [cell.lineView setHidden:YES];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSInteger row = indexPath.row;
    if (0 == row){
        /*修改密码*/
        ModifyPWDFirstViewController *nextVC = [[ModifyPWDFirstViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }else if (1 == row){
        /*更换图标*/
        ChangeLogoViewController *nextVC = [[ChangeLogoViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
//    else if (1 == row){/*进城隐藏*/
//        HidenMaskSetViewController *nextVC = [[HidenMaskSetViewController alloc] init];
//        [self.navigationController pushViewController:nextVC animated:YES];
//    }
    else if (2 == row){/*设备翻转*/
        ;
    }
   
}

- (void)maskSwitchPressed:(UISwitch *)privacySwitch{
    [self setPrivacySwitchStateFun];
    
    inputPWDViewController *nextVC = [inputPWDViewController new];
    nextVC.isCheckPwdType = YES;
    [self.navigationController pushViewController:nextVC animated:YES];
    
    KWeakSelf
    nextVC.didInputRightPwdFun = ^{
        [weakSelf updatePrivacyModeFun];
    };
    
}

#pragma mark 设置tabview 是否隐藏
- (void)setPrivacySwitchStateFun{
    BOOL haveOpenMask = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.isPrivacyMode;
    [_privacySwitch setOn:haveOpenMask];
    
    if(haveOpenMask){
        self.tableView.hidden = NO;
    }
    else{
        self.tableView.hidden = YES;
        [self HandleEnterNotPrivacyModeFun];
    }
}

#pragma mark 处理进来是非隐私模式的情况
- (void)HandleEnterNotPrivacyModeFun
{
    NSInteger maskModel = [HWDataManager getIntegerWithKey:Const_Mask_View_Model];
    if (maskModel != 0){
        [HWDataManager setIntegerWithKey:Const_Mask_View_Model value:0];
        [iTools lc_setAlternateIconName:@"AppIcon1"];
    }
}

#pragma mark 联网设置隐私模式
- (void)updatePrivacyModeFun{
    
    [self showNewIndicatorWithCanBack:YES canTouch:NO];

    BOOL haveOpenMask = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.isPrivacyMode;
    NSString* curSn = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
    
    NSMutableDictionary *paraDict = [NSMutableDictionary dictionary];
    [paraDict setValue:curSn forKey:@"sn"];
    [paraDict setValue:[NSNumber numberWithBool:!haveOpenMask] forKey:@"isPrivacyMode"];
    
    KWeakSelf
    [[netWorkManager shareInstance] CommonPostCallBackCode:updatePrivacyMode Parameters:paraDict success:^(id  _Nonnull responseObject) {
        HLog(@"%@", responseObject);
      [self removeNewIndicator];

      SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];

       if (model.status == 0) {
           [connectDeviceManager shareInstance].DeviceThirdIdMod.data.isPrivacyMode = ![connectDeviceManager shareInstance].DeviceThirdIdMod.data.isPrivacyMode;
           
           [weakSelf setPrivacySwitchStateFun];
       }
       else
       {
           [[iToast makeText:model.msg] show];
       }

    } failure:^(NSError * _Nonnull error) {
       HLog(@"%@", error);
        [[iToast makeText:NSLocalizedString(@"player_link_fail_tip",nil)] show];
      [self removeNewIndicator];
    }];
}
@end
