//
//  RelationshipTag.swift

//
//  Created by Leon on 2025/8/13.
//

import UIKit

enum RelationshipType{
    case meet
    case friend
    case flirting
    case couple
    case married
    
    var localizedText: String {
        switch self {
        case .meet:
            return "Meet"
        case .friend:
            return "Friend"
        case .flirting:
            return "Flirting"
        case .couple:
            return "Couple"
        case .married:
            return "Married"
        }
    }
}

enum RelationshipTagSize{
    case small
    case medium
    case large
}

class RelationshipTag: UIView {
    var effectView: UIVisualEffectView!
    var label: UILabel!
    var gradientBg:GradientView!
    
    // MARK: Config
    var heartBeatVal: CGFloat = 0
    var type: RelationshipType = .meet {
        didSet {
            setupData()
        }
    }
    var tagSize: RelationshipTagSize = .medium
    var tagHeight = 24.0
    var tagFont = UIFont.t.tls
    var lrPadding = 4.0
    var isShowRelation: Bool = false

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupData()
        setupEvent()
    }
    
    init(type:RelationshipType = .meet, size:RelationshipTagSize = .medium){
        super.init(frame: .zero)
        self.type = type
        tagSize = size
        
        setupViews()
        setupData()
        setupEvent()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        layer.cornerRadius = 4
        layer.masksToBounds = true
        
        //❓是否需要，因为底部可能是有透明
        effectView = {
            let v = UIVisualEffectView(effect: UIBlurEffect(style: .light))
            v.alpha = 0.5
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
    
        switch tagSize {
        case .small:
            tagHeight = 16
            tagFont = .t.tls
            lrPadding = 2.0
        case .medium:
            tagHeight = 24
            tagFont = .t.tls
            lrPadding = 4.0
        case .large:
            tagHeight = 32
            tagFont = .t.tlm
            lrPadding = 8.0
        }
        
        snp.makeConstraints { make in
            make.height.equalTo(tagHeight)
        }
        
        gradientBg = {
            let gradient = CLSystemToken.gradient(token: .cpgn)
            let v = GradientView(colors: gradient.colors(), gradientType: .leftToRight)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            v.isHidden = true
            return v
        }()
        
        label = {
            let v = UILabel()
            v.font = tagFont
            v.textColor = .c.ctpn
            v.setContentCompressionResistancePriority(UILayoutPriority(756), for: .horizontal)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(lrPadding)
                make.trailing.equalToSuperview().offset(-lrPadding)
                make.centerY.equalToSuperview()
            }
            return v
        }()
    }
    
    private func setupData(){
        
    }   
    
    private func setupEvent(){
        
    }
    
    // MARK: - Helper
    private func refreshViewsShow(){
        gradientBg.isHidden = true
        
        label.text = getShowStringBy(type.localizedText)
        
        switch type {
        case .meet:
            backgroundColor = .c.csen
        case .friend:
            backgroundColor = CLGlobalToken.color(token: .glo_color_violet_40)?.withAlphaComponent(0.45)
        case .flirting:
            backgroundColor = CLGlobalToken.color(token: .glo_color_orange_50)?.withAlphaComponent(0.45)
        case .couple:
            backgroundColor = CLGlobalToken.color(token: .glo_color_magenta_50)?.withAlphaComponent(0.45)
        case .married:
            backgroundColor = .clear
            gradientBg.isHidden = false
        }
    }
    
    private func getShowStringBy(_ text: String) -> String{
        let val = heartBeatVal
        
        let formatVal = val.formatted(decimal: 1, usesGroupingSeparator: false)
        if val > 0 && isShowRelation{
           let array = [text, "\(formatVal)℃"]
            return array.joined(separator: " · ")
        }else if val > 0{
            return "\(formatVal)℃"
        }
        else{
            return text
        }
    }
    
    // MARK: - Public
    func bind(level: HeartbeatLevel?, heartBeatVal: CGFloat? = 0, isShow: Bool? = false){
        guard let heartLevel = level else{
            isHidden = true
            return
        }
        isHidden = false
        self.isShowRelation = isShow ?? false
        
        self.heartBeatVal = heartBeatVal ?? 0.0
        
        type = heartLevel.relationType
        
        refreshViewsShow()
    }
}
