//
//  TipsView.swift
//  THUD
//
//  Created by TSYH on 2024/4/11.
//

import UIKit
import SnapKit
//import ExtensionsKit

public enum TipsViewStyle {
    case normal
    case success
    case warning
}

public struct TipsViewConfig: THUDProtocol {
    public var iconName: String?
    public var message: String?
    public var background: UIColor?
    public var textColor: UIColor?
    public var duration: TimeInterval = 0
    
    public init() {
        
    }
    
    // MARK: THUDProtocol
    public var hudView: THUDViewProtocol {
        let v = TipsView()
        v.config = self
        return v
    }
    
    public var hudDuration: TimeInterval? {
        return duration
    }
}

class TipsView: UIView, THUDViewProtocol {
    lazy var iconView = UIImageView()
    lazy var messageLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        customCornerRadius = 12
        
        messageLabel.numberOfLines = 0
        messageLabel.font = UIFont.systemFont(ofSize: 14)
        messageLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        
        let stackView = UIStackView(arrangedSubviews: [iconView, messageLabel])
        stackView.axis = .horizontal
        stackView.spacing = 10
        stackView.alignment = .center
        stackView.distribution = .fill
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(16)
            make.bottom.equalTo(-16)
            make.width.lessThanOrEqualTo(280)
        }
        iconView.snp.makeConstraints { make in
            make.width.height.equalTo(24)
        }
    }
    
    func setShadow() {
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.1
        layer.shadowRadius = 4
        layer.shadowOffset = CGSize(width: 0, height: 2)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var hudIdentifier: String? {
        return messageLabel.text
    }
    
    lazy var config: TipsViewConfig = TipsViewConfig() {
        didSet {
            updateUI(config: config)
        }
    }
    
    private func updateUI(config: TipsViewConfig) {
        iconView.isHidden = true
        if let imageName = config.iconName {
            iconView.isHidden = false
            iconView.image = UIImage(named: imageName)
        }
        backgroundColor = config.background
        messageLabel.textColor = config.textColor
        setShadow()
        messageLabel.text = config.message
    }
}

extension TipsViewStyle {
    func tipsConfig(message: String) -> TipsViewConfig {
        var config = TipsViewConfig()
        config.message = message
        config.duration = 5.0
        switch self {
        case .normal:
            config.background = UIColor.black.withAlphaComponent(0.6)
            config.textColor = .white
        case .success:
            config.background = .white
            config.textColor = .black
            config.iconName = "ic-hud-tip-success"
        case .warning:
            config.background = .white
            config.textColor = .black
            config.iconName = "ic-hud-tip-warning"
        }
        return config
    }
}
