//
//  LNMineViewController.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/14.
//

import Foundation
import UIKit
import SnapKit


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


class LNMineViewController: UIViewController {
    private let stackView = UIStackView()
    private let qrCodeView = LNMineQRCodeShareView()
    private let orderRecordView = LNMineOrderRecordView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
        onUserInfoChanged(userInfo: myUserInfo)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        LNRelationManager.shared.reloadRelationInfoIfNeed()
        LNProfileManager.shared.reloadMyProfileDetail()
        LNPurchaseManager.shared.reloadWalletInfo()
        
        if myUserInfo.playmate {
            LNGameMateManager.shared.getGameMateManagerInfo()
        }
    }
}

extension LNMineViewController: LNProfileManagerNotify {
    func onUserInfoChanged(userInfo: LNUserProfileVO) {
        guard userInfo.userNo.isMyUid else { return }
        
        qrCodeView.isHidden = !userInfo.playmate
        orderRecordView.isHidden = !userInfo.playmate
    }
}

extension LNMineViewController {
    private func setupViews() {
        view.backgroundColor = .primary_1
        
        let topCover = UIImageView()
        topCover.image = .icHomeTopBg
        view.addSubview(topCover)
        topCover.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
        }
        
        let fakeNavBar = LNFakeNaviBar()
        fakeNavBar.isUserInteractionEnabled = false // 因为是空白
        view.addSubview(fakeNavBar)
        fakeNavBar.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.contentInset = .init(top: 0, left: 0, bottom: -view.commonBottomInset + 70, right: 0)
        scrollView.clipsToBounds = false
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalToSuperview()
            make.top.equalTo(fakeNavBar.snp.bottom)
        }
        
        stackView.axis = .vertical
        stackView.spacing = 16
        scrollView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        stackView.addArrangedSubview(LNMineUserInfoView())
        stackView.addArrangedSubview(qrCodeView)
        stackView.addArrangedSubview(LNMineWalletInfoView())
        stackView.addArrangedSubview(orderRecordView)
        stackView.addArrangedSubview(LNMineFunctionView())
    }
}


#if DEBUG

import SwiftUI

struct LNMineViewControllerPreview: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        LNMineViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
}

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