//
//  LNJoinUsPhotoExamplePanel.swift
//  Gami
//
//  Created by OneeChan on 2026/1/22.
//

import Foundation
import UIKit
import SnapKit


class LNJoinUsPhotoExamplePanel: LNPopupView {
    private let titleLabel = UILabel()
    private let descLabel = UILabel()
    
    private let imageView = UIImageView()
    
    private var curItem: LNSkillFieldExample?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ example: LNSkillFieldExample) {
        titleLabel.text = example.title
        descLabel.text = example.desc
        
        imageView.sd_setImage(with: URL(string: example.value))
        { [weak self] image, _, _, _ in
            guard let self else { return }
            guard let image else { return }
            imageView.snp.makeConstraints { make in
                make.height.equalTo(self.imageView.snp.width)
                    .multipliedBy(image.size.height / image.size.width).priority(.high)
            }
        }
        
        curItem = example
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNJoinUsPhotoExamplePanel {
    private func setupViews() {
        let headerView = buildHeader()
        container.addSubview(headerView)
        headerView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        imageView.layer.cornerRadius = 12
        imageView.clipsToBounds = true
        imageView.contentMode = .scaleAspectFit
        imageView.onTap { [weak self] in
            guard let self else { return }
            guard let curItem else { return }
            presentImagePreview([curItem.value], 0)
        }
        container.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(32)
            make.top.equalTo(headerView.snp.bottom)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(imageView.snp.width).multipliedBy(159.0 / 311.0).priority(.low)
            make.height.lessThanOrEqualTo(UIScreen.main.bounds.height * 0.5 - 70)
        }
    }
    
    private func buildHeader() -> UIView {
        let container = UIView()
        
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.numberOfLines = 0
        titleLabel.textAlignment = .center
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(32)
            make.top.equalToSuperview().offset(13)
        }
        
        descLabel.font = .body_xs
        descLabel.textColor = .text_3
        descLabel.numberOfLines = 0
        descLabel.textAlignment = .center
        container.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(32)
            make.bottom.equalToSuperview().offset(-13)
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
        }
        
        return container
    }
}
