//
//  LNOrderRefundInfoView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/23.
//

import Foundation
import UIKit
import SnapKit


class LNOrderRefundInfoView: UIView {
    private let reasonLabel = UILabel()
    private let photosView = LNMultiLineStackView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ detail: LNOrderDetailResponse) {
        isHidden =  detail.reason.isEmpty 
        reasonLabel.text = detail.reason
        var photos: [UIImageView] = []
        for (index, attachment) in detail.attachments.enumerated() {
            let imageView = UIImageView()
            imageView.sd_setImage(with: URL(string: attachment.attachmentUrl))
            photos.append(imageView)
            imageView.snp.makeConstraints { make in
                make.width.height.equalTo(100)
            }
            imageView.onTap { [weak self] in
                guard let self else { return }
                presentImagePreview(detail.attachments.map({ $0.attachmentUrl }), index)
            }
        }
        photosView.update(photos)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderRefundInfoView {
    private func setupViews() {
        backgroundColor = .fill
        layer.cornerRadius = 12
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00146")
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(16)
        }
        
        let holder = UIView()
        holder.backgroundColor = .fill_1
        holder.layer.cornerRadius = 12
        addSubview(holder)
        holder.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
        }
        
        reasonLabel.font = .body_m
        reasonLabel.textColor = .text_5
        reasonLabel.numberOfLines = 0
        holder.addSubview(reasonLabel)
        reasonLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.verticalEdges.equalToSuperview().inset(10)
        }
        
        let photoLabel = UILabel()
        photoLabel.text = .init(key: "A00147")
        photoLabel.font = .heading_h4
        photoLabel.textColor = .text_5
        addSubview(photoLabel)
        photoLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalTo(holder.snp.bottom).offset(16)
        }
        
        photosView.columns = 3
        photosView.itemDistribution = .equalSpacing
        addSubview(photosView)
        photosView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().offset(-16)
            make.top.equalTo(photoLabel.snp.bottom).offset(8)
        }
    }
}
