//
//  allVersionView.m
//  隐私保护
//
//  Created by xd h on 2023/11/27.
//

#import "allVersionView.h"
#import "couldphoneSysInfoModel.h"
#import "connectDeviceManager.h"
#import "iPhone.h"

@interface allVersionView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray*dataArr;
}
@property(nonatomic,strong)UIView *whiteBgView;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)couldphoneSysInfoModel *couldphoneSysInfoMod;
@end

@implementation allVersionView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCouldPhoneBaseInfoFun:)  name:getCouldPhoneSysInfoNotification  object:nil];
    
    [self drawAnyView];
    
    return self;
}

- (void)drawAnyView{

    dataArr = [NSMutableArray new];
    
    NSString *str1 = NSLocalizedString(@"my_set_about_version_tap_jingxiang",nil);
    NSString *str2 = NSLocalizedString(@"my_set_about_version_tap_daili",nil);
    NSString *str3 = NSLocalizedString(@"my_set_about_version_tap_app",nil);
    NSString *str4 = NSLocalizedString(@"my_set_about_version_tap_sn",nil);
    
    [dataArr addObject:str1];
    [dataArr addObject:str2];
    [dataArr addObject:str3];
    [dataArr addObject:str4];
    
    self.backgroundColor = [UIColor hwColor:@"#000000" alpha:0.6];
    
    _whiteBgView = [[UIView alloc] init];
    _whiteBgView.backgroundColor = [UIColor whiteColor];
    _whiteBgView.layer.cornerRadius = 8;
    [self addSubview:_whiteBgView];
    
    [_whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(260);
        make.centerY.mas_equalTo(self.center).offset(-20);
    }];
    
    UIButton* delBut = [[UIButton alloc] init];
    [delBut setImage:[UIImage imageNamed:@"common_del"] forState:UIControlStateNormal];
    [delBut addTarget:self action:@selector(delViewFun) forControlEvents:UIControlEventTouchUpInside];
    [_whiteBgView addSubview:delBut];
    
    [delBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40.0);
    }];
    
    UILabel * versionTipLabel = [[UILabel alloc] init];
    versionTipLabel.text = NSLocalizedString(@"my_set_about_version_tap_tip",nil) ;
    versionTipLabel.textAlignment = NSTextAlignmentCenter;
    versionTipLabel.font = [UIFont boldSystemFontOfSize:16.0];
    [_whiteBgView addSubview:versionTipLabel];
    
    [versionTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(30);
        make.height.mas_equalTo(20.0);
    }];
    
    [_whiteBgView addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(versionTipLabel.mas_bottom).offset(20);
        make.bottom.mas_equalTo(-16.0);
    }];
}

#pragma mark - 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
//        _tableView.contentInset = UIEdgeInsetsMake(-H_STATE_BAR, 0, 0, 0);
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
        [_tableView setSeparatorColor:[UIColor clearColor]];
        [_tableView setBackgroundColor:[UIColor hwColor:@"#D8D8D8" alpha:1.0]];
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setBounces:YES];
        _tableView.layer.cornerRadius = 8;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        
    }
    
    return _tableView;
}

- (void)delViewFun
{
    [self removeFromSuperview];
}

#pragma mark - 列表委托

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    static NSString *identifier = @"UITableViewCell";
    
    UITableViewCell * cell =  [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setAccessoryType:(UITableViewCellAccessoryNone)];
        
        cell.textLabel.font = [UIFont systemFontOfSize:12.0];
        cell.textLabel.textColor =  [UIColor hwColor:@"#666666" alpha:1.0];
        
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14.0];
        cell.detailTextLabel.textColor =  [UIColor hwColor:@"#0A132B" alpha:1.0];
    }
  
    if(row < dataArr.count){
        cell.textLabel.text = dataArr[row];
    }
    
    if(_couldphoneSysInfoMod){
        switch (row)
        {
            case 0:{
                cell.detailTextLabel.numberOfLines = 0;
                cell.detailTextLabel.text = _couldphoneSysInfoMod.data.data.ImageVersion;
            }
                break;
            case 1:
                cell.detailTextLabel.text = _couldphoneSysInfoMod.data.data.AgentVersion.versionName;
                break;
            case 2:{
                NSString *vers = [iPhone appVersion];
                cell.detailTextLabel.text = [NSString stringWithFormat:@"V%@",vers];
            }
                break;
            case 3:
                //cell.detailTextLabel.text = _couldphoneSysInfoMod.data.data.sn;
                cell.detailTextLabel.text = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
                break;
        }
    }

    return cell;
}

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

- (void)getCouldPhoneBaseInfoFun:(NSNotification*)not
{
    _couldphoneSysInfoMod = [not object];
    //HLog(@"%@",baseInfoModel);
    
    if(!_couldphoneSysInfoMod || ![_couldphoneSysInfoMod isKindOfClass:[couldphoneSysInfoModel class]]){
        return;
    }
    
    [self.tableView reloadData];
}
@end
