//
//  EZTestVC.m
//  EZOpenSDKDemo
//
//  Created by JuneCheng on 2023/6/20.
//  Copyright © 2023 hikvision. All rights reserved.
//

#import "EZTestVC.h"
#import "SMIPContainerTestViewController.h"
#import "EZDeviceInfo.h"

// 特殊功能标识常量
static NSString * const kSpecialAutoTest = @"SPECIAL_AUTO_TEST";
static NSString * const kSpecialSpaceId = @"SPECIAL_SPACE_ID";
static NSString * const kSpecialClearVerifyCode = @"SPECIAL_CLEAR_VERIFY_CODE";
static NSString * const kSpecialSwipePlayTest = @"SPECIAL_SWIPE_PLAY_TEST";

@interface EZTestVC () {
    NSArray *array;
}

@end

@implementation EZTestVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"萤石功能测试";
    
    BOOL ddnsDemoShow = NO;
#ifdef EZVIZ_GLOBAL_DEMO
    ddnsDemoShow = YES;
#endif
    NSString *autoTestTitle = [kUserDefaults boolForKey:EZAutoTest] ? @"自动化测试(开启)" : @"自动化测试(关闭)";
    
    // 配置功能列表 - 格式: [标题, ViewController类名或特殊功能标识]
    // ViewController类名: 跳转到对应页面
    // SPECIAL_开头: 特殊功能处理
    array = @[@[@"设备发现", @"EZLocalDeviceListViewController"],
              @[@"P2P测试", @"EZP2pTestVC"],
              @[autoTestTitle, kSpecialAutoTest],
              @[@"取流方式设置", @"EZStreamTypeSettingVC"],
              @[@"取流特殊设置", @"EZStreamConfigSettingVC"],
              @[@"设置云录制SpaceId", kSpecialSpaceId],
              @[@"小权限tkToken设置", @"EZTKTokenTestVC"],
              @[@"接口测试", @"EZHttpInterfaceTestVC"],
              @[@"设备验证码清空", kSpecialClearVerifyCode],
              @[@"左右滑动播放测试", kSpecialSwipePlayTest],
              ];
    [self setupDataArr:array];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *className = self.classNames[indexPath.row];
    
    // 判断是否为特殊功能项
    if ([className hasPrefix:@"SPECIAL_"]) {
        [self handleSpecialAction:className];
    } else if (!kStringIsEmpty(className)) {
        // 跳转到对应的ViewController
        UIViewController *vc = [[NSClassFromString(className) alloc] init];
        vc.title = self.titles[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - 特殊功能处理

/**
 * 处理特殊功能项 - 根据特殊标识符处理
 */
- (void)handleSpecialAction:(NSString *)specialType {
    if ([specialType isEqualToString:kSpecialAutoTest]) {
        // 自动化测试
        [self showAlertWithTitle:@"是否开启自动化测试" message:@"萤石SDK测试人员专用，开启后本应用中的账号登录和退出功能将禁用" appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
            alertMaker.addActionCancelTitle(@"取消");
            alertMaker.addActionDefaultTitle(@"确定");
        } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
            if (buttonIndex == 1) {
                [kUserDefaults setBool:YES forKey:EZAutoTest];
                [kUserDefaults synchronize];
            }
        }];
    } else if ([specialType isEqualToString:kSpecialSpaceId]) {
        // 云录制spaceId设置
        [self showAlertWithTitle:@"请输入云录制spaceId" message:nil appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
            alertMaker.addActionCancelTitle(@"取消");
            alertMaker.addActionDefaultTitle(@"确定");
            [alertMaker addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            }];
        } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
            if (buttonIndex == 1) {
                UITextField *spaceIdTF = alertSelf.textFields.firstObject;
                [kUserDefaults setValue:spaceIdTF.text forKey:EZCloudRecordSpaceId];
                [kUserDefaults synchronize];
                [EZToast show:@"SpaceId设置成功"];
            }
        }];
    } else if ([specialType isEqualToString:kSpecialClearVerifyCode]) {
        // 验证码清空
        [self showAlertWithTitle:@"是否清空所有设备验证码" message:nil appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
            alertMaker.addActionCancelTitle(@"取消");
            alertMaker.addActionDefaultTitle(@"确定");
        } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
            if (buttonIndex == 1) {
                [[GlobalKit shareKit] clearAllDeviceSerialVerifyCode];
                [EZToast show:@"所有设备验证码已清空"];
            }
        }];
    } else if ([specialType isEqualToString:kSpecialSwipePlayTest]) {
        // 左右滑动播放测试
        NSMutableArray *array = [NSMutableArray array];
        for (int i = 0; i < self.deviceArray.count; i ++) {
            EZDeviceInfo *deviceInfo = self.deviceArray[i];
            
            SMTestPlayConfig *config = [[SMTestPlayConfig alloc] init];
            config.deviceSerial = deviceInfo.deviceSerial;
            config.cameraNo = 1;
            
            [array addObject:config];
        }
        SMIPContainerTestViewController *testVC = [[SMIPContainerTestViewController alloc] initWithConfigs:array];
        [self.navigationController pushViewController:testVC animated:YES];
    }
}

#pragma mark - 懒加载


@end
