//
//  LNSkillPhotosView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/14.
//

import Foundation
import UIKit
import SnapKit


class LNSkillPhotosView: UIView {
    private let stackView = UIStackView()
    private let scrollView = UIScrollView()
    private let voiceBar = LNSkillVoiceBarView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ detail: LNGameMateSkillDetailVO) {
        stackView.arrangedSubviews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        isHidden = detail.images.isEmpty
        let urls = detail.images
        for (index, url) in urls.enumerated() {
            let container = UIView()
            
            let imageView = UIImageView()
            imageView.layer.cornerRadius = 12
            imageView.clipsToBounds = true
            imageView.contentMode = .scaleAspectFill
            imageView.onTap { [weak self] in
                guard let self else { return }
                presentImagePreview(urls, index)
            }
            container.addSubview(imageView)
            imageView.snp.makeConstraints { make in
                make.leading.equalToSuperview()
                make.trailing.equalToSuperview().offset(detail.images.count < 2 ? 0 : -10)
                make.verticalEdges.equalToSuperview()
            }
            imageView.sd_setImage(with: URL(string: url))
            
            if index == 0, !detail.voiceBar.isEmpty {
                container.addSubview(voiceBar)
                voiceBar.snp.makeConstraints { make in
                    make.trailing.equalToSuperview().offset(-20)
                    make.bottom.equalToSuperview().offset(-10)
                }
                voiceBar.setVoice(detail.voiceBar)
            }
            
            stackView.addArrangedSubview(container)
            container.snp.makeConstraints { make in
                make.size.equalTo(stackView.superview!)
            }
        }
        scrollView.snp.updateConstraints { make in
            make.trailing.equalToSuperview().offset(detail.images.count < 2 ? 0 : -23)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillPhotosView {
    private func setupViews() {
        clipsToBounds = false
        
        scrollView.isPagingEnabled = true
        scrollView.clipsToBounds = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalToSuperview()
            make.trailing.equalToSuperview().offset(-23)
            make.height.equalTo(scrollView.snp.width).multipliedBy(193.0/343.0)
        }
        
        let fakeView = UIView()
        scrollView.addSubview(fakeView)
        fakeView.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
            make.height.equalToSuperview()
            make.width.equalTo(0)
        }
        
        stackView.axis = .horizontal
        stackView.spacing = 0
        scrollView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}
