//
//  ChatListCell.m
//  AIIM
//
//  Created by gan on 2025/4/6.
//

#import <Foundation/Foundation.h>
#import "ChatListCell.h"
#import "CryptoAES.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "config.h"

@interface ChatListCell()
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UILabel *lastMsg;
@property (weak, nonatomic) IBOutlet UILabel *countText;

@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@end

@implementation ChatListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    }
    return self;
}
- (void)fillWithData:( NSDictionary*)data {
    
//    NSLog(@"fillWithData:%@",data);
    NSString *avatar =data[@"avatar"];
    if([avatar isKindOfClass:[NSString class]]){
        
    }
    else{
        avatar = @"";
    }
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage imageNamed:@"Avatar"]];
     self.name.text = data[@"name"];
    NSString *dcontent = data[@"lastMessage"];//[CryptoAES.shareInstance decryptDataL:data[@"lastMessage"]];
    self.lastMsg.text = dcontent;
    _countText.layer.cornerRadius = 10.0f; // 设置圆角
    _countText.layer.masksToBounds = YES; // 防止子视图超出边界
    _countText.backgroundColor = globalColor(GCTypeOrangeR);
    
    self.avatar.layer.cornerRadius = 22.f;
    
    NSString *count = data[@"unreadCount"];
    NSLog(@"count:%@",count);
    if(count.intValue>99){
        _countText.text=@"99+";
        _countText.alpha = 1;
    }
    else if(count.intValue==0){
        _countText.alpha = 0;
    }
    else{
        _countText.text=[NSString stringWithFormat:@"%@",count];
        _countText.alpha = 1;
    }
    
    //NSLog(@"lastTime:%@",data[@"lastTime"]);
    NSString *timestr =data[@"lastTime"];
    
    if([timestr isKindOfClass:[NSNumber class]]){
        _time.text=[self timeF:timestr.longLongValue];
    }
    else if([timestr isKindOfClass:[NSString class]]){
        _time.text=[self timeF:timestr.longLongValue];
    }
    else{
        _time.text=@"";
    }
    
    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];

    [self layoutIfNeeded];
}

-(NSString *)timeF:(NSInteger)time{
    //NSLog(@"time:%ld",(long)time);
    
    if(time<=0){
        return @"";
    }
    // 创建日期格式器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd"]; // 设置你想要的日期格式
    
    
    long long timestamp =(long)time/1000; // 例如：2021-10-01 00:00:00 UTC
    // 转换为NSDate
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSString *dateString = [formatter stringFromDate:date];
    
    
    NSDate *now = [NSDate date];
    NSTimeInterval trt = [now timeIntervalSince1970];
    date = [NSDate dateWithTimeIntervalSince1970:trt];
    NSString *tdateString = [formatter stringFromDate:date];
    
    
    NSInteger shijiancha = trt-timestamp;
    
    if(shijiancha<60){
        return NSLocalizedString(@"time-oneminint", @"");
    }
    if(shijiancha>24*3600||dateString.intValue!=tdateString.intValue){
        // 假设这是你的时间戳（以秒为单位）
        long long timestamp =(long)time/1000; // 例如：2021-10-01 00:00:00 UTC
        // 转换为NSDate
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
        // 创建日期格式器
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"]; // 设置你想要的日期格式
        // 转换为字符串
        NSString *dateString = [formatter stringFromDate:date];
        return dateString;
    }
    else{
        NSInteger xiaoshi = shijiancha/3600;
        NSInteger fenzhong = shijiancha/60;
        if(xiaoshi>0){
            // 假设这是你的时间戳（以秒为单位）
            long long timestamp =(long)time/1000; // 例如：2021-10-01 00:00:00 UTC
            // 转换为NSDate
            NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
            // 创建日期格式器
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"HH:mm"]; // 设置你想要的日期格式
            // 转换为字符串
            NSString *dateString = [formatter stringFromDate:date];
            return dateString;
        }
        else{
            return [NSString stringWithFormat:@"%ld%@",(long)fenzhong,NSLocalizedString(@"time-outmin", @"")];
        }
    }
    
    return @"";
}

@end

