//
//  PlayerViewController+otherDelegate.m
//  隐私保护
//
//  Created by xd h on 2023/10/13.
//

#import "PlayerViewController+otherDelegate.h"
#import "RCCommandHelp.h"
#import "couldPhoneBaseInfoModel.h"
#import "cachesFileManager.h"
#import "couldphoneSysInfoModel.h"
#import "TvStatusModel.h"
#import "HWVersionModel.h"
#import <MJExtension.h>
#import "HaveNewVersionView.h"
#import "connectDeviceManager.h"

@implementation PlayerViewController (otherDelegate)

#pragma mark   ComontAlretViewControllerDelegate
- (void)CommonAlertokBtnClickPressed{
    //HLog("11111");
    
    self.isLoginAgainType = YES;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:lockBypwdNotification object:nil];
}
- (void)CommonAlertCancelBtnClickPressed{
    HLog("强制退出app");
    
    exit(0);/*强制退出app*/
}

- (void)checkVersionFun
{
    [self getVersion];
}

#pragma mark- 网络请求
- (void)getVersion {
    
    NSMutableDictionary *paraDict = [NSMutableDictionary dictionary];
    [paraDict setValue:@"ios" forKey:@"type"];
    [paraDict setValue:@"skyworth" forKey:@"source"];
    
    [[netWorkManager shareInstance] CommonGetWithCallBackCode:upgradeNewVersion Parameters:paraDict success:^(id  _Nonnull responseObject) {
        //HLog(@"%@", responseObject);

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

       if (model.status == 0) {

           HWVersionModel *model = [HWVersionModel mj_objectWithKeyValues:responseObject[@"data"]];
           [self checkVersion:model];
       }
       else
       {
           //[[iToast makeText:model.msg] show];
       }

    } failure:^(NSError * _Nonnull error) {
      HLog(@"%@", error);
    }];
}

- (void)checkVersion:(HWVersionModel*)versionModel {
//  NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    NSString *newVersion = [versionModel.versionNumber stringByReplacingOccurrencesOfString:@"v" withString:@""];  //去掉v
//    HLog(@"%@---%@", newVersion, currentVersion);
    if ([currentVersion compare:newVersion options:NSNumericSearch] == NSOrderedSame) { // 本地版本号 == 服务器版本号
    }else if ([currentVersion compare:newVersion options:NSNumericSearch] == NSOrderedDescending) { // 本地版本号 > 服务器版本号 （异常情况）
        [HWDataManager setBoolWithKey:Const_haveVersion value:NO];
    }else { // 本地版本号 < 服务器版本号 (有新版本)
        [self showNewVersion:versionModel];
        [HWDataManager setBoolWithKey:Const_haveVersion value:YES];
    };
}

#pragma mark 需要强制重启
- (void)updateForceStartFun{
    
    
    NSString* curSn = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
    
    NSMutableDictionary *paraDict = [NSMutableDictionary dictionary];
    [paraDict setValue:curSn forKey:@"sn"];
    [paraDict setValue:[NSNumber numberWithBool:YES] forKey:@"isForceStart"];
    
    [[netWorkManager shareInstance] CommonPostCallBackCode:updateForceStart Parameters:paraDict success:^(id  _Nonnull responseObject) {
        
      SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];

       if (model.status == 0) {
          
       }
       else
       {
           
       }

    } failure:^(NSError * _Nonnull error) {
       HLog(@"%@", error);
    }];
}

- (void)showNewVersion:(HWVersionModel*)versionModel {
    
    HaveNewVersionView *haveNewVersionView = [HaveNewVersionView shardInstance];
    if (haveNewVersionView.isShow) {
        return;
    }
    haveNewVersionView.versionModel = versionModel;
    haveNewVersionView.isContinueCheckAlert = NO; // 是否继续走下一步弹窗检测流程
    [haveNewVersionView setBackgroundColor:RGBACOLOR(0, 0, 0, 0.5)];
    [[iTools getKeyWindow] addSubview:haveNewVersionView];
    haveNewVersionView.isShow = YES;
    
    if(!versionModel.necessary){
        haveNewVersionView.deleteBtn.hidden = NO;
    }
    
    [haveNewVersionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    haveNewVersionView.alpha = 0.0;

    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
                haveNewVersionView.alpha = 1.0;
                KyoLog(@"in animate start");
            } completion:^(BOOL finished) {
                KyoLog(@"in animate completion");
    }];
}


- (void)getCouldPhoneBaseInfoResponseFun:(NSDictionary *)dataDict
{
    couldPhoneBaseInfoModel *model = [[couldPhoneBaseInfoModel alloc] initWithDictionary:dataDict error:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneBaseInfoNotification object:model];
}

- (void)getCouldPhoneSysInfoResponseFun:(NSDictionary *)dataDict{
    couldphoneSysInfoModel *model = [[couldphoneSysInfoModel alloc] initWithDictionary:dataDict error:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneSysInfoNotification object:model];
}

- (void)getCouldPhoneTvStatusResponseFun:(NSDictionary *)dataDict
{
    TvStatusModel *model = [[TvStatusModel alloc] initWithDictionary:dataDict error:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneTvStatusNotification object:model];
}

#pragma mark 监听文件开始的的通知
- (void)beginGotoUploadFileFun:(NSNotification *)notification
{
    self.isSuspendUploadType = NO;
    
    uploadFileDataModel *uploadFileDataMod = [notification object];
    //HLog(@"%@",uploadFileDataMod);
    self.curUploadFileDataMod = uploadFileDataMod;
    
    if(![uploadFileDataMod isKindOfClass:[uploadFileDataMod class]]){
        HLog(@"上传的文件类型不对");
        return;
    }
    
    [self ApplyUploadFileFun];
}

- (void)applyUploadFileServiceResponseFun:(NSDictionary *)dataDict
{
    if([dataDict isKindOfClass:[NSDictionary class]]){
        if ([[dataDict allKeys] containsObject:@"data"]) {
            NSDictionary *data = dataDict[@"data"];

            if ([[data allKeys] containsObject:@"status"]) {
                NSString  *status = [data objectForKey:@"status"];
                if(![status isKindOfClass:[NSString class]]){
                    status = [[NSString  alloc] initWithFormat:@"%@",status];
                }
                
                //if(1)
                if([status isEqualToString:@"1"])
                {
                    //[self tryAgainApplyUploadFun];
                    [self clearUploadFileDataFun];
                    [[uploadFileManager shareInstance] uploadFileFailFun];
                    
                }else if ([status isEqualToString:@"0"]){
                    if ([[data allKeys] containsObject:@"taskUid"]) {
                        NSString *taskUid = data[@"taskUid"];
                        if(![status isKindOfClass:[NSString class]]){
                            taskUid = [[NSString  alloc] initWithFormat:@"%@",taskUid];
                        }
                        
                        if(taskUid &&  [taskUid isEqualToString:self.taskUid]){
                            
                            NSNumber * fileLengthNum = @0;
                            if ([[data allKeys] containsObject:@"fileLength"])
                            {
                                fileLengthNum = data[@"fileLength"];
                            }
                            
                        //    [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:[fileLengthNum integerValue] withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
                        //
                        //    }];
                            
                            if([fileLengthNum integerValue] == self.curUploadFileDataMod.totalBytes){
                                self.taskUid = nil;
                                
                                NSMutableArray *delArr = [NSMutableArray new];
                                [delArr addObject:self.curUploadFileDataMod];
                                [[uploadFileManager shareInstance] deleteUploadFileRecordBy:delArr complete:^(BOOL isSuccess) {
                                    if (isSuccess) {
                                        [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileDeteleNotification object:self.curUploadFileDataMod];
                                    }
                                }];
                                
                                mainBlock(^{
                                    [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
                                });
                                
                                HLog(@"hxd already 上传任务异常终止")
                                return;
                            }
                            
                            if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeImage){
                                [self beginGotoUploadImageFun:fileLengthNum];
                            }
                            else{
                                [self beginGotoUploadVidelFun:fileLengthNum];
                            }
                        }
                    }
                   
                }
            }
        }
        else{
            //异常
            [self tryAgainApplyUploadFun];
        }
    }
    else{
        //异常
        [self tryAgainApplyUploadFun];
    }
}


#pragma mark 图片上传
-(void)beginGotoUploadImageFun:(NSNumber*)fileLengthNum{
    
    self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
    
    if(!self.cutFileDataArr){
        [self clearUploadFileDataFun];
        [[uploadFileManager shareInstance] uploadFileFailFun];
        
        self.taskUid = nil;
        
        mainBlock(^{
            [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
        });
        HLog(@"hxd fail 上传任务异常终止")
        return;
    }
    
    //答复的taskUid 跟申请的一致 开发发送内容
    [self upLoadFileFun];
}


#pragma mark 视频上传
//每次从文件重新切一片 传一片
-(void)beginGotoUploadVidelFun:(NSNumber*)fileLengthNum{
    
    NSData *data = [self cutVideoFileFunAtIndex:fileLengthNum.longLongValue];
    HLog(@"data leng:%ld",[data length]);

    self.AllFileData = data;
    self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:0];
    self.indexOfUploadFlie = 0;
    HLog(@"data leng:%ld",[data length]);
    
    //return;

//    NSString*videoPathStr = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo];
//    self.AllFileData = [NSData dataWithContentsOfFile:videoPathStr];

   // self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
    
    if(!self.cutFileDataArr){
        [self clearUploadFileDataFun];
        [[uploadFileManager shareInstance] uploadFileFailFun];
        
        self.taskUid = nil;
        
        mainBlock(^{
            [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
        });
        HLog(@"hxd fail 上传任务异常终止")
        return;
    }
    
    //答复的taskUid 跟申请的一致 开发发送内容
    [self upLoadFileFun];
}

#pragma mark 分段读视频文件
-(NSData*)cutVideoFileFunAtIndex:(NSUInteger)dataIndex{
    NSString *filePath = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo]; // 文件路径
    
    NSFileManager *manager0 = [NSFileManager defaultManager];
    if(![manager0 fileExistsAtPath:filePath]) {
     
        return [NSData new];
    }
    
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; // 创建文件句柄
      
    // 设置分段读取的大小，这里以每次读取1KB为例
    //const NSUInteger chunkSize = 50 * 1024 *1024;
    const NSUInteger chunkSize = 50 * 1024 *1024;
    NSMutableData *data = [NSMutableData data];
    
    if (fileHandle) {
       
        long long endOfFile = [fileHandle seekToEndOfFile];
        if (endOfFile >=  chunkSize) {
            
            // 读取文件的分段数据到某个位置
            [fileHandle seekToFileOffset:dataIndex];
            
            // 读取文件的分段数据
            NSData* chunk = [fileHandle readDataOfLength:chunkSize];
            if (chunk) {
                [data appendData:chunk];
            }
        }
        else{
            // 读取文件的分段数据到某个位置
            [fileHandle seekToFileOffset:dataIndex];
            
            [data appendData:[fileHandle readDataToEndOfFile]];
        }
        
        // 在这里可以对文件内容进行处理
        // ...
        
        // 关闭文件句柄
        [fileHandle closeFile];
    }
    
    return data;
}


- (void)upLoadFileFunServiceResponseFun:(NSDictionary *)dataDict
{
    //{"type":"uploadFileRandomRet","data":{"progress":"9\/10","filePath":"/sdcard/Download/IMG_0036.JPG","status":"0","msg":"success"}}
    
    if([dataDict isKindOfClass:[NSDictionary class]]){
        if ([[dataDict allKeys] containsObject:@"data"]) {
            NSDictionary *data = dataDict[@"data"];

            if ([[data allKeys] containsObject:@"status"]) {
                NSString  *status = [data objectForKey:@"status"];
                if(![status isKindOfClass:[NSString class]]){
                    status = [[NSString  alloc] initWithFormat:@"%@",status];
                }
                
                if ([status isEqualToString:@"1"]) {
                    //[self tryAgainApplyUploadFun];
                    [self clearUploadFileDataFun];
                    [[uploadFileManager shareInstance] uploadFileFailFun];
                    
                }else if ([status isEqualToString:@"0"]){
                    
                    NSNumber * fileLengthNum = @0;
                    if ([[data allKeys] containsObject:@"fileLenth"])
                    {
                        fileLengthNum = data[@"fileLenth"];
                    }
                    
                    [self handleUploadingFun:fileLengthNum];
                }
            }
        }
        else{
            //异常
            [self tryAgainApplyUploadFun];
        }
    }
    else{
        //异常
        [self tryAgainApplyUploadFun];
    }
    
}

#pragma mark 申请上传文件
- (void)ApplyUploadFileFun
{
    if(!self.taskUid){
        self.taskUid = [iTools getTaskUidStr];
        self.fileName = self.curUploadFileDataMod.filename;
        if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
            //self.AllFileData = self.curUploadFileDataMod.videoData;
        }
        else{
            self.AllFileData = self.curUploadFileDataMod.imageData;
        }
        
        self.indexOfUploadFlie = 0;
        
        //HLog(@"%@  %@",self.taskUid,self.fileName);
        //self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData];
    }
    
    NSString * commandStr = [RCCommandHelp applyForUploadFileBy:self.taskUid  filePath:self.fileName];
    [self.commandChannelManager rc_sendData:commandStr];
}

#pragma mark 遇到异常时 重新上传
- (void)tryAgainApplyUploadFun
{
    self.taskUid = nil;
    [self ApplyUploadFileFun];
}

#pragma mark 处理上传过程
- (void)handleUploadingFun:(NSNumber*)fileLengthNum
{
    if(self.isSuspendUploadType){
        //暂停
        self.taskUid = nil;
        NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
        [[uploadFileManager shareInstance] changeUploadFileState:uploadStateSuspend withDidUploadBytes:didUploadSize  withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {

        }];
        
        return;
    }
    
    if(self.indexOfUploadFlie < self.cutFileDataArr.count -1) {
        
        //NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
        NSInteger didUploadSize = fileLengthNum.longLongValue;
        
        [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {

        }];
        
        self.indexOfUploadFlie ++;
        [self upLoadFileFun];
    }
    else if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo && fileLengthNum && fileLengthNum.longLongValue < self.curUploadFileDataMod.totalBytes){
        //视频做了二次切割 不一定上传完了
        HLog(@"视频上传传完一片 再来一片");
        [self beginGotoUploadVidelFun:fileLengthNum];
    }
    else{
        HLog(@"上传完成");
        
        [self clearUploadFileDataFun];
        [[uploadFileManager shareInstance] uploadFileDoneFun];
    }
}

-(void)clearUploadFileDataFun{
    //上传数据清空
    self.taskUid = nil;
    self.fileName = nil;
    self.AllFileData = nil;
    self.indexOfUploadFlie = 0;
    self.cutFileDataArr = nil;
    self.curUploadFileDataMod = nil;
    self.isSuspendUploadType = NO;
    
}

- (Byte)getBCC:(NSData *)data {
    Byte bcc = 0;
    unsigned char *bytes = (unsigned char *)[data bytes];
    for (int i = 0; i < [data length]; i++) {
        bcc = (Byte)(bcc ^ bytes[i]);
    }
    return bcc;
}


#pragma mark 文件切片
-(NSMutableArray *)fileCutPartsBy:(NSData*)curData withDidUpLoadSize:(NSInteger)didLoadSize{
 
    NSData * data = curData;
    
    NSUInteger allLength = data.length;
    
    NSUInteger subs = 5*1024*1024;//4096;//要切片的大小，我这里设置的是4096字节
    //NSUInteger subs = 31*1024;//4096;//要切片的大小，内存没有暴增
    
    self.cutFileEachPieceSzie = subs;
    
    NSInteger index = 0;//起始位置
    
    if(didLoadSize != 0){
        if (didLoadSize % subs != 0){
            //出错了
            return nil;
        }
        else{
//            index = didLoadSize/subs;
//            allLength = allLength - didLoadSize;
            
            self.indexOfUploadFlie = didLoadSize/subs;
        }
    }

    NSMutableArray *dataArray =[NSMutableArray new];
    
    do {
        @autoreleasepool {
            if (allLength>subs) {
                
                NSRange range =NSMakeRange(index*subs, subs);
                
                index++;
                
                //NSLog(@"%@",NSStringFromRange(range));
                
                [dataArray addObject:[data subdataWithRange:range]];
                
                allLength = allLength - subs;
                
            }else{
                NSRange range = NSMakeRange(index*subs, allLength);
                
                //NSLog(@"%@",NSStringFromRange(range));
                
                [dataArray addObject:[data subdataWithRange:range]];
                
                allLength = 0;
            }
        }
        
    } while (allLength>0);
    
    HLog(@"dataArray leng:%ld",dataArray.count);//最后得到切片的结果，数组里面是NSData对象

    return dataArray;
}

// int转NSData
- (NSData *)int2Data:(int)i{
    Byte b0 = i & 0xff;
    Byte b1 = i >> 8 & 0xff;
    Byte b2 = i >> 16 & 0xff;
    Byte b3 = i >> 24 & 0xff;
    // 有大小端模式问题？
    //Byte result[] = {b0, b1, b2, b3};
    Byte result[] = {b3, b2, b1, b0};
    return [NSData dataWithBytes:result length:sizeof(result)];
}

// short转NSData
- (NSData *)short2Data:(short)i{
    Byte b0 = i & 0xff;
    Byte b1 = i >> 8 & 0xff;
    // 有大小端模式问题？
    //Byte result[] = {b0, b1, b2, b3};
    Byte result[] = { b1, b0};
    return [NSData dataWithBytes:result length:sizeof(result)];
}

#pragma mark 可以发送文件了
/*
文件数据字节流byte[]
  格式1:下载日志文件压缩包
       |head|msgType|taskId|    datalen            | curNum | totalNum|   data   |bcc校验|end|
  长度：  1      1      6 （curNum+totalNum+data）        4         4      datalen- 8   1     1
       0x68  0x0e                                                        0x16
    bcc校验从第1位 也就是从msgType起 到 校验位前（不包含校验位）数据作bcc校验
 */
- (void)upLoadFileFun
{
    //HLog(@"上传文件%ld/%ld",self.indexOfUploadFlie +1,self.cutFileDataArr.count);
    //data
    NSData *curData = self.cutFileDataArr[self.indexOfUploadFlie];
    //HLog(@"headData:%@",curData);
    
    //head
    unsigned char headNum = 104;
    NSData *headData = [NSMutableData dataWithBytes:&headNum length:sizeof(headNum)];
    //HLog(@"headData:%@",headData);
    
    //msgType
    //unsigned char msgType = 12;
    unsigned char msgType = 15;
    NSData *msgTypeData = [NSMutableData dataWithBytes:&msgType length:sizeof(msgType)];
    //HLog(@"msgTypeData:%@",msgTypeData);
    
    //taskId
    NSData *taskIdData = [self.taskUid dataUsingEncoding:NSUTF8StringEncoding];
    //HLog(@"taskIdData:%@",taskIdData);
    
    //datalen
    int datalenNum = int([curData length] +(4+4));
    //NSData *datalenData = [NSData dataWithBytes:&datalenNum length:sizeof(datalenNum)];
    NSData *datalenData = [self int2Data:datalenNum];
    //HLog(@"datalenData %@",datalenData);
    
    
    //curNum
    int curNum = (int)self.indexOfUploadFlie;
    //NSData *curNumData = [NSData dataWithBytes:&curNum length:sizeof(curNum)];
    NSData * curNumData = [self int2Data:curNum];
    //HLog(@"curNumData:%@",curNumData);
    
    //totalNum
    int totalNum = (int)self.cutFileDataArr.count;
    //NSData *totalNumData = [NSData dataWithBytes:&totalNum length:sizeof(totalNum)];
    NSData * totalNumData = [self int2Data:totalNum];
    //HLog(@"totalNumData:%@",totalNumData);
    

    NSMutableData *checkBCCData = [NSMutableData new];
    [checkBCCData appendData:msgTypeData];
    [checkBCCData appendData:taskIdData];
    [checkBCCData appendData:datalenData];
    [checkBCCData appendData:curNumData];
    [checkBCCData appendData:totalNumData];
    [checkBCCData appendData:curData];
    
    long length = [curData length];
    //HLog(@"curData length %d",length);
    
    Byte bccCheck = [self getBCC:checkBCCData];
    //HLog(@"bcc 000 %d",bccCheck);
    
    //bcc校验
    char bccNum = (char)bccCheck;
    NSData *bccData = [NSMutableData dataWithBytes:&bccNum length:sizeof(bccNum)];
    //HLog(@"%@",bccData);
    
    //end
    unsigned char endNum = 22;
    NSData *endData = [NSMutableData dataWithBytes:&endNum length:sizeof(endNum)];
    //HLog(@"%@",endData);
    
    NSMutableData *sendData = [NSMutableData new];
    [sendData appendData:headData];
    [sendData appendData:checkBCCData];
    [sendData appendData:bccData];
    [sendData appendData:endData];
    
    //HLog(@"sendData length %ld",[sendData length]);
    [self.commandChannelManager rc_sendData:sendData];
    //HLog(@"文件上传sendData %@",sendData);
}

- (void)suspendUploadFileFun:(NSNotification *)notification
{
    self.isSuspendUploadType = YES;
}

@end
