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

#import "JCTextViewFormCell.h"
#import "PlaceholderTextView.h"

#import "JCFormItem.h"

static NSString * const ID = @"JCTextViewFormCell";

@interface JCTextViewFormCell()

@property (nonatomic, strong) UILabel *lab_title;
@property (nonatomic, strong) PlaceholderTextView *tv_value;

@end

@implementation JCTextViewFormCell

+ (instancetype)cellWithTableView:(UITableView *)tableView cellStyle:(UITableViewCellStyle)style {
    JCTextViewFormCell *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) {
        [self setUI];
        [self setLayout];
    }
    return self;
}

/** 创建UI */
- (void)setUI {
    _lab_title = [[UILabel alloc] init];
    [self.contentView addSubview:_lab_title];
    
    _tv_value = [[PlaceholderTextView alloc] init];
    _tv_value.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_tv_value];
}

/** 设置约束 */
- (void)setLayout {
    [_lab_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self).offset(7);
    }];
    [_tv_value mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_lab_title.mas_bottom);
        make.bottom.equalTo(self).offset(-7);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
    }];
}

/** 输入框监听 */
- (void)textFieldDidChange {
    self.item.subTitle = _tv_value.text;
    NSLog(@"textFieldDidChange--->%@",@"TextView");
}

/** UI赋值 */
- (void)setItem:(JCFormItem *)item {
    if (!_item) {
        _item = item;
        [kNotificationCenter addObserver:self selector:@selector(textFieldDidChange) name:UITextViewTextDidChangeNotification object:nil];
    } else {
        _item = item;
    }
    [self fillData];
    [self changeUI];
}

/** 数据填充 */
- (void)fillData {
    self.lab_title.textColor = self.item.titleColor;
    self.tv_value.text = self.item.subTitle;
    // 是否必填
    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.lab_title.attributedText = attrString;
    } else {
        self.lab_title.text = self.item.title;
    }
}

/** UI调整 */
- (void)changeUI {
    self.lab_title.font = self.item.titleFont;
    
    self.tv_value.placeholder = [NSString stringWithFormat:@"请输入%@", self.item.title];
    self.tv_value.font = self.item.subTitleFont;
    self.tv_value.textColor = self.item.subTitleColor;
}

@end
