//
//  NavigationBackViewController.swift
//  PaiaiUIKit
//
//  Created by FFIB on 2019/1/11.
//  Copyright © 2019 FFIB. All rights reserved.
//

import UIKit

public protocol NavigationBackViewController: class {
    func setupNavigationBackItem()
}

public extension NavigationBackViewController where Self: UIViewController {
    func setupNavigationBackItem() {
        let btn = UIButton(type: .system)
        btn.contentHorizontalAlignment = .left
        btn.frame = CGRect(x: 0, y: 0, width: 40 , height: 40)
        btn.setImage(UIImage(named: "navigation-back"), for: .normal)
        btn.addTarget(self, action: #selector(backToViewController), for: .touchDown)
        
        let backItem = UIBarButtonItem(customView: btn)
        let spaceItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        spaceItem.width = -10
        
        navigationItem.leftBarButtonItems = [spaceItem, backItem]
    }
}

fileprivate extension UIViewController {
    @objc func backToViewController() {
        navigationController?.popViewController(animated: true)
    }
}