//
//  RCOperateManager.m
//  Demo
//
//  Created by 余衡武 on 2024/10/7.
//

#import "RCOperateManager.h"
#import "RCWebSocket.h"

@interface RCOperateManager ()

/**
 *
 *  sdk版本号
 */
@property (nonatomic, strong, readwrite) NSString      *sdkVersion;


@end

@implementation RCOperateManager

#pragma mark - 单例
static RCOperateManager *shareInstance = nil;
+ (RCOperateManager *)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[RCOperateManager alloc] init];
        shareInstance.overtime = 1;
        shareInstance.reconnectCount = 5;
        shareInstance.timeout = 30;
    });
    return shareInstance;
}

// sdk版本号
- (NSString *)sdkVersion {
    NSString *appVersion = @"1.27.001";
    NSLog(@"SDK Version: %@", appVersion);
    return appVersion;
}


#pragma mark - 批量操作
/// 同步操作-设置分辨率和DPI指令
- (void)resolutionWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens width:(NSString *)width height:(NSString *)height dpi:(NSString *)dpi progress:(RCProgress)progress complete:(RCComplete)complete {

    NSString *order = [RCWSOrder dpiOrder:dpi]; // 先发送dpi指令
    NSString *order2 = [RCWSOrder resolutionOrder:width height:height]; // 再发送分辨率指令
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendSyscOrder:order order2:order2 toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 全局root控制指令
- (void)rootSystemWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens open:(BOOL)isOpen progress:(RCProgress)progress complete:(RCComplete)complete {
    int open = isOpen ? 1 : 0;
    NSString *order = [RCWSOrder rootSystemOrder:open];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 剪贴板指令
- (void)clipboardWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens text:(NSString *)text progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder clipboardOrder:text];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 虚拟定位指令
- (void)virtualLocationWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens longitude:(NSString *)longitude latitude:(NSString *)latitude progress:(RCProgress)progress complete:(RCComplete)complete {
 
    NSString *order = [RCWSOrder virtualLocationOrder:longitude latitude:latitude];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 真机键盘开关指令
- (void)setKeyoardModeWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens mode:(int)mode progress:(RCProgress)progress complete:(RCComplete)complete {
 
    NSString *order = [RCWSOrder setKeyoardModeOrder:mode];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 真机键盘输入指令
- (void)setKeyoardInfoWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens data:(NSString *)data position:(int)position progress:(RCProgress)progress complete:(RCComplete)complete {
 
    NSString *order = [RCWSOrder setKeyoardInfoOrder:data position:position];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

#pragma mark - 应用管理指令
/// 启动指定的应用指令
- (void)startAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageName:(NSString *)packageName progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder startApp:packageName];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 停止指定的应用指令
- (void)stopAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageName:(NSString *)packageName progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder stopApp:packageName];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 卸载指定的应用指令
- (void)uninstallAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageName:(NSString *)packageName progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder uninstallApp:packageName];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 隐藏预置的应用指令
- (void)hideAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageNames:(NSArray *)packageNames progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder hideApps:packageNames];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 显示预置的应用指令
- (void)showAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageNames:(NSArray *)packageNames progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder showApps:packageNames];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 应用root权限控制指令
- (void)rootAppsWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageName:(NSString *)packageName open:(BOOL)isOpen progress:(RCProgress)progress complete:(RCComplete)complete {
    int open = isOpen ? 1 : 0;
    NSString *order = [RCWSOrder rootApps:@[packageName] root:open];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 启用指定的应用指令
- (void)enableAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageName:(NSString *)packageName progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder enableApp:packageName];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];

    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 停用指定的应用指令
- (void)disableAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageName:(NSString *)packageName progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder disableApp:packageName];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];

    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 清理指定的应用指令
- (void)clearAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens packageName:(NSString *)packageName progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder clearApp:packageName];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];

    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

/// 设备新机指令
- (void)renewAppWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens brand:(NSString *)brand type:(NSString *)type progress:(RCProgress)progress complete:(RCComplete)complete {
    NSString *order = [RCWSOrder renewBrand:brand model:type];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];

    [[RCWebSocket shareInstance] sendOperationOrder:order toUrls:urls progress:^(int success, int fail) {
        progress(success, fail);
    } complete:^(BOOL isComplete) {
        complete(isComplete);
    }];
}

#pragma mark - 预览图指令
/// 开启预览图推流指令
- (void)startPreviewWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens width:(NSString *)width height:(NSString *)height interval:(NSString *)interval quality:(NSString *)quality preview:(RCPreview)preview {
    NSString *order  = [RCWSOrder stopPreview];
    NSString *order2 = [RCWSOrder startPreview:width height:height interval:interval quality:quality];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendPreviewOrder:order order2:order2 toUrls:urls preview:^(NSString *key, UIImage * _Nullable image) {
//        NSLog(@"预览图 size: %@", NSStringFromCGSize(image.size));
        BOOL isRotation = image.size.width > image.size.height;
        if (isRotation) { image = [self rotateImage:image]; }
        preview(key, image);
    }];
}

- (UIImage *)rotateImage:(UIImage *)image {
    // 检查输入图像是否有效
    if (!image) return nil;
    
    // 创建图像上下文
    CGSize originalSize = image.size;
    CGSize rotatedSize = CGSizeMake(originalSize.height, originalSize.width); // 宽高互换
    UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, image.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 平移上下文，使图像中心对齐
    CGContextTranslateCTM(context, rotatedSize.width / 2, rotatedSize.height / 2);
    
    // 旋转上下文 90 度（顺时针）
    CGContextRotateCTM(context, M_PI_2);
    
    // 绘制图像
    [image drawInRect:CGRectMake(-originalSize.width / 2, -originalSize.height / 2, originalSize.width, originalSize.height)];
    
    // 获取旋转后的图像
    UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 结束图像上下文
    UIGraphicsEndImageContext();
    
    return rotatedImage;
}

/// 停止预览图推流指令
- (void)stopPreviewWSHosts:(NSArray *)hosts ips:(NSArray *)ips tokens:(NSArray *)tokens preview:(RCPreview)preview {
    NSString *order = [RCWSOrder stopPreview];
    NSMutableArray *urls = [RCWSOrder getUrlsFrom:hosts ips:ips tokens:tokens];
    
    [[RCWebSocket shareInstance] sendPreviewOrder:order order2:order toUrls:urls preview:^(NSString *key, UIImage * _Nullable image) {
        preview(key, image);
    }];
}

/// 停止所有预览图推流
- (void)stopAllConnect {
    [[RCWebSocket shareInstance] stopAllConnect];
}

#pragma mark - 其他
/// ws 地址 前缀部分
+ (NSString *)getPreUrlFrom:(NSString *)host ip:(NSString *)ip {
    return [RCWSOrder getPreUrlFrom:host ip:ip];
}

@end
