//
//  CustomSubViewViewController.m
//  HWMSDKDemo
//
//

#import "CustomSubViewViewController.h"
#import <HWMUISDK/HWMBizSdk.h>
#import "CustomSubViewConfigViewController.h"
#import "GlobalHandlerImpl.h"

@interface CustomSubViewViewController ()

@property (nonatomic, strong) UISwitch *topLeftSwitch;
@property (nonatomic, strong) UISwitch *bottomCenterSwitch;
@property (nonatomic, strong) UISwitch *bottomRightSwitch;
@property (nonatomic, strong) NSMutableDictionary *config;
@property (nonatomic, strong) NSMutableDictionary *customViews;

@end

@implementation CustomSubViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"自定义视图";
    self.config = [[NSMutableDictionary alloc] init];
    self.customViews = [[NSMutableDictionary alloc] init];
    NSArray *positions = @[@(HWMCustomSubViewPositionTopLeft),
                           @(HWMCustomSubViewPositionBottomCenter),
                           @(HWMCustomSubViewPositionBottomRight)];
    for (NSNumber *position in positions) {
        HWMCustomSubViewModel *model = [self.class createDefaultCustomSubViewModel];
        [self.config setObject:model forKey:position];
        [self.customViews setObject:[self.class createButton:position.integerValue] forKey:position];
    }

    
}

- (void)setupViews {
    [super setupViews];
    
    self.topLeftSwitch = [self creatLabelAndSwitchWithTopView:nil superView:self.view describe:@"TopLeft" tag:0];
    self.bottomCenterSwitch = [self creatLabelAndSwitchWithTopView:self.topLeftSwitch.superview superView:self.view describe:@"BottomCenter" tag:1];
    self.bottomRightSwitch = [self creatLabelAndSwitchWithTopView:self.bottomCenterSwitch.superview superView:self.view describe:@"BottomRight" tag:2];
    
    UIButton *saveButton = [UIButton newButtonWithTitle:@"保存"];
    [self.view addSubview:saveButton];
    [saveButton addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bottomRightSwitch.mas_bottom).mas_offset(50);
        make.leading.mas_equalTo(30);
        make.trailing.mas_equalTo(-30);
        make.height.mas_equalTo(50);
    }];
    
    UIButton *updateBtn = [UIButton newButtonWithTitle:@"更新自定义视图"];
    [self.view addSubview:updateBtn];
    [updateBtn addTarget:self action:@selector(updateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(saveButton.mas_bottom).offset(50);
        make.leading.trailing.height.mas_equalTo(saveButton);
    }];
}

- (UISwitch *)creatLabelAndSwitchWithTopView:(UIView *)topView superView:(UIView *)superView describe:(NSString *)describe tag:(NSInteger)tag {
    UIView *v = [[UIView alloc] init];
    v.tag = tag;
    [superView addSubview:v];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        if (topView) {
            make.top.mas_equalTo(topView.mas_bottom).mas_offset(20);
            make.leading.trailing.height.equalTo(topView);
        } else {
            make.top.mas_equalTo(100);
            make.leading.mas_equalTo(30);
            make.trailing.mas_equalTo(-30);
            make.height.mas_equalTo(40);
        }
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
    [v addGestureRecognizer:tap];
    
    UISwitch *swt = [[UISwitch alloc] init];
    [v addSubview:swt];
    [swt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(v);
        make.trailing.equalTo(v);
    }];
    
    UILabel *describeLabel = [self newLabelWithTitle:describe];
    [v addSubview:describeLabel];
    [describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(v);
        make.leading.mas_equalTo(v.mas_leading);
    }];
    return swt;
}

- (UILabel *)newLabelWithTitle:(NSString *)title {
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = HexColor(0x333333);
    return label;
}

- (void)onTap:(UITapGestureRecognizer *)tap {
    HWMCustomSubViewPosition position = tap.view.tag;
    HWMCustomSubViewModel *originalModel = [self.config objectForKey:@(position)];
    CustomSubViewConfigViewController *vc = [[CustomSubViewConfigViewController alloc] initWithModel:originalModel];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)saveBtnClick {
    NSMutableDictionary *config = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *customViews = [[NSMutableDictionary alloc] init];
    if (self.topLeftSwitch.isOn) {
        config[@(HWMCustomSubViewPositionTopLeft)] = self.config[@(HWMCustomSubViewPositionTopLeft)];
        customViews[@(HWMCustomSubViewPositionTopLeft)] = self.customViews[@(HWMCustomSubViewPositionTopLeft)];
    }
    if (self.bottomCenterSwitch.isOn) {
        config[@(HWMCustomSubViewPositionBottomCenter)] = self.config[@(HWMCustomSubViewPositionBottomCenter)];
        customViews[@(HWMCustomSubViewPositionBottomCenter)] = self.customViews[@(HWMCustomSubViewPositionBottomCenter)];
    }
    if (self.bottomRightSwitch.isOn) {
        config[@(HWMCustomSubViewPositionBottomRight)] = self.config[@(HWMCustomSubViewPositionBottomRight)];
        customViews[@(HWMCustomSubViewPositionBottomRight)] = self.customViews[@(HWMCustomSubViewPositionBottomRight)];
    }
    
    [GlobalHandlerImpl sharedInstance].customSubViewConfig = config.copy;
    [GlobalHandlerImpl sharedInstance].customSubViews = customViews.copy;
    [UIUtil showMessage:@"已保存"];
}

- (void)updateBtnClick {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定义视图位置" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *topLeftAction = [UIAlertAction actionWithTitle:@"TopLeft" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self updateCustomSubView:HWMCustomSubViewPositionTopLeft];
    }];
    UIAlertAction *bottomCenterAction = [UIAlertAction actionWithTitle:@"BottomCenter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self updateCustomSubView:HWMCustomSubViewPositionBottomCenter];
    }];
    UIAlertAction *bottomRightAction = [UIAlertAction actionWithTitle:@"BottomRight" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self updateCustomSubView:HWMCustomSubViewPositionBottomRight];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:topLeftAction];
    [alertController addAction:bottomCenterAction];
    [alertController addAction:bottomRightAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)updateCustomSubView:(HWMCustomSubViewPosition)position {
    [[HWMBizSdk getBizOpenApi] updateMeetingCustomSubViewWithPosition:position];
}

+ (HWMCustomSubViewModel *)createDefaultCustomSubViewModel {
    HWMCustomSubViewModel *model = [[HWMCustomSubViewModel alloc] init];
    model.enableDrag = YES;
    model.enableAttach = YES;
    model.attachType = HWMCustomSubViewAttachTypeLeftAndRight;
    model.showFollowConfCtrlBar = YES;
    return model;
}

+ (UIButton *)createButton:(NSInteger)tag {
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:@"custom_conf_setting"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(customButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = UIColor.blackColor;
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
    }];
    btn.tag = tag;
    return btn;
}

+ (void)customButtonClick:(UIButton *)btn {
    [UIUtil showMessage:[NSString stringWithFormat:@"click custom button: %ld", btn.tag]];
}

@end
