//
//  LNMultiImagesUploadView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/12.
//
import Foundation
import UIKit
import SnapKit


protocol LNMultiImagesUploadViewDelegate: AnyObject {
    func onMultiImagesUploadView(view: LNMultiImagesUploadView, imageUrlsChanged urls: [String])
}


class LNMultiImagesUploadView: UIView {
    var maxPhoto = 6
    private let photoView = LNMultiLineStackView()
    private let addImageButton = UIButton()
    var uploadType: LNUploadFileType = .feedback
    weak var delegate: LNMultiImagesUploadViewDelegate?
    
    var curFileUrls: [String] {
        photoView.curItemViews.compactMap {
            ($0 as? LNImageUploadView)?.imageUrl
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNMultiImagesUploadView: LNImageUploadViewDelegate {
    func onImageUploadView(view: LNImageUploadView, didUploadImage url: String) {
        delegate?.onMultiImagesUploadView(view: self, imageUrlsChanged: curFileUrls)
    }
    
    func onImageUploadViewDidClickDelete(view: LNImageUploadView) {
        photoView.remove([view])
        if !photoView.curItemViews.contains(addImageButton) {
            photoView.append([addImageButton])
        }
        
        delegate?.onMultiImagesUploadView(view: self, imageUrlsChanged: curFileUrls)
    }
}

extension LNMultiImagesUploadView {
    private func buildImageView() -> LNImageUploadView {
        let imageView = LNImageUploadView()
        imageView.uploadType = .other
        imageView.layer.cornerRadius = 10
        imageView.clipsToBounds = true
        imageView.showClearButton = true
        imageView.delegate = self
        imageView.snp.makeConstraints { make in
            make.width.height.equalTo(100)
        }
        
        return imageView
    }
    
    private func setupViews() {
        photoView.columns = 3
        photoView.spacing = 4
        photoView.itemDistribution = .equalSpacing
        addSubview(photoView)
        photoView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(0).priority(.low)
        }
        
        let config = UIImage.SymbolConfiguration(pointSize: 17)
        addImageButton.backgroundColor = .fill_1
        addImageButton.layer.cornerRadius = 8.33
        addImageButton.setImage(.init(systemName: "plus", withConfiguration: config), for: .normal)
        addImageButton.tintColor = .text_3
        addImageButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            LNBottomSheetMenu.showImageSelectMenu(view: self) { [weak self] image, _ in
                guard let self else { return }
                guard let image = image?.compress(type: .photoWall) else { return }
                var curViews = photoView.curItemViews
                let imageView = buildImageView()
                imageView.uploadImage(image: image)
                curViews.insert(imageView, at: 0)
                if curViews.count > maxPhoto {
                    curViews.removeAll { $0 == self.addImageButton }
                }
                photoView.update(curViews)
            }
        }), for: .touchUpInside)
        addImageButton.snp.makeConstraints { make in
            make.width.height.equalTo(100)
        }
        photoView.update([addImageButton])
    }
}
