//
//  TSBaseVC.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

import UIKit
import Combine

class TSBaseVC: UIViewController {
    
    var cancellable: [AnyCancellable] = []
    
    private var blurEffect: UIBlurEffect?
    private lazy var navBlurView: UIVisualEffectView = {
        let view = UIVisualEffectView()
        return view
    }()
    
    lazy var contentView: UIView = {
        let view = UIView()
        return view
    }()
    
    
    lazy var navBarContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    private lazy var normalNavBarView: TSNormalNavigationBarView = {
        let view = TSNormalNavigationBarView()
        return view
    }()
    
    
    lazy var viewBgImageView: UIImageView = {
        let view = UIImageView()
        view.backgroundColor = .clear
        return view
    }()
    
    lazy var nullView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    lazy var netWorkView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    /// 提供一个无参数的默认初始化方法
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupSuperSomeView()
        createData()
        createView()
        dealThings()
    }
    
    private func setupSuperSomeView() {
        edgesForExtendedLayout = [.all]
        
        view.addSubview(contentView)
        view.addSubview(navBarContentView)
        
        contentView.snp.makeConstraints { make in
            make.top.equalTo(k_Nav_Height)
            make.leading.trailing.bottom.equalToSuperview()
        }
        
        navBarContentView.snp.makeConstraints { make in
            make.leading.top.trailing.equalToSuperview()
            make.height.equalTo(k_Nav_Height)
        }
    

        view.backgroundColor = .mainBg
        view.layoutIfNeeded()
    }
    
    
    
    
    func setViewBgImageNamed(named:String){
        if viewBgImageView.superview == nil {
            view.insertSubview(viewBgImageView, at: 0)
            viewBgImageView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
        viewBgImageView.image = UIImage(named: named)
    }
    
    
    //子类重写
    func createData(){
        
        
    }
    func createView(){
        
        
    }
    
    func dealThings(){
        
        
    }
    

    func addNormalNavBarView(){
        navBarContentView.addSubview(normalNavBarView)
        normalNavBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
//    func useTransparentBlurredNavigationBar() {
//        navBarView.removeFromSuperview()
//        edgesForExtendedLayout = .all
//        
//        view.addSubview(navBlurView)
//        navBlurView.snp.makeConstraints { make in
//            make.left.top.right.equalToSuperview()
//            make.height.equalTo(k_Nav_Height)
//        }
//        
//        setClearNavigationBar()
//        navBarView.titleNavBtn?.titleLabel?.alpha = 0.0
//        navBlurView.contentView.addSubview(navBarView)
//        
//        navBarView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
//        }
//        
//        contentView.snp.updateConstraints { make in
//            make.top.equalToSuperview()
//        }
//    }
//    
//    func handleTransparentBlurredNavigationBar(scrollViewY: CGFloat) {
//        if scrollViewY >= - kStatusBar_Height {
//            navBlurView.effect = blurEffect
//            navBarView.titleNavBtn?.titleLabel?.alpha = 1.0
//        } else {
//            navBlurView.effect = nil
//            navBarView.titleNavBtn?.titleLabel?.alpha = 0.0
//        }
//    }
    
    

    
    // ... 其他方法根据需要重写 ...
    
    
    
    
    
    override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
        debugPrint("进入------>\(String(describing: type(of: self)))")
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
        cancellable.removeAll()
        debugPrint("♻️♻️♻️ TGRootViewController -> \(type(of: self)) deinit ♻️♻️♻️")
    }

    // MARK: - 导航栏方法

    func setTitleText(_ title: String) {
       _ = normalNavBarView.setTitleName(NSLocalizedString(title, comment: ""))
    }

    func setPageTitle(_ title: String) {
        let pageTitle = title
        let backTitle = " "
        setTitleText(pageTitle)
        _ = setNavigationItem(backTitle, imageName: "navi_back_white", direction: .left, action: #selector(navBarClickLeftAction))
    }

    func setNavigationItem(_ name: String, imageName: String, direction: NSTextAlignment, action: Selector) -> UIButton {
        if direction == .left {
            return normalNavBarView.setLeftNavigationItem(name: name, imageName: imageName, target: self, action: action)
        } else {
            return normalNavBarView.setRightNavigationItem(name: name, imageName: imageName, target: self, action: action)
        }
    }

    @objc func navBarClickLeftAction() {
        debugPrint("navBarClickLeftAction -> \(type(of: self))")
        pop()
    }

    func pop() {
        if navigationController == nil {
            dismiss(animated: true, completion: nil)
        } else if navigationController?.presentingViewController != nil, navigationController?.viewControllers.count == 1 {
            navigationController?.dismiss(animated: true, completion: nil)
        } else {
            navigationController?.popViewController(animated: true)
        }
    }

}


extension TSBaseVC {
    
    func setNavBarViewHidden(_ isHidden: Bool) {
        navBarContentView.snp.updateConstraints { make in
            make.height.equalTo(isHidden ? 0 : k_Nav_Height)
        }
        
        contentView.snp.updateConstraints { make in
            make.top.equalTo(isHidden ? 0 : k_Nav_Height)
        }
    }
}
