//
//  CustomSubViewConfigViewController.m
//  HWMSDKDemo
//
//

#import "CustomSubViewConfigViewController.h"

@interface CustomSubViewConfigViewController ()

@property (weak, nonatomic) IBOutlet UISwitch *enableDragSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *enableAttachSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *enableShowFollowConfCtrlBarSwitch;
@property (weak, nonatomic) IBOutlet UIButton *attachTypeButton;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (nonatomic, strong) HWMCustomSubViewModel *model;
@property (nonatomic, assign) HWMCustomSubViewAttachType attachType;

@end

@implementation CustomSubViewConfigViewController

- (instancetype)initWithModel:(HWMCustomSubViewModel *)model {
    self = [super initWithNibName:@"CustomSubViewConfigViewController" bundle:[NSBundle mainBundle]];
    if (self) {
        _model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.saveButton setDefaultButton];
    self.enableDragSwitch.on = self.model.enableDrag;
    self.enableAttachSwitch.on = self.model.enableAttach;
    self.enableShowFollowConfCtrlBarSwitch.on = self.model.showFollowConfCtrlBar;
    [self.attachTypeButton setTitle:[self getAttachTextWithType:self.attachType] forState:UIControlStateNormal];
    self.attachType = self.model.attachType;
}

- (IBAction)selectAttachType {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"贴边方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"Left_Right" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.attachType = HWMCustomSubViewAttachTypeLeftAndRight;
        [self.attachTypeButton setTitle:[self getAttachTextWithType:self.attachType] forState:UIControlStateNormal];
    }];
    UIAlertAction *topAction = [UIAlertAction actionWithTitle:@"Top_Bottom" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.attachType = HWMCustomSubViewAttachTypeTopAndBottom;
        [self.attachTypeButton setTitle:[self getAttachTextWithType:self.attachType] forState:UIControlStateNormal];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:leftAction];
    [alertController addAction:topAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (NSString *)getAttachTextWithType:(HWMCustomSubViewAttachType)type {
    switch (type) {
        case HWMCustomSubViewAttachTypeLeftAndRight:
            return @"Left_Right";
        case HWMCustomSubViewAttachTypeTopAndBottom:
            return @"Top_Bottom";
        default:
            return @"";
    }
}

- (IBAction)save {
    self.model.attachType = self.attachType;
    self.model.enableDrag = self.enableDragSwitch.isOn;
    self.model.enableAttach = self.enableAttachSwitch.isOn;
    self.model.showFollowConfCtrlBar = self.enableShowFollowConfCtrlBarSwitch.isOn;
    [self.navigationController popViewControllerAnimated:YES];
}


@end
