//
//  ChatMsgListCell.m
//  smartRhino
//
//  Created by armin on 2019/11/2.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "ChatMsgListCell.h"
#import "NSDate+Extension.h"
#import "UIImage+GH.h"

@interface ChatMsgListCell()
@property (copy,nonatomic) NSMutableArray *chatDistoryArray;
@end
@implementation ChatMsgListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (CGFloat)configCell0Height{
    return 50;
}
+ (CGFloat)configCell1Height{
    return 80;
}
+ (CGFloat)configCell2Height{
    return 69;
}
+ (CGFloat)configCell3Height{
    return 76;
}
+ (CGFloat)configCell4Height{
    return 40;
}
+ (CGFloat)configCell5Height{
    return 76;
}

+ (ChatMsgListCell *)configCell0:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"ChatMsgListCell0";
    ChatMsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ChatMsgListCell" owner:nil options:nil] objectAtIndex:0];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cell0SearchBgView.layer.masksToBounds = YES;
    cell.cell0SearchBgView.layer.cornerRadius = 36/2.0;
    cell.cell0SearchBgView.layer.borderWidth = 0.5;
    cell.cell0SearchBgView.layer.borderColor = RGB(204, 204, 204).CGColor;
    cell.cell0MengCengView.hidden = YES;
    return cell;
}
+ (ChatMsgListCell *)configCell1:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"ChatMsgListCell1";
    ChatMsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ChatMsgListCell" owner:nil options:nil] objectAtIndex:1];
    }
    cell.rightImg.hidden = NO;
    cell.cell1RightIntoBtn.hidden = NO;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
+ (ChatMsgListCell *)configCell2:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"ChatMsgListCell2";
    ChatMsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ChatMsgListCell" owner:nil options:nil] objectAtIndex:2];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cell2RightArrowImgView.hidden = NO;
    cell.enterBtn.hidden = YES;
    return cell;
}
+ (ChatMsgListCell *)configCell3:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"ChatMsgListCell3";
    ChatMsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ChatMsgListCell" owner:nil options:nil] objectAtIndex:3];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.cell3RedBgView.layer.masksToBounds = YES;
    cell.cell3RedBgView.layer.cornerRadius = 16/2.0;
    
    cell.cell3MengCengView.hidden = YES;
 
    return cell;
}
+ (ChatMsgListCell *)configCell4:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"ChatMsgListCell4";
    ChatMsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ChatMsgListCell" owner:nil options:nil] objectAtIndex:4];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

+ (ChatMsgListCell *)configCell5:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"ChatMsgListCell5";
    ChatMsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ChatMsgListCell" owner:nil options:nil] objectAtIndex:5];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.cell5RedBgView.layer.masksToBounds = YES;
    cell.cell5RedBgView.layer.cornerRadius = 16/2.0;
     
    return cell;
}
+ (ChatMsgListCell *)configCell6:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"ChatMsgListCell6";
    ChatMsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ChatMsgListCell" owner:nil options:nil] objectAtIndex:6];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cell2RightArrowImgView.hidden = YES;
    return cell;
}
- (void)setCell2DataWithModel:(NoticeModel *)model
{
    self.cell2TitleLabel.text = model.FolderName;
    self.cell2TimeLabel.text = [NSString stringWithFormat:@"%ld",(long)model.UReadTopicCount];
}
- (void)setCell2MsgWithModel:(ChatMsgWorkModel *)model
{
    self.cell2TitleLabel.text = model.Name;
    self.cell2TimeLabel.text = [NSDate getTimeStringAutoShort2:[self isNullWithString:model.LastModifiedDate]];
}
- (void)setCell3DataWithModel:(ChatMsgListImModel *)model
{
    WS(weakSelf);
    [SDWebImageManager.sharedManager loadImageWithURL:[NSURL URLWithString:model.AvatarUrl] options:SDWebImageAvoidAutoSetImage progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        weakSelf.cell3UserImgView.image = [UIImage cutImageWithImage:image];

    }];
    self.cell3TitleLabel.text = model.Name;
    NSString * infoText = [self getUserInfoInDocument:[NSString stringWithFormat:@"%ld",(long)model.ImId]];
    if (infoText.length > 0) {
        self.cell3IntroLabel.attributedText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"[草稿]%@",infoText]];
        self.cell3IntroLabel.attributedText = [self.cell3IntroLabel subhighlightString:@"[草稿]" color:[UIColor redColor]];
    }else{
        self.cell3IntroLabel.attributedText = model.ChatText;
    }
    if (model.redNum > 0 && !model.IsDisturb) {
        self.cell3RedBgView.hidden = NO;
        self.cell3RedNumLabel.text = [NSString stringWithFormat:@"%ld",(long)model.redNum];
    }else{
        self.cell3RedBgView.hidden = YES;
    }
    self.IsTopView.hidden = !model.IsTop;
    self.cell3TimeLabel.text = [NSDate getTimeStringAutoShort2:model.LastModifiedDate];
}
- (void)setCellSearchModel:(ChatSearchModel *)model
{
    WS(weakSelf);
    [SDWebImageManager.sharedManager loadImageWithURL:[NSURL URLWithString:model.AvatarUrl] options:SDWebImageAvoidAutoSetImage progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        weakSelf.cell3UserImgView.image = [UIImage cutImageWithImage:image];

    }];
    self.cell3TitleLabel.text = model.Name;
    NSString * infoText = [self getUserInfoInDocument:[NSString stringWithFormat:@"%ld",(long)model.ImId]];
    if (infoText.length > 0) {
        self.cell3IntroLabel.hidden = NO;
        self.cell3IntroLabel.attributedText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"[草稿]%@",infoText]];
        self.cell3IntroLabel.attributedText = [self.cell3IntroLabel subhighlightString:@"[草稿]" color:[UIColor redColor]];
    }else{
        self.cell3IntroLabel.hidden = YES;
    }
    self.cell3RedBgView.hidden = YES;
    self.IsTopView.hidden = YES;
    self.cell3TimeLabel.text = [NSDate getTimeStringAutoShort2:model.LastModifiedDate];
}
- (void)setCell5DataWithModel:(ChatMsgListImModel *)model
{
    WS(weakSelf);
    [SDWebImageManager.sharedManager loadImageWithURL:[NSURL URLWithString:model.AvatarUrl] options:SDWebImageAvoidAutoSetImage progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        weakSelf.cell5UserImgView.image = [UIImage cutImageWithImage:image];

    }];
    self.cell5TitleLabel.text = model.Name;
    NSString * infoText = [self getUserInfoInDocument:[NSString stringWithFormat:@"%ld",(long)model.ImId]];
    if (infoText.length > 0) {
        self.cell5IntroLabel.attributedText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"[草稿]%@",infoText]];
        self.cell5IntroLabel.attributedText = [self.cell3IntroLabel subhighlightString:@"[草稿]" color:[UIColor redColor]];
    }else{
        self.cell5IntroLabel.attributedText = model.ChatText;
    }
    self.cell5RedBgView.hidden = YES;
    self.cell5TimeLabel.text = @"";
    self.IsTopView.hidden = !model.IsTop;
    self.cell5SelectBtn.selected = model.isSelected;
}
- (NSString *)isNullWithString:(id)value
{
    if ([value isKindOfClass:[NSNull class]]) {
        return @"";
    }else{
        if ([value isKindOfClass:[NSString class]]) {
            return value;
        }else{
            return [value stringValue];
        }
    }
}

- (void)layoutSubviews
{
    for (UIView *supView in self.subviews){
        if ([supView isKindOfClass:NSClassFromString(@"UITableViewCellReorderControl")]){
            for (UIView *view in supView.subviews)
            {
               if ([view isKindOfClass: [UIImageView class]])
                {
                    UIImageView * imagV = (UIImageView *)view;
                    [imagV setFrame:CGRectMake(0, 0, 22, 18)];
                    imagV.center = supView.center;
                    imagV.contentMode = UIViewContentModeCenter;
                    imagV.image = [UIImage imageNamed: @"list_sort"];
                    UIView * label = [UIView new];
                    label.backgroundColor = UIColorHex(0xEEEEEE);
                    [supView addSubview:label];
                    [label setFrame:CGRectMake(0, supView.frame.size.height - 0.5, supView.frame.size.width, 0.5)];
                }else{
                    [view removeFromSuperview];
                }
            }
        }
        if ([supView isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]){

        }
    }
    [super layoutSubviews];
}

- (NSString *)getUserInfoInDocument:(NSString *)ImId{
    self.chatDistoryArray = [[NSMutableArray alloc] initWithArray:USERDEFAULTSGET(@"ChatList")];
    for (NSMutableDictionary * dict in self.chatDistoryArray) {
        if ([[dict objectForKey:@"ImId"] isEqualToString:ImId]) {
            NSString * text = [dict objectForKey:@"text"];
            return text;
        }
    }
    return @"";
}


@end