//
//  CustomTextViewController.m
//  HWMSDKDemo
//
//

#import "CustomTextViewController.h"
#import "GlobalHandlerImpl.h"
#import "GlobalInputView.h"

@interface CustomTextViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) GlobalInputView *enView;
@property (nonatomic, strong) GlobalInputView *cnView;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *typeArray;
@property (nonatomic, strong) UISwitch *swt;

@end

@implementation CustomTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    GlobalInputView *cnView = [[GlobalInputView alloc] init];
    [self.view addSubview:cnView];
    [cnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(100);
        make.height.mas_equalTo(50);
    }];
    cnView.titleLabel.text = @"cn:";
    cnView.inputTextField.placeholder = @"  输入定制文案";
    self.cnView = cnView;
    
    GlobalInputView *enView = [[GlobalInputView alloc] init];
    [self.view addSubview:enView];
    [enView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.height.mas_equalTo(self.cnView);
        make.top.mas_equalTo(self.cnView.mas_bottom).offset(10);
    }];
    enView.titleLabel.text = @"en:";
    enView.inputTextField.placeholder = @"  Input custom text";
    self.enView = enView;
    
    UISwitch *swt = [[UISwitch alloc] init];
    [self.view addSubview:swt];
    [swt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(enView.mas_bottom).offset(15);
        make.trailing.equalTo(cnView.mas_trailing);
    }];
    [swt addTarget:self action:@selector(switchChange) forControlEvents:UIControlEventValueChanged];
    self.swt = swt;
    
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    self.pickerView = pickerView;
    [self.view addSubview:self.pickerView];
    [pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.cnView);
        make.top.equalTo(self.enView.mas_bottom).offset(40);
        make.height.mas_equalTo(200);
    }];
    
    UIButton * confirmBtn = [UIButton newButtonWithTitle:@"确定"];
    confirmBtn.accessibilityIdentifier = HWMGlobalConfigCustomTextConfirm;
    [self.view addSubview:confirmBtn];
    [confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(pickerView.mas_bottom).mas_offset(50);
        make.leading.mas_equalTo(50);
        make.height.mas_equalTo(50);
        make.trailing.mas_equalTo(-50);
    }];
}

- (void)confirmClick {
    if (self.cnView.inputTextField.text.length <= 0 || self.enView.inputTextField.text.length <= 0) {
        [UIUtil showMessage:@"请输入中文和英文定制文案"];
        return;
    }
    NSInteger index = [self.pickerView selectedRowInComponent:0];
    NSDictionary *typeJson = self.typeArray[index];
    HWMCustomUITextScene scene = [typeJson[@"type"] integerValue];
    [[GlobalHandlerImpl sharedInstance] setupCustomUIText:scene cnText:self.cnView.inputTextField.text enText:self.enView.inputTextField.text];
    [UIUtil showMessage:@"操作成功"];
}

#pragma mark - UIPickerViewDataSource & UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.typeArray.count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *rowView = (UILabel *)view;
    if (!rowView) {
        rowView = [[UILabel alloc] init];
        rowView.backgroundColor = [UIColor clearColor];
        rowView.font = [UIFont systemFontOfSize:16];
        rowView.textAlignment = NSTextAlignmentCenter;
    }
    NSDictionary *item = self.typeArray[row];
    if (self.swt.isOn) {
        rowView.text = item[@"screenText"];
    } else {
        rowView.text = item[@"typeText"];
    }
    return rowView;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return self.view.bounds.size.width - 100;
}

- (void)switchChange {
    [self.pickerView reloadComponent:0];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (NSArray *)typeArray {
    if (!_typeArray) {
        _typeArray = @[
            @{
                @"type": @(HWMCustomUITextSceneCloudRecordFinishedAlert),
                @"typeText": @"CloudRecordFinishedAlert",
                @"screenText": @"会议云录制结束弹窗提示文案"
            },
            @{
                @"type": @(HWMCustomUITextSceneCloudRecordFinishedInMeetingAlert),
                @"typeText": @"CloudRecordFinishedInMeetingAlert",
                @"screenText": @"会中会议云录制结束弹窗提示文案"
            },
            @{
                @"type": @(HWMCustomUITextSceneLeaveOrEndConfPopupWindow),
                @"typeText": @"LeaveOrEndConfPopupWindow",
                @"screenText": @"离开或结束会议弹框标题文案"
            },
            @{
                @"type": @(HWMCustomUITextSceneCloudRecordMenuTitle),
                @"typeText": @"CloudRecordMenuTitle",
                @"screenText": @"录制弹窗菜单中的云录制标题"
            },
            @{
                @"type": @(HWMCustomUITextSceneMainWindowCloudRecordDes),
                @"typeText": @"MainWindowCloudRecordDes",
                @"screenText": @"主窗口中的云录制描述"
            },
            @{
                @"type": @(HWMCustomUITextSceneCustomLiveType),
                @"typeText": @"CustomLiveType",
                @"screenText": @"主窗口的自定义直播文案场景"
            },
            @{
                @"type": @(HWMCustomUITextSceneCustomLiveToast),
                @"typeText": @"CustomLiveToast",
                @"screenText": @"主窗口的自定义直播提示文案"
            }
        ];
    }
    return _typeArray;
}

@end
