//
//  JCFormCell.m
//  EZOpenSDKDemo
//
//  Created by JuneCheng on 2026/3/12.
//  Copyright © 2026 hikvision. All rights reserved.
//

#import "JCFormCell.h"
#import "UIImageView+WebCache.h"
#import "JCFormItem.h"
#import "JCFormArrowItem.h"

static NSString * const ID = @"JCFormCell";

@implementation JCFormCell

+ (instancetype)cellWithTableView:(UITableView *)tableView cellStyle:(UITableViewCellStyle)style {
    JCFormCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:style reuseIdentifier:ID];
    }
    return cell;
}

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

/** UI赋值 */
- (void)setItem:(JCFormItem *)item {
    _item = item;
    [self fillData];
    [self changeUI];
}

/** 数据填充 */
- (void)fillData {
    self.textLabel.textColor = self.item.titleColor;
    // 是否必填
    if (self.item.isRequired) {
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"* %@", self.item.title]];
        [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,2)];
        self.textLabel.attributedText = attrString;
    } else {
        self.textLabel.text = self.item.title;
    }
    self.detailTextLabel.text = self.item.subTitle;
    /** 左边的图片 UIImage || NSURL || URLString || ImageName */
    if ([self.item.image isKindOfClass:[UIImage class]]) {
        self.imageView.image = self.item.image;
    } else if ([self.item.image isKindOfClass:[NSURL class]]) {
        [self.imageView sd_setImageWithURL:self.item.image];
    } else if ([self.item.image isKindOfClass:[NSString class]]) {
        if ([self.item.image hasPrefix:@"http://"] || [self.item.image hasPrefix:@"https://"] || [self.item.image hasPrefix:@"file://"]) {
            NSString *imageUrl = [self.item.image stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
        } else {
            self.imageView.image = [UIImage imageNamed:self.item.image];
        }
    }
}

/** UI调整 */
- (void)changeUI {
    self.textLabel.font = self.item.titleFont;
    // title颜色在数据填充时就要设置，见fillData方法
//    self.textLabel.textColor = self.item.titleColor;
    
    self.detailTextLabel.font = self.item.subTitleFont;
    self.detailTextLabel.textColor = self.item.subTitleColor;
    self.detailTextLabel.lineBreakMode = NSLineBreakByTruncatingHead;
    if (self.item.subTitleNumberOfLines > 0) {
        self.detailTextLabel.numberOfLines = self.item.subTitleNumberOfLines;
    }
    // 右侧箭头
    if ([self.item isKindOfClass:[JCFormArrowItem class]]) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        self.accessoryType = UITableViewCellAccessoryNone;
    }
    //
    if (self.item.itemClick || [self.item isKindOfClass:[JCFormArrowItem class]]) {
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    } else {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}

@end
