//
//  LNFakeNaviBar.swift
//  Lanu
//
//  Created by OneeChan on 2026/1/15.
//

import Foundation
import UIKit
import SnapKit


class LNFakeNaviBar: UIView {
    private(set) var backButton: UIButton?
    let actionView = UIView()
    
    private var rightMenuView: UIView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        clipsToBounds = false
        
        addSubview(actionView)
        actionView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(UIView.navigationBarHeight)
            make.top.equalToSuperview().offset(UIView.statusBarHeight)
        }
    }
    
    @discardableResult
    func showBackButton(_ handler: (() -> Void)? = nil) -> UIButton {
        let backButton = UIButton.backButton()
        backButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            if let handler {
                handler()
            } else {
                navigationController?.popViewController(animated: true)
            }
        }), for: .touchUpInside)
        actionView.addSubview(backButton)
        backButton.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.height.equalTo(44)
        }
        self.backButton = backButton
        
        return backButton
    }
    
    func setRightMenu(_ view: UIView) {
        rightMenuView?.removeFromSuperview()
        
        actionView.addSubview(view)
        view.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
            make.width.height.equalTo(44)
        }
        rightMenuView = view
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
