//
//  SHInputView.swift
//  ShorthandMaster
//
//  Created by 张哲 on 2020/9/24.
//  Copyright © 2020 明津李. All rights reserved.
//

import UIKit

class SHInputView: UIView {
    
    // MARK: - property
    var textView : SHInputTextView!
    var placeholder : UILabel!
    
    var bottomView : SHInputBottomView!
    
    /// 是否为编辑状态
    var isEdited: Bool = Bool()
    
    /// 是否有音频
    var haveAudio: Bool = Bool()
    
    // MARK: - func
    init(frame: CGRect, isEdited: Bool, haveAudio: Bool)
    {
        super.init(frame: frame)
        
        self.isEdited = isEdited
        
        self.haveAudio = haveAudio
        
        self.addSubviews()
        
        self.setUpSubviewsLocation()
        
        // 监听键盘出现
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(note:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        // 监听键盘消失
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(note:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    func addSubviews()
    {
        // 输入框
        self.textView = SHInputTextView.init()
        self.textView.backgroundColor = UIColor.clear
        self.textView.textColor = UIColor.init(r: 34, g: 34, b: 34, a: 1)
        self.textView.font = UIFont.init(name: SHPingFangMedium, size: 18)
        self.textView.contentInset = UIEdgeInsets.init(top: 24, left: 0, bottom: 0, right: 0);
        self.textView.alwaysBounceVertical = true
        self.addSubview(self.textView)
        
        // 占位文字
        self.placeholder = UILabel.init()
        self.placeholder.text = "笔记标题"
        self.placeholder.textColor = UIColor.init(r: 153, g: 153, b: 153)
        self.placeholder.font = UIFont.init(name: SHPingFangMedium, size: 18)
        self.textView.addSubview(self.placeholder)
        
        // 底部栏
        self.bottomView = SHInputBottomView.init(frame: .zero, isEdited: isEdited, haveAudio: self.haveAudio)
        self.addSubview(self.bottomView)
    }
    
    // MARK: 控件位置
    func setUpSubviewsLocation()
    {
        self.textView.mas_makeConstraints { (make) in
            
            make?.left.mas_equalTo()(20 - 5)
            make?.right.mas_equalTo()(0)
            make?.top.mas_equalTo()(0)
            make?.bottom.equalTo()(self.bottomView.mas_top)
            
        }
        
        self.placeholder.mas_makeConstraints { (make) in
            
            make?.left.mas_equalTo()(5)
            make?.top.mas_equalTo()(8)
            
        }
        
        self.bottomView.mas_makeConstraints { (make) in
            
            make?.left.right()?.bottom().mas_equalTo()(0)
            make?.height.mas_equalTo()((self.isEdited && self.haveAudio ? 100 : 50) + SafeAreaBottomHeight());
            
        }
    }
    
    // MARK: - Events
    // MARK: 监听键盘出现
    @objc func keyboardWillShow(note : Notification)
    {
        self.bottomView.directionImageView.image = UIImage(named: "input_down")
        
        let info : Dictionary = note.userInfo! as Dictionary
        let value = info[UIResponder.keyboardFrameEndUserInfoKey]
//            info.object(forKey: UIResponder.keyboardFrameEndUserInfoKey)
        let keyboardRect = (value as AnyObject).cgRectValue
        
        let height : Float = Float((keyboardRect?.size.height)!)
        
        weak var weakSelf = self
        
        self.setNeedsUpdateConstraints()
        
        UIView.animate(withDuration: 0.3) {
            
            weakSelf?.bottomView.mas_updateConstraints { (make) in
                
                make?.bottom.mas_equalTo()(-height + SafeAreaBottomHeight())
                
            }
            
            weakSelf?.bottomView.superview!.layoutIfNeeded()
            
        }
    }
    
    // MARK: 监听键盘消失
    @objc func keyboardWillHide(note : Notification)
    {
        self.bottomView.directionImageView.image = UIImage(named: "input_up")
        
        weak var weakSelf = self
        
        self.setNeedsUpdateConstraints()
        
        UIView.animate(withDuration: 0.3) {
            
            weakSelf?.bottomView.mas_updateConstraints { (make) in
                
                make?.bottom.mas_equalTo()(0)
                
            }
            
            weakSelf?.bottomView.superview!.layoutIfNeeded()
            
        }
    }
}
