//
//  OffcnZYJTestDataStorageView.m
//  OffcnLiveSDK
//
//  Created by zyj on 2020/3/28.
//  Copyright © 2020 中公教育. All rights reserved.
//

#import "OffcnZYJTestDataStorageView.h"
#import "UIViewExt.h"
#import "SDGeneralDefine.h"
#import "DataSafe.h"
#import "SDGeneralTool.h"

@interface OffcnZYJTestDataStorageView ()

@property (nonatomic, strong) UIButton *pasteBtn;
@property (nonatomic, strong) UIButton *cancellBtn;
@property (nonatomic, strong) UITextView *textView;
@end

@implementation OffcnZYJTestDataStorageView

-(UIButton *)pasteBtn{
    if (!_pasteBtn) {
        _pasteBtn = [[UIButton alloc] init];
        _pasteBtn.titleLabel.font = KFontNameMediumSize(18);
        [_pasteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_pasteBtn setTitle:@"复制" forState:UIControlStateNormal];
        [_pasteBtn addTarget:self action:@selector(pasteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _pasteBtn.backgroundColor = KButtonSelectColor;
    }
    return _pasteBtn;
}
-(void)pasteBtnAction:(UIButton *)button{
    KWeakSelf
    dispatch_async(dispatch_get_main_queue(), ^{
        
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = weakSelf.textView.text;
        [SDGeneralTool toastWithTitlemessage:@"复制成功"];
    });
}
-(UIButton *)cancellBtn{
    if (!_cancellBtn) {
        _cancellBtn = [[UIButton alloc] init];
        _cancellBtn.titleLabel.font = KFontNameMediumSize(18);
        [_cancellBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancellBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [_cancellBtn addTarget:self action:@selector(closeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _cancellBtn.backgroundColor = KButtonSelectColor;
    }
    return _cancellBtn;
}
-(void)closeBtnAction:(UIButton *)button{
    [self hidView];
}
-(UITextView *)textView{
    if (!_textView){
        _textView = [[UITextView alloc] initWithFrame:CGRectZero];
        _textView.font = KFontNameMediumSize(13);
        _textView.textColor = KTextColor;
        _textView.textAlignment = NSTextAlignmentLeft;
        
        _textView.linkTextAttributes = @{NSForegroundColorAttributeName:UICOLOR_RGB_Alpha(0x4690f7, 1)};
        _textView.backgroundColor = [UIColor clearColor];
        _textView.textContainer.lineFragmentPadding = 0;
        _textView.textContainerInset = UIEdgeInsetsZero;
        _textView.editable = NO;
        _textView.scrollEnabled = YES;
        _textView.dataDetectorTypes = UIDataDetectorTypeLink;
    }
    return _textView;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.pasteBtn];
        [self addSubview:self.cancellBtn];
        [self addSubview:self.textView];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.textView.frame = CGRectMake(20, 0, self.width-40, self.height-30-20);
    
    self.pasteBtn.frame = CGRectMake(20, CGRectGetMaxY(self.textView.frame)+10, (self.width-60)/2, 30);
    self.cancellBtn.frame = CGRectMake(CGRectGetMaxX(self.pasteBtn.frame)+20, self.pasteBtn.y, self.pasteBtn.width, self.pasteBtn.height);
}
-(void)showView{
    UIWindow *mainWindow = KKeyWindow;
    [mainWindow addSubview:self];
    [mainWindow bringSubviewToFront:self];
    [self setNeedsLayout];
    [self layoutIfNeeded];
}
-(void)setMsg:(NSString *)msg{
    _msg = msg;
    
    self.textView.text = msg;
}
-(void)hidView{
    [self endEditing:YES];
    
    [self removeAllSubViews];
    [self removeFromSuperview];
}
@end
