//
//  NvPositionListView.m
//  SDKDemo
//
//  Created by Meicam on 2018/6/6.
//  Copyright © 2018年 刘东旭. All rights reserved.
//

#import "NvPositionListView.h"
#import "NVHeader.h"

@interface NvPositionListView()

@property (nonatomic, strong) UILabel *styleApplyLabel;
@property (nonatomic, strong) NvButton *okButton;

@end

@implementation NvPositionListView

- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor nv_colorWithHexRGB:@"#242728"];
        self.leftButton = [UIButton nv_buttonWithTitle:@"" textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvLeftButton"]];
        self.rightButton = [UIButton nv_buttonWithTitle:@"" textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvRightButton"]];
        self.upButton = [UIButton nv_buttonWithTitle:@"" textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvUpButton"]];
        self.downButton = [UIButton nv_buttonWithTitle:@"" textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvDownButton"]];
        self.verticalButton = [UIButton nv_buttonWithTitle:@"" textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvVerticalButton"]];
        self.horizontalButton = [UIButton nv_buttonWithTitle:@"" textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvHorizontalButton"]];
        [self addSubview:self.leftButton];
        [self addSubview:self.rightButton];
        [self addSubview:self.upButton];
        [self addSubview:self.downButton];
        [self addSubview:self.verticalButton];
        [self addSubview:self.horizontalButton];
        [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(60*SCREANSCALE));
            make.top.equalTo(@(19*SCREANSCALEHEIGHT));
            make.width.equalTo(@(38*SCREANSCALE));
            make.height.equalTo(@(23*SCREANSCALE));
        }];
        [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(-60*SCREANSCALE));
            make.top.equalTo(@(19*SCREANSCALEHEIGHT));
            make.width.equalTo(@(38*SCREANSCALE));
            make.height.equalTo(@(23*SCREANSCALE));
        }];
        [self.verticalButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@(19*SCREANSCALEHEIGHT));
            make.centerX.equalTo(self);
            make.width.equalTo(@(38*SCREANSCALE));
            make.height.equalTo(@(23*SCREANSCALE));
        }];
        [self.upButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(60*SCREANSCALE));
            make.top.equalTo(self.leftButton.mas_bottom).offset(17*SCREANSCALEHEIGHT);
            make.width.equalTo(@(38*SCREANSCALE));
            make.height.equalTo(@(23*SCREANSCALE));
        }];
        [self.horizontalButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.leftButton.mas_bottom).offset(17*SCREANSCALEHEIGHT);
            make.width.equalTo(@(38*SCREANSCALE));
            make.height.equalTo(@(23*SCREANSCALE));
        }];
        [self.downButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(-60*SCREANSCALE));
            make.top.equalTo(self.leftButton.mas_bottom).offset(17*SCREANSCALEHEIGHT);
            make.width.equalTo(@(38*SCREANSCALE));
            make.height.equalTo(@(23*SCREANSCALE));
        }];
        __weak typeof(self)weakSelf = self;
        [self.leftButton nv_BtnClickHandler:^{
            weakSelf.type = Left;
            if ([weakSelf.delegate respondsToSelector:@selector(nvPositionListView:leftClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvPositionListView:weakSelf leftClick:weakSelf.leftButton isApplyToAllCaption:NO];
            }
        }];
        [self.rightButton nv_BtnClickHandler:^{
            weakSelf.type = Right;
            if ([weakSelf.delegate respondsToSelector:@selector(nvPositionListView:rightClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvPositionListView:weakSelf rightClick:weakSelf.rightButton isApplyToAllCaption:NO];
            }
        }];
        [self.upButton nv_BtnClickHandler:^{
            weakSelf.type = Up;
            if ([weakSelf.delegate respondsToSelector:@selector(nvPositionListView:upClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvPositionListView:weakSelf upClick:weakSelf.upButton isApplyToAllCaption:NO];
            }
        }];
        [self.downButton nv_BtnClickHandler:^{
            weakSelf.type = Down;
            if ([weakSelf.delegate respondsToSelector:@selector(nvPositionListView:downClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvPositionListView:weakSelf downClick:weakSelf.downButton isApplyToAllCaption:NO];
            }
        }];
        [self.verticalButton nv_BtnClickHandler:^{
            weakSelf.type = Vertical;
            if ([weakSelf.delegate respondsToSelector:@selector(nvPositionListView:verticalClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvPositionListView:weakSelf verticalClick:weakSelf.verticalButton isApplyToAllCaption:NO];
            }
        }];
        [self.horizontalButton nv_BtnClickHandler:^{
            weakSelf.type = Horizontal;
            if ([weakSelf.delegate respondsToSelector:@selector(nvPositionListView:horizontalClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvPositionListView:weakSelf horizontalClick:weakSelf.horizontalButton isApplyToAllCaption:NO];
            }
        }];
        self.applyButton = [NvButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvNoApplyAll"]];
        [self.applyButton setImage:[NvUtils imageWithName:@"NvApplyAll"] forState:UIControlStateSelected];
        self.styleApplyLabel = [UILabel nv_labelWithText:@"将样式应用到所有字幕" fontSize:10 textColor:[UIColor whiteColor]];
        self.styleApplyLabel.font = [NvUtils regularFontWithSize:10];
        self.styleApplyLabel.alpha = 0.8;
        [self.applyButton nv_BtnClickHandler:^{
            weakSelf.applyButton.selected = !weakSelf.applyButton.selected;
            if (weakSelf.applyButton.selected) {
                weakSelf.styleApplyLabel.textColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"];
            } else {
                weakSelf.styleApplyLabel.textColor = [UIColor nv_colorWithHexRGB:@"#FFFFFF"];
            }
        }];
        
        [self addSubview:self.applyButton];
        [self addSubview:self.styleApplyLabel];
        
        [self.applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(13*SCREANSCALE));
            make.bottom.equalTo(self).offset(-20*SCREANSCALE);
            make.width.height.equalTo(@(15*SCREANSCALE));
        }];
        [self.styleApplyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.applyButton.mas_centerY);
            make.left.equalTo(self.applyButton.mas_right).offset(7*SCREANSCALE);
        }];
    }
    return self;
}

- (void)resetApplyButton {
    self.type = None;
    self.applyButton.selected = NO;
    self.styleApplyLabel.textColor = [UIColor nv_colorWithHexRGB:@"#FFFFFF"];
}

- (CGSize)intrinsicContentSize {
    return CGSizeMake(SCREEN_WDITH, 87*SCREANSCALE);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
