//
//  LNOnlineView.swift
//  Lanu
//
//  Created by OneeChan on 2026/1/4.
//

import Foundation
import UIKit
import SnapKit


class LNOnlineView: UIView {
    private let borderColor: UIColor = .primary_3
    private let borderWidth: CGFloat = 1
    private let duration: Double = 1.2
    private let scale = 1.1
    private var animator: UIViewPropertyAnimator?
    
    private let borderLayer = CAShapeLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.cgColor
        clipsToBounds = false
        isUserInteractionEnabled = false
        
        borderLayer.borderWidth = borderWidth
        borderLayer.borderColor = borderColor.cgColor
        layer.addSublayer(borderLayer)
        
        startAnimate()
    }
    
    private func startAnimate() {
        let scaleAnim = CABasicAnimation(keyPath: "transform.scale")
        scaleAnim.fromValue = 1.0
        scaleAnim.toValue = scale
        scaleAnim.duration = duration
        
        let opacityAnim = CABasicAnimation(keyPath: "opacity")
        opacityAnim.fromValue = 1.0
        opacityAnim.toValue = 0.0
        opacityAnim.duration = duration
        
        let animGroup = CAAnimationGroup()
        animGroup.animations = [scaleAnim, opacityAnim]
        animGroup.duration = duration
        animGroup.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        animGroup.isRemovedOnCompletion = false
        animGroup.fillMode = .forwards
        animGroup.repeatCount = .infinity
        borderLayer.add(animGroup, forKey: "scaleAndFadeGroup")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layer.cornerRadius = bounds.height * 0.5
        borderLayer.frame = bounds
        borderLayer.cornerRadius = bounds.height * 0.5
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

#if DEBUG

import SwiftUI

struct LNOnlineViewPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNOnlineView()
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(50)
        }
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNOnlineViewPreview()
})
#endif
