//
//  KeyboardTeachDialogueCell.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/25.
//

import UIKit
import Lottie

class KeyboardTeachDialogueCell: UITableViewCell {
    
    static func reuseIdentifier() -> String {
        return "KeyboardTeachDialogueCell"
    }
    
    lazy var containerView: UIView = {
       
        let view = UIView(frame: CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: KeyboardTeachView.UX.resultCellHeight))
        view.backgroundColor = .white
        view.addFourCorner(topLeft: 14, topRight: 18, bottomLeft: 0, bottomRight: 18)
        
        return view
    }()
    
    lazy var dialogueLabel: UILabel = {
       
        let label = UILabel()
        label.isHidden = true
        label.numberOfLines = 0
        label.text = "巴拉巴拉小魔仙？"
        label.font = .systemFont(ofSize: 12)
        label.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
        label.setLineSpacing(5)
        return label
    }()
    
    lazy var loadingAnimate: LottieAnimationView = {
       
        let animate = LottieAnimationView(name: "keyboard_dialog_loading")
        animate.loopMode = .loop
        animate.backgroundColor = .clear
        return animate
    }()
    
    lazy var loadingLabel: UILabel = {

        let label = UILabel()
        label.text = "加载中..."
        label.font = .systemFont(ofSize: 12)
        label.textColor = .hexStringColor(hexString: "#000000", alpha: 0.47)
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = .clear
        
        self.contentView.addSubview(containerView)
//        containerView.snp.makeConstraints { make in
//            make.left.right.top.equalTo(0)
//            make.height.equalTo(height)
//        }
        
        containerView.addSubview(dialogueLabel)
        dialogueLabel.snp.makeConstraints { make in
            make.left.equalTo(11)
            make.right.equalTo(-10)
            make.centerY.equalToSuperview()
        }
        
        containerView.addSubview(loadingAnimate)
        containerView.addSubview(loadingLabel)
        
        loadingAnimate.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 14, height: 14))
            make.left.equalTo(10)
            make.centerY.equalToSuperview()
        }
        loadingAnimate.play()
        
        loadingLabel.snp.makeConstraints { make in
            make.left.equalTo(loadingAnimate.snp.right).offset(8)
            make.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setLoading(play: Bool) {
        
        if play {
            
            self.dialogueLabel.isHidden = true
            self.loadingAnimate.isHidden = false
            self.loadingLabel.isHidden = false
            self.loadingAnimate.play()
            
            self.containerView.frame = CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: KeyboardTeachView.UX.resultCellHeight)
            self.containerView.addFourCorner(topLeft: 14, topRight: 18, bottomLeft: 0, bottomRight: 18)
        } else {
            
            self.dialogueLabel.isHidden = false
            self.loadingAnimate.isHidden = true
            self.loadingLabel.isHidden = true
            self.loadingAnimate.pause()
        }
    }
    
    func config(content: String) {
        
        setLoading(play: false)
        
        self.dialogueLabel.text = content
        
        let height = content.heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
        self.containerView.frame = CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: height)
        self.containerView.addFourCorner(topLeft: 14, topRight: 18, bottomLeft: 0, bottomRight: 18)
    }
}
