//
//  WYNavigationBar.swift
//  Gengmei
//
//  Created by wangyang on 16/5/23.
//  Copyright © 2016年 更美互动信息科技有限公司. All rights reserved.
//

import UIKit
import GMKit

@objc public protocol GMNavigationBarDelegate: class {
    @objc optional func backAction(_ button: UIButton)
    @objc optional func rightButtonClicked(_ button: UIButton)
    @objc optional func nearRightButtonClicked(_ button: UIButton)
}

@objcMembers
public class GMNavigationBar: UIView {

    // MARK: - 普通属性
    public let itemView = UIView(frame: CGRect(x: 0, y: 0, width: Constant.screenWidth, height: 44))
    public let titleLabel = UILabel()
    public let leftButton = GMBaseNavigationButton(type: .custom)
    public let rightButton = GMBaseNavigationButton(type: .custom)
    public let nearRightButton = GMBaseNavigationButton(type: .custom)
    public weak var delegate: GMNavigationBarDelegate?
    public var isBarShowShadow = true {
        didSet {
            if isBarShowShadow {
                self.layer.shadowColor = UIColor.init(white: 0, alpha: 0.1).cgColor
            } else {
                self.layer.shadowColor = UIColor.clear.cgColor
            }
        }
    }
    // MARK: - 属性、属性观察器
    public var titleView: UIView? {
        didSet {
            if let newView = titleView {
                if newView != oldValue {
                    oldValue?.removeFromSuperview()
                }
                itemView.insertSubview(newView, at: 0)
            } else {
                oldValue?.removeFromSuperview()
            }
        }
    }

    public var title: String? {
        willSet {
            titleLabel.isHidden = (newValue == nil)
            titleLabel.text = newValue
        }
    }

    public var leftIcon: String? {
        willSet {
            setImage(newValue, for: leftButton)
        }
    }

    public var rightIcon: String? {
        willSet {
            setImage(newValue, for: rightButton)
        }
    }

    public var leftTitle: String? {
        willSet {
            leftButton.isHidden = false
            leftButton.setTitle(newValue, for: UIControlState())
            leftButton.setImage(nil, for: UIControlState())
        }
    }

    public var rightTitle: String? {
        willSet {
            rightButton.isHidden = false
            rightButton.setTitle(newValue, for: UIControlState())
        }
    }

    public var nearRightIcon: String? {
        willSet {
            setImage(newValue, for: nearRightButton)
        }
    }

    public var nearRightTitle: String? {
        willSet {
            nearRightButton.isHidden = false
            nearRightButton.setTitle(newValue, for: UIControlState())
        }
    }

    // MARK: - 方法
    override public init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: Constant.screenWidth, height: GMNavigationBar.barHeight))
        setup()
    }

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

    public func setup() {
        self.layer.masksToBounds = false
        self.layer.shadowOpacity = 1.0
        self.layer.shadowRadius = 2.0
        self.layer.shadowColor = UIColor.init(white: 0, alpha: 0.1).cgColor
        self.layer.shadowOffset = CGSize(width: 0, height: Constant.onePixel);
        self.backgroundColor = UIColor.white
        leftIcon = "back"
        leftButton.adaptiveHotAreaWidth = 70
        leftButton.setTitleColor(UIColor.headlineText, for: .normal)
        leftButton.titleLabel?.font = UIFont.gmFont(14)
        leftButton.addTarget(self, action: #selector(leftAction(_:)), for: .touchUpInside)

        rightButton.addTarget(self, action: #selector(rightAction(_:)), for: .touchUpInside)
        rightButton.isHidden = true
        rightButton.setTitleColor(UIColor.headlineText, for: .normal)
        rightButton.titleLabel?.font = UIFont.gmFont(14)
        rightButton.adaptiveHotAreaWidth = 40

        nearRightButton.isHidden = true
        nearRightButton.addTarget(self, action: #selector(nearRightAction(_:)), for: .touchUpInside)
        nearRightButton.setTitleColor(UIColor.headlineText, for: .normal)
        nearRightButton.titleLabel?.font = UIFont.gmFont(14)
        nearRightButton.adaptiveHotAreaWidth = 40

        titleLabel.isHidden = true
        titleLabel.textAlignment = .center
        titleLabel.backgroundColor = UIColor.clear
        titleLabel.textColor = UIColor.headlineText
        titleLabel.font = UIFont.gmFont(18)

        addSubview(itemView)
        itemView.addSubview(titleLabel)
        itemView.addSubview(leftButton)
        itemView.addSubview(rightButton)
        itemView.addSubview(nearRightButton)
        itemView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(44)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets.zero)
        }
        leftButton.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.centerY.equalToSuperview()
        }
        rightButton.snp.makeConstraints { (make) in
            make.right.equalTo(-20)
            make.centerY.equalToSuperview()
        }
        nearRightButton.snp.makeConstraints { (make) in
            make.right.equalTo(rightButton.snp.left).offset(-20)
            make.centerY.equalToSuperview()
        }
    }

    public func hideAllItems() {
        titleLabel.isHidden = true
        leftButton.isHidden = true
        rightButton.isHidden = true
        nearRightButton.isHidden = true
    }

    override public func layoutSubviews() {
        super.layoutSubviews()
        titleView?.center = CGPoint(x: itemView.bounds.size.width/2, y: itemView.bounds.size.height/2)
    }
}

// MARK: - 私有
@objc extension GMNavigationBar {

    fileprivate func setImage(_ iconName: String?, for button: GMBaseNavigationButton) {
        if let icon = iconName, let image = UIImage(named: icon) {
            button.isHidden = false
            button.setImage(image, for: UIControlState())
        } else {
            button.isHidden = true
            assert(iconName != nil && iconName!.characters.count > 0, "找不到图片")
        }
    }

    @objc fileprivate func leftAction(_ button: UIButton) {
        delegate?.backAction?(button)
    }

    @objc fileprivate func rightAction(_ button: UIButton) {
        delegate?.rightButtonClicked?(button)
    }

    @objc fileprivate func nearRightAction(_ button: UIButton) {
        delegate?.nearRightButtonClicked?(button)
    }
}


public extension GMNavigationBar {
    /// 状态栏的高度
    @objc public static var statusBarHeight: CGFloat {
        let statusBatr:CGFloat = Constant.screenHeight/Constant.screenWidth >= 2.0 ? 44 : 20
        return statusBatr;
    }

    /// navigationBar的高度
    @objc public static let barHeight = statusBarHeight + 44
    
    /// item的centerY
    @objc public static let navigationItemCenterY = statusBarHeight + 44/2.0
}
