//
//  LNLanguageSettingPanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/17.
//

import Foundation
import UIKit
import SnapKit


class LNLanguageSettingPanel: LNPopupView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNLanguageSettingPanel {
    private func setupViews() {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 0
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
        
        var itemViews: [UIView] = []
        LNAppLanguage.available.forEach {
            itemViews.append(buildLanguageItem($0))
        }
        
        for index in (1..<itemViews.count).reversed() {
            itemViews.insert(buildLine(), at: index)
        }
        itemViews.forEach {
            stackView.addArrangedSubview($0)
        }
    }
    
    private func buildLanguageItem(_ language: LNAppLanguage) -> UIView {
        let container = UIView()
        
        let checkIc = UIImageView()
        checkIc.image = language.isSelected ? .icCheck : .icUncheck
        container.addSubview(checkIc)
        checkIc.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = language.text
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(12)
            make.bottom.equalToSuperview().offset(-12)
            make.trailing.lessThanOrEqualTo(checkIc.snp.leading).offset(-5)
        }
        
        container.onTap { [weak self] in
            guard let self else { return }
            LNAppConfig.shared.curLang = language
            self.dismiss()
        }
        
        return container
    }
    
    private func buildLine() -> UIView {
        let container = UIView()
        
        let line = UIView()
        line.backgroundColor = .fill_2
        container.addSubview(line)
        line.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.height.equalTo(1)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        return container
    }
}


#if DEBUG

import SwiftUI

struct LNLanguageSettingPanelPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNLanguageSettingPanel()
        view.popup(container)
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNLanguageSettingPanelPreview()
})
#endif

