//
//  ERTCBeautyConfigurationViewController.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/8/16.
//

#import "ERTCBeautyConfigurationViewController.h"
#import "ERTCBeautyConfigurationBar.h"
#import "ERTCBeautyModel.h"
#import "ERTCPlayerViewManager.h"

@interface ERTCBeautyConfigurationViewController ()
@property (nonatomic, strong) ERTCBeautyConfigurationBar *configurationBar;
@property (nonatomic, strong) UIView *playView;
@property (nonatomic, strong) UISwitch *switchBtn;
@end

@implementation ERTCBeautyConfigurationViewController

-(void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self buildView];
}

#pragma mark - UIViewController Life Cycle
//-(BOOL)shouldAutorotate {
//    NSLog(@"ViewController-%@ shouldAutorotate", [self class]);
//    return YES;
//}
//
//-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    NSLog(@"ViewController-%@ supportedInterfaceOrientations", [self class]);
//    return UIInterfaceOrientationMaskPortrait;
//}

#pragma mark - 页面出现前
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    //禁用侧滑手势方法
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidAppear:(BOOL)animated {
    [[ERTCPlayerViewManager sharedInstance].engine setEnableLocalVideoStreamPublishing:NO];
    [[ERTCPlayerViewManager sharedInstance].engine enableLocalVideo:YES];
    [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:self.playView withRegionID:0];
    [[ERTCPlayerViewManager sharedInstance].engine setLocalPreviewScaleType:ERTCVideoFillMode_AspectFill withRegionID:0];
    [[ERTCPlayerViewManager sharedInstance].engine setBeautyEnabled:YES];
}

#pragma mark - 页面即将消失
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    //禁用侧滑手势方法
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    [[ERTCPlayerViewManager sharedInstance].engine enableLocalVideo:NO];
    [[ERTCPlayerViewManager sharedInstance].engine setLocalPreview:nil withRegionID:0];
}

#pragma mark - 页面重新绘制
/// 页面重新绘制
- (void)viewDidLayoutSubviews {
    
    [super viewDidLayoutSubviews];
    
    [self updateSwitchBtnLayer];
}
#pragma mark - Action
- (void)switchBtnChange:(UISwitch *)switchBtn {
    [[ERTCPlayerViewManager sharedInstance].engine setBeautyEnabled:switchBtn.on];
    self.configurationBar.hidden = ![ERTCPlayerViewManager sharedInstance].engine.beautyEnabled;
    [self.configurationBar updateBeautyEnabled];
    
    [self updateSwitchBtnLayer];
}

-(void)updateSwitchBtnLayer {
    if (CGRectGetWidth(self.switchBtn.frame) > 0) {
        if (@available(iOS 13.0, *)) {
            CGFloat minSide = MIN(self.switchBtn.frame.size.height, self.switchBtn.frame.size.width);
            self.switchBtn.layer.cornerRadius = ceil(minSide/2);
            self.switchBtn.layer.borderWidth = 1;
            if (self.switchBtn.on == NO) {
                self.switchBtn.layer.borderColor = [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1].CGColor;
            } else {
                self.switchBtn.layer.borderColor = [UIColor clearColor].CGColor;
            }
        }
    }
}

//切换摄像头
-(void)switchCammerBtnClick:(UIButton *)btn {
    NSLog(@"switchCammerBtnClick");
    [[ERTCPlayerViewManager sharedInstance].engine switchCamera];
    
}

#pragma mark - 初始化UI
- (void)buildView {
    //切换前后摄像头按钮
    
    [self.view addSubview:self.playView];
    
    UIButton *switchCammerBtn = [[UIButton alloc] init];
    [switchCammerBtn setImage:[UIImage imageNamed:@"小-2"] forState:(UIControlStateNormal)];
    [switchCammerBtn addTarget:self action:@selector(switchCammerBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.playView addSubview:switchCammerBtn];
    
    //美颜开关按钮
    UISwitch *switchBtn = [[UISwitch alloc] init];
    switchBtn.on = YES;
    switchBtn.tintColor =  [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1];
    switchBtn.onTintColor =  [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1];
    [switchBtn addTarget:self action:@selector(switchBtnChange:) forControlEvents:UIControlEventValueChanged];
    [self.playView addSubview:switchBtn];
    self.switchBtn = switchBtn;
    
    //美颜开关提示语
    UILabel *label = [[UILabel alloc] init];
    label.text = @"美颜";
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    label.textColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1/1.0];
    UILabel *switchBtnTip = label;
    [self.playView addSubview:switchBtnTip];
    
    //关闭按钮
    UIView *historyTapView = [UIView new];
    historyTapView.backgroundColor = [UIColor clearColor];
    
    [self.playView addSubview:historyTapView];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backToMainAPP)];
    [historyTapView addGestureRecognizer:tapGes];
    
    UIImageView *historyBtnLabel = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icn_close_search_bg"]];
    [historyTapView addSubview:historyBtnLabel];
    [historyBtnLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.center.equalTo(historyTapView);
    }];
    
    CGFloat top = 20;
    [historyTapView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(self.playView).with.offset(15);
        make.top.equalTo(kSafeMasTop(self.playView)).with.offset(top);
        make.width.height.ysmas_equalTo(50);
    }];
    
    [switchCammerBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.right.equalTo(self.playView).with.offset(-16);
        make.centerY.equalTo(historyTapView);
        make.width.height.ysmas_equalTo(40);
    }];
    
    [switchBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.right.equalTo(switchCammerBtn.ysmas_left).with.offset(-20);
        make.centerY.equalTo(historyTapView);
    }];
    
    [switchBtnTip ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.right.equalTo(switchBtn.ysmas_left).with.offset(-10);
        make.centerY.equalTo(historyTapView);
    }];
    
    
    [self.playView addSubview:self.configurationBar];
    [self.configurationBar ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.playView);
    }];
    
//    self.configurationBar.hidden = ![VCSBeautyManager sharedManager].beautyEnabled;
}

#pragma mark - init subViews
-(void)backToMainAPP
{
    [self.configurationBar saveCacheFilter];
    
    NSArray *viewcontrollers = self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1] == self) {
            //push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else
    {
        //present方式
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - UI Method
-(ERTCBeautyConfigurationBar *)configurationBar {
    if (!_configurationBar) {
        _configurationBar = [[ERTCBeautyConfigurationBar alloc] init];
        _configurationBar.vc = self;
    }
    
    return _configurationBar;
}

- (UIView *)playView {
    if (!_playView) {
        _playView = [UIView new];
        _playView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }
    return _playView;
}

@end
