//
//  AvatarView.swift

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

import UIKit

class AvatarView: UIView {
    // MARK: - Properties
    var avatar: CLAnimatedImage!
    var cameraCircle: UIView!
    var cameraIcon: UIImageView!
    var indicator: IndicatorView!
    var topButton: UIButton!
    
    var tapAction: (() -> Void)?
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Setup
    private func setupViews() {
        avatar = {
            let imageView = CLAnimatedImage(frame: .zero)
            imageView.backgroundColor = .c.csen
            addSubview(imageView)
            imageView.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.centerY.equalToSuperview()
                //make.size.equalTo(CGSize(width: 80, height: 80))
                make.edges.equalToSuperview()
            }
            return imageView
        }()

        cameraCircle = {
            let view = UIView()
            view.backgroundColor = .c.csfn
            view.layer.cornerRadius = 16
            view.layer.masksToBounds = true
            view.layer.borderColor = UIColor.c.cbd.cgColor
            view.layer.borderWidth = 2
            addSubview(view)
            view.snp.makeConstraints { make in
                make.trailing.equalTo(avatar.snp.trailing).offset(4)
                make.bottom.equalTo(avatar.snp.bottom).offset(-2)
                make.size.equalTo(CGSize(width: 32, height: 32))
            }
            view.isHidden = true
            return view
        }()

        cameraIcon = {
            let imageView = UIImageView()
            imageView.image = MWIconFont.image(fromIcon: .iconCameraFill, size: CGSizeMake(16, 16), color: .white)
            imageView.contentMode = .scaleAspectFit
            cameraCircle.addSubview(imageView)
            imageView.snp.makeConstraints { make in
                make.center.equalToSuperview()
                make.size.equalTo(CGSize(width: 16, height: 16))
            }
            return imageView
        }()
        
        indicator = {
            let v = IndicatorView(color: .text)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.center.equalTo(avatar)
            }
            v.isHidden = true
            return v
        }()

        topButton = {
            let button = UIButton()
            addSubview(button)
            button.snp.makeConstraints { make in
                make.size.equalTo(CGSizeMake(90, 90))
                make.center.equalTo(avatar)
            }
            button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
            return button
        }()
    }
    
    // MARK: - Actions
    @objc private func buttonTapped() {
        tapAction?()
    }
    
    // MARK: - Public Methods
    func bindImage(img: UIImage?, showUploading: Bool = false) {
        avatar.image = img
        
        if showUploading {
            indicator.isHidden = false
            indicator.startIndicator()
        } else {
            indicator.isHidden = true
            indicator.stopIndicator()
        }
    }
    
    func bindImageUrl(imgUrl: String?) {
        avatar.loadImage(imgUrl)
        indicator.stopIndicator()
        indicator.isHidden = true
    }
    
    func setTapAction(_ action: @escaping () -> Void) {
        tapAction = action
    }
}
