//
//  HaveNewVersionView.m
//  VclustersGemini
//
//  Created by APPLE on 2020/2/24.
//  Copyright © 2020 APPLE. All rights reserved.
//

#import "HaveNewVersionView.h"

@interface HaveNewVersionView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation HaveNewVersionView
@synthesize titleLabel;
@synthesize detailLabel;
@synthesize bgView;
@synthesize deleteBtn;

static HaveNewVersionView * _instance;

+ (instancetype)shardInstance {
    
    static dispatch_once_t HaveNewVersionViewonceToken;
    dispatch_once(&HaveNewVersionViewonceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        [self drawAnyView];
    }
    
    return self;
}


- (void)drawAnyView
{
    self.tag = 111111;
    [self setBackgroundColor:HW000000Color60];
    
    bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:[UIColor whiteColor]];
    [bgView.layer setCornerRadius:8.f];
    [self addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(328.f);
        make.width.mas_equalTo(300.f);
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    // 图片
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.image = [UIImage imageNamed:@"gengxinbeijing_pic"];
    topImageView.contentMode = UIViewContentModeScaleAspectFit;
    [bgView addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bgView.mas_top);
        make.left.mas_equalTo(bgView.mas_left);
        make.right.mas_equalTo(bgView.mas_right);
        make.height.mas_equalTo(141);
    }];
    
    /*提示文字*/
    UILabel *tipsLabel = [[UILabel alloc] init];
    [tipsLabel setText:NSLocalizedString(@"app_update_new_version_tips",nil)];
    [tipsLabel setTextColor:HW0A132BColor];
    [tipsLabel setFont:[UIFont systemFontOfSize:14.f]];
    [tipsLabel setTextAlignment:(NSTextAlignmentCenter)];
    [tipsLabel setNumberOfLines:0];
    [bgView addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImageView.mas_bottom).offset(30.f);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(60);
    }];
    
    /*删除按钮*/
    deleteBtn = [[UIButton alloc] init];
    [deleteBtn setBackgroundColor:[UIColor clearColor]];
    [deleteBtn setImage:[UIImage imageNamed:@"guanbi_bai_icon"] forState:(UIControlStateNormal)];
    [deleteBtn addTarget:self action:@selector(deleteBtnPressed) forControlEvents:(UIControlEventTouchUpInside)];
    [bgView addSubview:deleteBtn];
    
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(45.f);
        make.width.mas_equalTo(45.f);
        make.right.mas_equalTo(bgView.mas_right).mas_offset(0);
        make.top.mas_equalTo(bgView.mas_top).mas_offset(0);
    }];
    deleteBtn.hidden = YES;
    
    // 分割线
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = HWCCCCCCColor;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(-92);
    }];
    
//    /*确定按钮*/
//    UIButton *okBtn = [[UIButton alloc] init];
//    [okBtn setBackgroundImage:[UIImage imageNamed:@"login_select_corner"] forState:(UIControlStateNormal)];
//    [okBtn setTitle:@"更新" forState:(UIControlStateNormal)];
//    [okBtn.titleLabel setFont:[UIFont systemFontOfSize:14.f]];
//    [okBtn addTarget:self action:@selector(updateBtnPressed) forControlEvents:(UIControlEventTouchUpInside)];
//    [okBtn.layer setCornerRadius:20.f];
//    okBtn.layer.masksToBounds = YES;
//    [bgView addSubview:okBtn];
//
//    [okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(40.f);
//        make.width.mas_equalTo(168.f);
//        make.centerX.equalTo(bgView.mas_centerX);
//        make.bottom.mas_equalTo(-20.f);
//    }];
    
    UIButton *updatekBtn = [[UIButton alloc] init];
    updatekBtn.frame = CGRectMake(0, 0, 160.f, 48.f);

    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,160.f,48.f);
    gl.startPoint = CGPointMake(0, 0.5);
    gl.endPoint = CGPointMake(1, 0.5);
    gl.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
    gl.locations = @[@(0), @(1.0f)];
    
    [updatekBtn.layer addSublayer:gl];
    [updatekBtn addTarget:self action:@selector(updateBtnPressed) forControlEvents:(UIControlEventTouchUpInside)];
    [updatekBtn setTitle:NSLocalizedString(@"app_update_update",nil) forState:(UIControlStateNormal)];
    [updatekBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [updatekBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
    [updatekBtn.layer setCornerRadius:8.f];
    updatekBtn.clipsToBounds = YES;
    [bgView addSubview:updatekBtn];
    [updatekBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView.mas_centerX);
        make.width.mas_equalTo(160.f);
        make.bottom.mas_equalTo(-20.f);
        make.height.mas_equalTo(48.f);
    }];
    
//    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 1, 1) style:(UITableViewStylePlain)];
//    tableView.delegate = self;
//    tableView.dataSource = self;
//    [tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
//    [tableView setSeparatorColor:[UIColor clearColor]];
//    [tableView setBackgroundColor:[UIColor clearColor]];
//    if (@available(iOS 15.0, *)) {
//        tableView.sectionHeaderTopPadding = 0;
//    }
//    [bgView addSubview:tableView];
//    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(0);
//        make.right.mas_equalTo(0);
//        make.top.mas_equalTo(topImageView.mas_bottom);
//        make.bottom.mas_equalTo(line.mas_top);
//    }];
}

- (void)deleteBtnPressed
{
    [self removeFromSuperview];
    self.isShow = NO;
    
    if ([_delegate respondsToSelector:@selector(closeBtnBePressed)]) {
        [_delegate closeBtnBePressed];
    }
}

- (void)updateBtnPressed {
    
    if (!self.versionModel.necessary) { // 非强制更新
        self.isShow = NO;
        [self removeFromSuperview];
        
        if ([_delegate respondsToSelector:@selector(closeBtnBePressed)]) {
            [_delegate closeBtnBePressed];
        }
    }
    
    /*跳转蒲公英*/
    
    NSURL * url = [NSURL URLWithString:self.versionModel.downUrl];
    
    HLog(@"立即更新-跳转到：%@",self.versionModel.downUrl);
//    NSString *url = [self.versionModel.downUrl stringByReplacingOccurrencesOfString:@" " withString:@""]; // 防止url里有空格字符导致无法跳转
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
        ;
    }];
}

#pragma mark - 列表委托
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier  = @"newVersionInfoCellidentifier";
    NSInteger row     = indexPath.row;
    
    NSString *tipsString = self.dataSource[row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UITableViewCell *payTypecell = [tableView dequeueReusableCellWithIdentifier:identifier];
    payTypecell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundColor:[UIColor clearColor]];
        
       /*文字*/
        [cell.textLabel setFont:[UIFont systemFontOfSize:14.f]];
        [cell.textLabel setTextColor:HWCFD1D4Color];
        cell.textLabel.numberOfLines = 0;
        [cell.textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.top.mas_equalTo(5);
            make.bottom.mas_equalTo(5);
        }];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"·%@",tipsString];
        
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 37)];
    bgView.backgroundColor = HWF5F7FAColor;
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = [NSString stringWithFormat:@"发现新版本（%@）", self.versionModel.versionNumber];
    label1.textColor = HWCFD1D4Color;
    label1.font = [UIFont boldSystemFontOfSize:14.f];
    [bgView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
    }];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = [NSString stringWithFormat:@"大小：%@MB", self.versionModel.fileSize];
    label2.textColor = HWF5F7FAColor;
    label2.font = [UIFont systemFontOfSize:10.f];
    [bgView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label1.mas_right).mas_offset(5);
        make.centerY.mas_equalTo(label1.mas_centerY);
    }];
    
    return bgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 35;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        NSArray *tips = [self.versionModel.versionInfo componentsSeparatedByString:@"&&"];
        _dataSource = [NSMutableArray arrayWithArray:tips];
    }
    return _dataSource;
}
@end
