//
//  YHHKRecordsRemarkCell.swift
//  galaxy
//
//  Created by alexzzw on 2024/9/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHHKRecordsRemarkCell: YHResignDocumentCell {

    static let cellReuseIdentifier = "YHHKRecordsRemarkCell"
    
    var textViewDidChanged: ((UITextView) -> Void)?
    
    private lazy var infoTitleLabel: UILabel = {
        let label = UILabel()
        label.font = .PFSC_R(ofSize: 14)
        label.textColor = .mainTextColor50
        return label
    }()
    
    private lazy var remarkTextView: UITextView = {
        let view = UITextView()
        view.font = .PFSC_R(ofSize: 14)
        view.textColor = .mainTextColor
        view.tintColor = .mainTextColor
        view.backgroundColor = .contentBkgColor
        view.font = UIFont.systemFont(ofSize: 14)
        view.returnKeyType = .default
        view.enablesReturnKeyAutomatically = true
        view.delegate = self
        view.addObserver(self, forKeyPath: "attributedText", options: .new, context: nil)
        view.textContainerInset = UIEdgeInsets.zero
        view.textContainer.lineFragmentPadding = 0
        return view
    }()
    
    private lazy var editTipsLabel: UILabel = {
        let label = UILabel()
        label.isHidden = true
        label.text = "请输入备注内容"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = .mainTextColor30
        return label
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupCellInfo(title: String? = "备注：", titleColor: UIColor = .mainTextColor50,  detail: String?, isEditable: Bool = false, cellType: YHResignRoundCellType = .mid) {
        updateCellCorner(cellType)
        infoTitleLabel.text = title
        remarkTextView.isEditable = isEditable
        remarkTextView.text = detail
        editTipsLabel.isHidden = !isEditable || (detail?.count ?? 0) > 0
    }
}

extension YHHKRecordsRemarkCell {
    
    private func setupUI() {
        subContainerView.addSubview(infoTitleLabel)
        subContainerView.addSubview(remarkTextView)
        subContainerView.addSubview(editTipsLabel)
        
        infoTitleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18)
            make.top.equalToSuperview().offset(16)
        }
        remarkTextView.snp.makeConstraints { make in
            make.left.equalTo(infoTitleLabel)
            make.right.equalToSuperview().offset(18)
            make.height.equalTo(88)
            make.top.equalTo(infoTitleLabel.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(-16)
        }
        editTipsLabel.snp.makeConstraints { make in
            make.left.equalTo(remarkTextView.snp.left).offset(12)
            make.top.equalTo(remarkTextView.snp.top).offset(10)
        }
    }
}

extension YHHKRecordsRemarkCell: UITextViewDelegate {
    
    func textViewDidChange(_ textView: UITextView) {
        editTipsLabel.isHidden = textView.text.count > 0
        textViewDidChanged?(textView)
    }
}
