//
//  KeyboardCharacterCell.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/23.
//

import UIKit
import Lottie
import MarqueeLabel

class KeyboardCharacterCell: UICollectionViewCell {
    
    static func reuseIdentifier() -> String {
        return "KeyboardCharacterCell"
    }
    
    var characterModel: CharacterModel?
    
    lazy var containerView: UIView = {
        
        let view = UIView()
        view.layer.cornerRadius = 9
        view.backgroundColor = .white
        return view
    }()
    
    lazy var titleLabel: MarqueeLabel = {
       
        let label = MarqueeLabel(frame: .zero, duration: 3.0, fadeLength: 0)
        label.type = .leftRight
        label.textAlignment = .center
        label.text = "💑 恋爱脑"
        label.font = .systemFont(ofSize: 12)
        label.textColor = .hexStringColor(hexString: "#000000").withAlphaComponent(0.8)
        return label
    }()
    
    lazy var addBtn: UIButton = {
       
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: KeyboardHelpView.UX.cellWidth, height: KeyboardHelpView.UX.cellHeight))
        btn.isHidden = true
        btn.setTitle("添加人设", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12)
        btn.setTitleColor(.hexStringColor(hexString: "#7D46FC"), for: .normal)
        btn.setImage(UIImage(named: "icon_plus"), for: .normal)
        btn.setImageTitleLayout(.imgLeft, spacing: 3)
        return btn
    }()
    
    lazy var loadingAnimate: LottieAnimationView = {
       
        let animate = LottieAnimationView(name: "keyboard_loading")
        animate.isHidden = true
        animate.loopMode = .loop
        animate.backgroundColor = .clear
        return animate
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.addSubview(containerView)
        containerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.containerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(4)
            make.right.equalTo(-4)
        }
        
        self.containerView.addSubview(addBtn)
        addBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.containerView.addSubview(loadingAnimate)
        loadingAnimate.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 32, height: 32))
            make.center.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func config(model: CharacterModel) {
        self.characterModel = model
        if let emoji = model.emoji, let name = model.name {
            self.titleLabel.text = "\(emoji) \(name)"
        }
    }
    
    func config(content: String) {
        self.titleLabel.text = "\(content)"
    }
    
    // 设置动画
    func setAnimate(play: Bool) {
        
        if play {
            self.titleLabel.isHidden = true
            self.loadingAnimate.isHidden = false
            self.loadingAnimate.play()
        } else {
            self.titleLabel.isHidden = false
            self.loadingAnimate.isHidden = true
            self.loadingAnimate.pause()
        }
    }
}
