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

import Foundation
import UIKit
import SnapKit


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


class LNSettingsViewController: LNViewController {
    private let curLanguageLabel = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
    }
}

extension LNSettingsViewController {
    private func setupViews() {
        title = .init(key: "A00211")
        view.backgroundColor = .primary_1
        
        let functionView = UIView()
        functionView.backgroundColor = .fill
        functionView.layer.cornerRadius = 12
        view.addSubview(functionView)
        functionView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(22)
        }
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 0
        functionView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.verticalEdges.equalToSuperview().inset(3)
        }
        
        let language = buildFunctionItem(icon: .icLanguage18, title: .init(key: "A00256"), infoView: curLanguageLabel)
        curLanguageLabel.font = .body_s
        curLanguageLabel.textColor = .text_5
        curLanguageLabel.text = LNAppConfig.shared.curLang.text
        language.onTap {
            let panel = LNLanguageSettingPanel()
            panel.popup()
        }
        stackView.addArrangedSubview(language)
        
        let cleanCache = buildFunctionItem(icon: .icCleanCache, title: .init(key: "A00257"), infoView: nil)
        cleanCache.onTap {
            let alert = LNCommonAlertView()
            alert.titleLabel.text = .init(key: "A00257")
            alert.messageLabel.text = .init(key: "B00110")
            alert.showConfirm {
                showToast(.init(key: "A00258"))
            }
            alert.showCancel()
            alert.popup()
        }
        stackView.addArrangedSubview(cleanCache)
        
        let newVersionTag = LNNewVersionView()
        newVersionTag.isHidden = !LNAppConfig.shared.hasNewVersion
        let about = buildFunctionItem(icon: .icAbout, title: .init(key: "A00247"), infoView: newVersionTag)
        about.onTap { [weak self] in
            guard let self else { return }
            view.pushToAbout()
        }
        stackView.addArrangedSubview(about)
        
        let delete = buildFunctionItem(icon: .icDelete, title: .init(key: "A00259"), infoView: nil)
        delete.onTap { [weak self] in
            guard let self else { return }
            view.pushToWebView(LNJumpWebViewConfig.normalConfig(url: .deleteAccountUrl))
        }
        stackView.addArrangedSubview(delete)
        
        let logoutView = UIView()
        logoutView.backgroundColor = .fill
        logoutView.layer.cornerRadius = 12
        view.addSubview(logoutView)
        logoutView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(functionView.snp.bottom).offset(10)
        }
        let logout = buildFunctionItem(icon: .icLogout, title: .init(key: "A00260"), infoView: nil)
        logout.onTap {
            LNCommonAlertView.showLogoutAlert()
        }
        logoutView.addSubview(logout)
        logout.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.verticalEdges.equalToSuperview()
        }
    }
    
    private func buildFunctionItem(icon: UIImage, title: String, infoView: UIView?) -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(42)
        }
        
        let ic = UIImageView()
        ic.image = icon.withRenderingMode(.alwaysTemplate)
        ic.tintColor = .text_4
        container.addSubview(ic)
        ic.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.height.equalTo(18)
        }
        
        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_m
        titleLabel.textColor = .text_5
        titleLabel.text = title
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(ic.snp.trailing).offset(2)
            make.verticalEdges.equalToSuperview()
            make.trailing.lessThanOrEqualTo((infoView ?? arrow).snp.leading)
        }
        
        return container
    }
}
