//
//  LNAboutViewController.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/22.
//

import Foundation
import UIKit
import SnapKit


extension UIView {
    func pushToAbout() {
        let vc = LNAboutViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}


class LNAboutViewController: LNViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
    }
}

extension LNAboutViewController {
    private func setupViews() {
        title = .init(key: "A00247")
        view.backgroundColor = .primary_1
        
        let icon = UIImageView()
        icon.image = getAppIcon()
        icon.layer.cornerRadius = 16
        icon.clipsToBounds = true
        view.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(40)
            make.width.height.equalTo(72)
        }
        
        let nameIc = UIImageView()
        nameIc.image = .icGami
        view.addSubview(nameIc)
        nameIc.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(icon.snp.bottom).offset(7)
        }
        
        var versionStr: String = .init(key: "A00248", curAppVersion)
        
#if DEBUG
        versionStr += "(\(curBuildVersion))"
#endif
        
        let versionLabel = UILabel()
        versionLabel.text = versionStr
        versionLabel.font = .body_s
        versionLabel.textColor = .text_5
        view.addSubview(versionLabel)
        versionLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(nameIc.snp.bottom).offset(13)
        }
        
        let infoView = UIView()
        infoView.backgroundColor = .fill
        infoView.layer.cornerRadius = 12
        view.addSubview(infoView)
        infoView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(versionLabel.snp.bottom).offset(40)
        }
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 26
        infoView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(16)
        }
        
        let privacy = buildFunctionItem(title: .init(key: "A00249"), infoView: nil)
        privacy.onTap { [weak self] in
            guard let self else { return }
            view.pushToWebView(LNJumpWebViewConfig.normalConfig(url: .privacyUrl))
        }
        stackView.addArrangedSubview(privacy)
        
        let service = buildFunctionItem(title: .init(key: "A00250"), infoView: nil)
        service.onTap { [weak self] in
            guard let self else { return }
            view.pushToWebView(LNJumpWebViewConfig.normalConfig(url: .serviceUrl))
        }
        stackView.addArrangedSubview(service)
        
        let social = buildFunctionItem(title: .init(key: "A00251"), infoView: nil)
        social.onTap { [weak self] in
            guard let self else { return }
            view.pushToWebView(LNJumpWebViewConfig.normalConfig(url: .communityUrl))
        }
        stackView.addArrangedSubview(social)
        
        let newVersion = LNNewVersionView()
        newVersion.isHidden = !LNAppConfig.shared.hasNewVersion
        let version = buildFunctionItem(title: .init(key: "A00252"), infoView: newVersion)
        version.onTap {
            if LNAppConfig.shared.hasNewVersion {
                LNAppConfig.shared.jumpToAppStore()
            } else {
                showToast(.init(key: "A00253"))
            }
        }
        stackView.addArrangedSubview(version)
    }
    
    private func buildFunctionItem(title: String, infoView: UIView?) -> UIView {
        let container = UIView()
        
        let arrow = UIImageView.arrowImageView(size: 14)
        arrow.tintColor = .text_4
        container.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        if let infoView {
            container.addSubview(infoView)
            infoView.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.trailing.equalTo(arrow.snp.leading).offset(-2)
            }
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .body_s
        titleLabel.textColor = .text_5
        titleLabel.text = title
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
            make.trailing.lessThanOrEqualTo((infoView ?? arrow).snp.leading)
        }
        
        return container
    }
    
    func getAppIcon() -> UIImage? {
        guard let infoPlist = Bundle.main.infoDictionary,
              let iconDict = infoPlist["CFBundleIcons"] as? [String: Any],
              let primaryIcon = iconDict["CFBundlePrimaryIcon"] as? [String: Any],
              let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
              let lastIconName = iconFiles.last else {
            return nil
        }
        
        return UIImage(named: lastIconName)
    }
}
