//
//  FtCjhxDialog.m
//  Open
//
//  Created by weface on 2023/6/9.
//  Copyright © 2023 QXY. All rights reserved.
//

#import "GoodCommentDialog.h"

@interface GoodCommentDialog ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong)UIView *contentView;
@property (nonatomic, copy)btnYes btnYes;
@property (nonatomic, copy) btnNo btnNo;

@end

@implementation GoodCommentDialog

- (instancetype)initWithTitle:(NSString *)btnTitle btnYes:(btnYes)btnYes btnNo:(btnNo)btnNo{
    self = [super init];
    if (self) {
        self.btnYes = btnYes;
        self.btnNo = btnNo;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, ScreenWidth/20*17, 40 * WIDTHRADIU*9.5);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 5 * WIDTHRADIU;
    
    
    //顶部图片
    UIImageView *topImage=[UIImageView new];
    topImage.image  = [UIImage imageNamed:@"good_comment_top"];
    [self.contentView addSubview:topImage];
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    //窗体主内容
    NSString *msg=@"觉得好用就给我们一个好评吧~";
    UILabel *lb_message = [UILabel new];
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    style.lineSpacing = 10;
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:msg];
    [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, msg.length)];
    lb_message.attributedText = attrString;
    lb_message.text =msg;
    //lb_message.bounds=CGRectMake(0, 0, ScreenWidth/5*4, ScreenHeight/10*3);
    lb_message.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.textColor = [UIColor colorWithHex:0x505050];
    lb_message.lineBreakMode=UILineBreakModeWordWrap;
    lb_message.numberOfLines=0;
    [self.contentView addSubview:lb_message];
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImage.mas_bottom).offset(10 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    

    
    //同意按钮
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"五星好评" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
    confirmBtn.backgroundColor = [UIColor colorWithHex:0x2F72FA];
    confirmBtn.layer.cornerRadius = 5 * WIDTHRADIU;
    confirmBtn.layer.masksToBounds = YES;
    [confirmBtn addTarget:self action:@selector(btnYes:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_message.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.offset(ScreenWidth/20*17-40*WIDTHRADIU);
        make.height.offset(50 * WIDTHRADIU);
    }];
    
    //不同意
    UILabel *textNo = [UILabel new];
    textNo.text =@"我要吐槽";
    textNo.font = [UIFont systemFontOfSize:16 * WIDTHRADIU weight:UIFontWeightMedium];
    textNo.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapText03 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnNo:)];
    [textNo addGestureRecognizer:tapText03];
    //text01.textAlignment = NSTextAlignmentCenter;
    textNo.textColor = [UIColor colorWithHex:0x4c4c4c];
    [self.contentView addSubview:textNo];
    [textNo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(confirmBtn.mas_bottom).offset(18*WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
}


-(void)btnYes:(UIButton *)sender{
    if (self.btnYes) {
        self.btnYes();
    }
    [self dismissUI];
    
}
-(void)btnNo:(UITapGestureRecognizer *)tap{
    if (self.btnNo) {
        self.btnNo();
    }
    [self dismissUI];
}
#pragma mark - public methods,
+ (void)showWithTitle:(UINavigationController *) view:(NSString *)btnTitle btnYes:(btnYes)btnYes btnNo:(btnNo)btnNo{
    
    GoodCommentDialog *alertView = [[GoodCommentDialog alloc] initWithTitle: btnTitle btnYes:btnYes btnNo:btnNo];
    
    //UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [view.view addSubview:alertView];
}

//点击空白处
- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self dismissUI];
}

#pragma mark -隐藏弹框
- (void)dismissUI{
    [self removeFromSuperview];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.contentView]) {
        return NO;
    }
    return YES;
}
@end
