//
//  FavoritesAudioCell.m
//  AIIM
//
//  Created by qitewei on 2025/4/30.
//

#import "FavoritesAudioCell.h"

#import "Masonry.h"
#import "config.h"

@interface FavoritesAudioCell()
@property (nonatomic, strong) UILabel * contentLbl;
@property (nonatomic, strong) UILabel * timeLbl;
@property (nonatomic, strong) UIImageView * typeImageView;
@end

@implementation FavoritesAudioCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self configUI];
    }
    return self;
}

- (void)configUI{
    self.contentView.backgroundColor = globalColor(GCTypeDark6);
    
    [self.contentView addSubview:self.typeImageView];
    [self.typeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(16);
        make.bottom.mas_equalTo(-16);
    }];
    
    [self.contentView addSubview:self.timeLbl];
    [self.timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(12);
        make.left.mas_equalTo(20);
        make.bottom.mas_equalTo(-24);
    }];
    
    [self.contentView addSubview:self.contentLbl];
    [self.contentLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(14);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(self.typeImageView.mas_left).offset(-12);
        make.bottom.mas_equalTo(self.timeLbl.mas_top).offset(-12);
    }];
}

- (void)setMsg:(NSDictionary *)msg{
    _msg = msg;
    
    NSString *extend = msg[@"extend"];
    NSData *data = [extend dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *extendDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    if ([msg[@"messageType"] isEqualToString:@"3"]) {
        self.typeImageView.image = kImageMake(@"audio_icon");
        if ([extendDict jk_hasKey:@"time"]) {
            self.contentLbl.text = [NSString stringWithFormat:@"%@''",extendDict[@"time"]];
        }
    }else{
        self.contentLbl.text = extendDict[@"fileName"]?:@"";
        self.typeImageView.image = kImageMake(@"file_icon");
    }
    self.timeLbl.text = msg[@"createTime"];
    
}

#pragma mark lazy
- (UILabel *)contentLbl{
    if (!_contentLbl) {
        _contentLbl = [[UILabel alloc] init];
        _contentLbl.textColor = UIColor.whiteColor;
        _contentLbl.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    }
    return _contentLbl;
}

- (UILabel *)timeLbl{
    if (!_timeLbl) {
        _timeLbl = [[UILabel alloc] init];
        _timeLbl.textColor = globalColor(GCTypeDark7);
        _timeLbl.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    }
    return _timeLbl;
}

- (UIImageView *)typeImageView{
    if (!_typeImageView) {
        _typeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"audio_icon"]];
    }
    return _typeImageView;
}

@end
