//
//  HeartLikeCountView.swift

//
//  Created by Leon on 2025/7/25.
//

import UIKit
import SnapKit // Assuming SnapKit is used for layout instead of Masonry
import Lottie // Assuming Lottie is used for animations

enum HeartLikeCountSize {
    case small
    // h: 用在Album
    case medium
    case large
    // wh: 44x44
    case xl
    // wh: 48x48
    case xxl
}

/// 默认创建: Medium
class HeartLikeCountView: UIView {
    
    // MARK: - Properties
    
    let countLabel: UILabel
    let countImageView: UIImageView
    let likeButton: UIButton
    let lotiView: LottieAnimationView
    let contentStackView: UIStackView
    
    var viewSize: HeartLikeCountSize = .medium
    
    var isLike: Bool = false {
        didSet {
            let code = isLike ? IconCode.likeFill : IconCode.like
            let color: UIColor = isLike ? UIColor.c.cin : UIColor.white
            
            // 根据尺寸配置图标大小
            let iconSize = getIconSize()
            let image = MWIconFont.image(fromIcon: code, size: iconSize, color: color)
            countImageView.image = image
        }
    }
    
    // MARK: - Initialization
    
    init(viewSize: HeartLikeCountSize = .medium) {
        self.viewSize = viewSize
        countLabel = UILabel()
        countImageView = UIImageView()
        likeButton = UIButton(type: .custom)
        lotiView = LottieAnimationView(name: "like_motion")
        lotiView.contentMode = .scaleAspectFit
        contentStackView = UIStackView()
        
        super.init(frame: .zero)
        
        setupUI()
        setupTheme()
        setupSize()
    }
    
    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Setup Methods
    
    private func setupTheme() {
        countLabel.textColor = .c.ctpn
    }
    
    private func setupUI() {
        backgroundColor = .c.csedn
        
        // Setup contentStackView
        addSubview(contentStackView)
        contentStackView.axis = .horizontal
        contentStackView.alignment = .center
        contentStackView.distribution = .fill
        
        // Setup countImageView
        countImageView.contentMode = .scaleAspectFill
        contentStackView.addArrangedSubview(countImageView)
        
        // Setup countLabel
        contentStackView.addArrangedSubview(countLabel)
        
        // Setup lotiView
        addSubview(lotiView)
        lotiView.isHidden = true
        
        // Setup likeButton
        addSubview(likeButton)
        likeButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        isLike = false
    }
    
    private func setupSize() {
        let sizeConfig = getSizeConfig()
        
        self.snp.makeConstraints { make in
            make.height.equalTo(sizeConfig.cornerRadius * 2)
        }
        // 设置整体圆角
        layer.cornerRadius = sizeConfig.cornerRadius
        
        // 设置 contentStackView 约束
        contentStackView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(sizeConfig.horizontalPadding)
            make.trailing.equalToSuperview().offset(-sizeConfig.horizontalPadding)
            make.centerY.equalToSuperview()
        }
        
        // 设置图标大小
        countImageView.snp.makeConstraints { make in
            make.size.equalTo(sizeConfig.iconSize)
        }
        
        // 设置 StackView 间距
        contentStackView.spacing = sizeConfig.iconTextSpacing
        
        // 设置字体
        countLabel.font = sizeConfig.font
        
        // 设置 Lottie 动画视图约束
        lotiView.snp.makeConstraints { make in
            make.centerX.equalTo(countImageView)
            make.centerY.equalTo(countImageView)
            make.size.equalTo(sizeConfig.lottieSize)
        }
    }
    
    // MARK: - Size Configuration
    
    private struct SizeConfig {
        let iconSize: CGSize
        let font: UIFont
        let cornerRadius: CGFloat
        let horizontalPadding: CGFloat
        let iconTextSpacing: CGFloat
        let lottieSize: CGSize
    }
    
    private func getSizeConfig() -> SizeConfig {
        switch viewSize {
        case .small:
            return SizeConfig(
                iconSize: CGSize(width: 12, height: 12),
                font: .t.tls,
                cornerRadius: 12,
                horizontalPadding: 8,
                iconTextSpacing: 3,
                lottieSize: CGSize(width: 24, height: 24)
            )
        case .medium:
            return SizeConfig(
                iconSize: CGSize(width: 16, height: 16),
                font: .t.tlm,
                cornerRadius: 16,
                horizontalPadding: 12,
                iconTextSpacing: 4,
                lottieSize: CGSize(width: 30, height: 30)
            )
        case .large:
            return SizeConfig(
                iconSize: CGSize(width: 20, height: 20),
                font: .t.tll,
                cornerRadius: 20,
                horizontalPadding: 16,
                iconTextSpacing: 6,
                lottieSize: CGSize(width: 36, height: 36)
            )
        case .xl:
            return SizeConfig(
                iconSize: CGSize(width: 20, height: 20),
                font: .t.tll,
                cornerRadius: 20,
                horizontalPadding: 12,
                iconTextSpacing: 8,
                lottieSize: CGSize(width: 44, height: 44)
            )
        case .xxl:
            return SizeConfig(
                iconSize: CGSize(width: 24, height: 24),
                font: .t.tll,
                cornerRadius: 24,
                horizontalPadding: 32,
                iconTextSpacing: 8,
                lottieSize: CGSize(width: 48, height: 48)
            )
            
        }
    }
    
    private func getIconSize() -> CGSize {
        return getSizeConfig().iconSize
    }
    
    // MARK: - Lazy Properties
    
    private func configureCountLabel() -> UILabel {
        let label = UILabel()
        let typography = CLSystemToken.typography(token: .tnms)
        label.font = typography.font
        return label
    }
    
    private func configureCountImageView() -> UIImageView {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        return imageView
    }
    
    private func configureLikeButton() -> UIButton {
        return UIButton(type: .custom)
    }
    
    private func configureLotiView() -> LottieAnimationView {
        let view = LottieAnimationView(name: "like_motion")
        view.isHidden = true
        return view
    }
    
    // MARK: - Public
    
    func purIconStyle(){
        backgroundColor = .clear
        countLabel.isHidden = true
    }
    
    func playLotteLike(_ completion:((Bool)->Void)?){
        lotiView.isHidden = false
        countImageView.alpha = 0  // 使用 alpha 而不是 isHidden
        lotiView.play {[weak self] completed in
            self?.lotiView.isHidden = true
            self?.countImageView.alpha = 1  // 恢复显示
            completion?(completed)
        }
    }
    
    func bind(aiAlbum: AlbumPhotoItem?){
        guard let photo = aiAlbum else{return}
        
        isLike = photo.likedStatus == .liked
        let likedCount = photo.likedCount ?? 0
        let likeCountDisplay = String.displayNumber(NSNumber(value: likedCount), scale: 1)
        countLabel.text = likeCountDisplay
    }
}
