//
//  MySetViewController.m
//  双子星云手机
//
//  Created by APPLE on 2023/8/17.
//

#import "MySetViewController.h"
#import <SDWebImage.h>
#import <Masonry.h>
#import "HelpDownScrollView.h"
#import "MineViewCell.h"
#import "ChangeLogoViewController.h"
#import "HidenMaskSetViewController.h"
#import "CustomerWebViewController.h"
#import "AboutViewController.h"
#import "iPhone.h"
#import "CloudPhoneAPI.h"
#import "ComontAlretViewController.h"
#import "ModifyPWDFirstViewController.h"
#import "TipsQRCodeForChangeDeviceViewController.h"
#import "connectDeviceManager.h"
#import "mySetHeadView.h"
#import "uploadImageOrVideoViewController.h"
#import "TZImagePickerController.h"
#import "uploadFileRecordViewController.h"
#import "privacyModeViewController.h"
#import "TvStatusModel.h"

@interface MySetViewController ()<HelpDownScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,ComontAlretViewControllerDelegate>
{
    UILabel *phoneLabel;
    UILabel *vipTypeTipsLabel;
    UIButton *tvP2PBtn;
}
@property (nonatomic, strong, nullable) UITableView *tableView;

@end

@implementation MySetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCouldPhoneTvStatusFun:)  name:getCouldPhoneTvStatusNotification  object:nil];
    
    [self drawAnyView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if(_getTvStatus){
        _getTvStatus();
    }
}

- (void)drawAnyView{
    [self.view setBackgroundColor:HWF5F7FAColor];
    
    [self.toolBar setHidden:YES];
    [self.navigationBar setHidden:YES];
    [self.navBarBGView setHidden:NO];
    [self.titleLabel setText:NSLocalizedString(@"more_set",nil)];
    
    [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(self.navBarBGView.mas_bottom).offset(5.f);
    }];
}



- (UIView *)tableHeadView{
    UIView *bgView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, SCREEN_W, 108.f + 15))];
    [bgView setBackgroundColor:[UIColor clearColor]];
    
    mySetHeadView *headView = [[mySetHeadView alloc] init];
    [bgView addSubview:headView];
    headView.layer.cornerRadius = 8;
    headView.layer.masksToBounds = YES;
    
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-15);
    }];
    
#pragma mark 上传文件相关处理
    headView.didClickBut = ^(NSInteger tag) {
        
        if(tag ==2 || tag ==3){
            
            if(self->_getBaseInfo){
                self->_getBaseInfo();
            }
            
            uploadImageOrVideoViewController *vc = [uploadImageOrVideoViewController new];
            if(tag ==2){
                vc.isPhotoType = YES;
            }
            else{
                vc.isPhotoType = NO;
            }
            [self.navigationController pushViewController:vc animated:YES];
            
        }
        else if(tag == 1){
            uploadFileRecordViewController *vc = [uploadFileRecordViewController new];
            vc.isNotUploadingType = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    };
    
    return bgView;
}

- (UIView *)tableFoorView{
    UIView *bgView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, SCREEN_W, 68.f))];
    [bgView setBackgroundColor:[UIColor clearColor]];
    
    tvP2PBtn = [[UIButton alloc] initWithFrame:(CGRectMake(15, 20, SCREEN_W - 2*15.f, 48.f))];
    [tvP2PBtn setBackgroundColor:HWFFFFFFColor];
    [tvP2PBtn setTitle:NSLocalizedString(@"my_set_no_TV_p2p",nil) forState:(UIControlStateNormal)];
    [tvP2PBtn setTitle:NSLocalizedString(@"my_set_no_close_TV_p2p",nil) forState:(UIControlStateSelected)];
    [tvP2PBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
    [tvP2PBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:14.f]];
    [tvP2PBtn addTarget:self
                 action:@selector(tvP2PBtnPressed)
       forControlEvents:(UIControlEventTouchUpInside)];
    [tvP2PBtn.layer setCornerRadius:8.f];
    [bgView addSubview:tvP2PBtn];
    return bgView;
}

#pragma mark 点击投屏
- (void)tvP2PBtnPressed{
    
    if(tvP2PBtn.selected)
    {
        [self showCloseTVP2PFun];
    }
    else{
        [self showTVP2PFun];
    }
}

#pragma mark 显示投屏提示语
- (void)showTVP2PFun{
    /*弹窗提示TV投屏*/
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"my_set_TVP2P_Open_title",nil)
                                                                                     msg:NSLocalizedString(@"my_set_TVP2P_Open_Subtitle",nil)
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"my_set_TVP2P_Open_sure",nil)
                                                                                 okTitle:nil isOkBtnHighlight:NO
                                                                              didClickOk:^{
                                                                                  HLog(@"111");
    } didClickCancel:^{
        //HLog(@"2222");
        [self gotoSetTVP2PFun];
    }];
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];
}

#pragma mark 确认开始TV投屏
-(void)gotoSetTVP2PFun
{
    if(_needToTVP2P){
        _needToTVP2P();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 显示关闭投屏提示语
- (void)showCloseTVP2PFun{
    /*弹窗提示TV投屏*/
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"my_set_no_close_TV_p2p_tip",nil)
                                                                                     msg:nil
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"other_cancel",nil)
                                                                                 okTitle:NSLocalizedString(@"my_set_TVP2P_Open_sure",nil) isOkBtnHighlight:YES
                                                                              didClickOk:^{
        [self gotoCloseTVP2PFun];
    } didClickCancel:^{
        //HLog(@"2222");
        
    }];
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];
    
    
}

#pragma mark 确认开始TV投屏
-(void)gotoCloseTVP2PFun
{
    if(_closeTVP2P){
        _closeTVP2P();
        tvP2PBtn.selected = NO;
    }
    
    tvP2PBtn.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self->tvP2PBtn.userInteractionEnabled = YES;
    });
}


- (void)helpDownScrollViewBePressed:(nonnull id)sender withIndex:(NSInteger)index {
    HLog(@"\n----点击了第个%ld按钮----",index);
    if (index == 0){
        ChangeLogoViewController *nextVC = [[ChangeLogoViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }else if (index == 1){/**/
        HidenMaskSetViewController *nextVC = [[HidenMaskSetViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
}

#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;
        }
        
        [_tableView setTableFooterView:[self tableFoorView]];
        [_tableView setTableHeaderView:[self tableHeadView]];
    }
    
    return _tableView;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
     //return 5;//去掉更换icon
}

- (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_Privacy_Model",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"icon_Privacy_model"]];
        
//        [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_logo",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 == 1){
//        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_fanzhuan_exit",nil)];
//        [cell.mImageView setImage:[UIImage imageNamed:@"fanzhuan"]];
//        [cell.rightImage setHidden:YES];
//        [cell.maskSwitch setHidden:NO];
    //}else if (row == 4){/*版本信息*/
    }else if (row == 1){/*版本信息*/
        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_check_update",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"app_update"]];
        [cell.titleLabel2 setText:[iPhone appVersion]];
        [cell.titleLabel2 setHidden:NO];
    //}else if (row == 5){/*更换设备*/
    }else if (row == 2){/*更换设备*/
        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_change_phone",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"icon_change_ohone"]];
    //}else if (row == 6){
    }else if (row == 3){/*常见问题*/
        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_common_problem",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"icon_common_problem"]];
    }else if (row == 4){/*回复出厂*/
        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_Restore_Factory",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"icon_Restore_Factory"]];
    }else if (row == 5){/*重启空间*/
        [cell.titleLabel setText:NSLocalizedString(@"my_set_no_restart_phone",nil)];
        [cell.mImageView setImage:[UIImage imageNamed:@"icon-gengxin"]];
        /*下圆角*/
        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){
        
        privacyModeViewController *nextVC = [[privacyModeViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
        
        /*修改密码*/
//        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 (3 == row){/*设备翻转*/
//        ;
    }
    //else if (4 == row){/*版本信息*/
    else if (1 == row){/*版本信息*/
        AboutViewController *vc = [[AboutViewController alloc] init];
        vc.getSysInfo = ^{
            if(self->_getSysInfo){
                self->_getSysInfo();
            }
        };
        [self.navigationController pushViewController:vc animated:YES];
    //}else if (5 == row){/*更换设备*/
    }else if (2 == row){/*更换设备*/
        TipsQRCodeForChangeDeviceViewController *vc = [[TipsQRCodeForChangeDeviceViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (3 == row){/*常见问题*/
        CustomerWebViewController *vc = [[CustomerWebViewController alloc] init];
        vc.titleStr = NSLocalizedString(@"my_set_no_common_problem",nil);
        NSString *url = @"https://cliys.armclouding.com/h5/microserviceUserH5/commonProblem/questionIndex.html";
        vc.webUrl = url;
        [self.navigationController pushViewController:vc animated:YES];
    
    }else if (4 == row){/*恢复出厂*/
        [self RestoreFactoryAleartFun];
    }else if (5 == row){/*重启空间*/
        /*弹窗提示重启*/
        ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"my_set_no_restart_phone_tips",nil)
                                                                                         msg:nil
                                                                                    imageStr:nil
                                                                                 cancelTitle:NSLocalizedString(@"other_cancel",nil)
                                                                                     okTitle:NSLocalizedString(@"my_set_no_restart_phone_btn_ok",nil) isOkBtnHighlight:NO
                                                                                  didClickOk:^{
            
        } didClickCancel:^{
            
        }];
        nextVC.modalPresentationStyle = UIModalPresentationCustom;
        nextVC.delegate = self;
        [self presentViewController:nextVC animated:YES completion:^{
            nextVC.view.superview.backgroundColor = [UIColor clearColor];
        }];
    }
}

- (void)CommonAlertokBtnClickPressed{
//    /*重启云手机*/
//  通过指令通道发送 {"type":"reboot"}
    if(_needToReboot){
        _needToReboot();
    }
    
    //提示语
    [[iToast makeText:NSLocalizedString(@"player_link_rebooting_Tips",nil)] show];
}

#pragma mark 点击恢复出厂
- (void)RestoreFactoryAleartFun
{
    KWeakSelf
    /*弹窗提示恢复出厂*/
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"my_set_no_Restore_Factory_tips",nil)
                                                                                     msg:NSLocalizedString(@"my_set_no_Restore_Factory_msg",nil)
                                                                                imageStr:@"icon_Restore_Factory_big"
                                                                             cancelTitle:NSLocalizedString(@"my_set_TVP2P_Open_sure",nil)
                                                                                 okTitle:NSLocalizedString(@"other_cancel",nil) isOkBtnHighlight:YES
                                                                              didClickOk:^{
        
    } didClickCancel:^{
        //点击确定
        [weakSelf gotoResetFun];
    }];
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
    
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];
}

#pragma mark 点击了恢复出厂
- (void)gotoResetFun
{
    if(_needToReset){
        _needToReset();
    }
    
    [self RestoreFactoryingFun];
}

#pragma mark 恢复出厂中
- (void)RestoreFactoryingFun
{
    KWeakSelf
    /*弹窗提示恢复出厂*/
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"my_set_no_Restore_Factorying_tips",nil)
                                                                                     msg:NSLocalizedString(@"my_set_no_Restore_Factorying_msg",nil)
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"my_set_no_Restore_Factorying",nil)
                                                                                 okTitle:nil
                                                                        isOkBtnHighlight:NO
                                                                              didClickOk:^{
        [weakSelf RestoreFactoryCompleteFun];
    } didClickCancel:^{
    
    }];
    
    [nextVC setButtonCountdownFun:180];//90
    
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
    
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];
}

#pragma mark 恢复出厂倒计时结束
- (void)RestoreFactoryCompleteFun
{
    KWeakSelf
    //瑞云发起重连
    NSString *snStr = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
    if(snStr){
        [[connectDeviceManager shareInstance] getThridMsgBySN:snStr needReconnect:YES didNetEnd:^(bool didSuc) {
            if(didSuc){
                if(self->_needToResetAndOk){
                    self->_needToResetAndOk();
                }
            }
            else{
                [weakSelf RestoreFactoryCompleteFun];
                return;
            }
        }];
    }
    
    
    /*弹窗提示恢复出厂*/
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"my_set_no_Restore_Factory_ok_tips",nil)
                                                                                     msg:NSLocalizedString(@"my_set_no_Restore_Factory_ok_msg",nil)
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"my_set_no_Restore_FactoryOK",nil)
                                                                                 okTitle:nil
                                                                        isOkBtnHighlight:NO
                                                                              didClickOk:^{
        
    } didClickCancel:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    
    
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
    
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];
}


- (void)getCouldPhoneTvStatusFun:(NSNotification*)not
{
    TvStatusModel *model = [not object];
    //HLog(@"%@",baseInfoModel);
    
    if(!model || ![model isKindOfClass:[TvStatusModel class]]){
        return;
    }
    
    if([model.msg containsString:@"PushStreamBActivity"]){
        tvP2PBtn.selected = YES;
    }
    else{
        tvP2PBtn.selected = NO;
    }
    
}
@end
