//
//  Keyboard.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/17.
//

extension TSChatViewController {
    
    var creatInputBarVC: TSChatInputBarVC{
        let inputBarVC = TSChatInputBarVC()
        inputBarVC.sendComplete = { [weak self] components in
            guard let self = self else { return }
            inputSendMsg(components)
        }
        return inputBarVC
    }
    
    
    var creatInputBarBgView:UIView{
        let inputBarBgView = UIView()
        inputBarBgView.addShadow(shadowColor: "#111111".uiColor.cgColor, shadowOffset: CGSize(width: 0, height: -10), shadowRadius: 10, shadowOpacity: 1.0)
        return inputBarBgView
    }
    
    var creatScrollToBottomButton: UIButton{
        let backBottomBtn = UIButton.createButton(image: UIImage(named: "down_arrow_line")) { [weak self]  in
            guard let self = self else { return }
            messagesCollectionView.scrollToLastItem(animated: false)
        }
        backBottomBtn.isHidden = true
        backBottomBtn.backgroundColor = .popupColor
        backBottomBtn.cornerRadius = 16.0
        return backBottomBtn
    }
    
}
extension TSChatViewController{
    
    
    // UICollectionViewDelegate 方法
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        let contentHeight = scrollView.contentSize.height
        let frameHeight = scrollView.frame.size.height
        
        // 判断是否需要显示滚动到底部的按钮
        if offsetY > contentHeight - frameHeight + inputContainerView.frame.size.height - 40 {
            scrollToBottomButton.isHidden = true
        } else {
            scrollToBottomButton.isHidden = false
        }
        
      
    }
}



extension TSChatViewController{
    
    func setUpInputBarType(){
        inputBarBgView.addSubview(inputBarTopView)
        inputBarTopView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(0)
        }
        
        if viewModel.uiStyle == .chat {
            inputBarBgView.addSubview(inputBarVC.view)
            inputBarVC.view.snp.makeConstraints { make in
                make.leading.equalTo(0)
                make.trailing.equalTo(0)
                make.top.equalTo(inputBarTopView.snp.bottom)
                make.bottom.equalTo(0)
            }
        }
        inputBarType = .custom(inputBarBgView)
    }
    

    func configureMessageInputBar() {
        handleKeyBoard()
        configureScrollToBottomButton()
    }
    
    
    func handleKeyBoard(){
        if viewModel.uiStyle == .chat {
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickView))
            tapGesture.cancelsTouchesInView = false
            messagesCollectionView.addGestureRecognizer(tapGesture)
            
            // 监听键盘事件
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        }
    }
    
    
    func configureScrollToBottomButton() {
        view.addSubview(scrollToBottomButton)
        scrollToBottomButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(inputContainerView.snp.top).offset(-14)
            make.width.height.equalTo(32)
        }
        self.scrollViewDidScroll(self.messagesCollectionView)
        

    }
    
    @objc func clickView(){
        view.endEditing(true)
    }
    

    @objc func keyboardWillShow(_ notification: Notification) {

        guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
              let animationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else {
            return
        }

        let keyboardHeight = keyboardFrame.height
        let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)

        UIView.animate(withDuration: animationDuration) {
            self.messagesCollectionView.contentInset = contentInset
            self.messagesCollectionView.scrollIndicatorInsets = contentInset
        }
        
        kDelayMainShort {
            self.messagesCollectionView.scrollToLastItem(animated: false)
        }
    }

    @objc func keyboardWillHide(_ notification: Notification) {
        guard let animationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else {
            return
        }

        let differenceOfBottomInset = inputContainerView.bounds.size.height
        let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: differenceOfBottomInset, right: 0)
        
        UIView.animate(withDuration: animationDuration) {
            self.messagesCollectionView.contentInset = contentInset
            self.messagesCollectionView.scrollIndicatorInsets = contentInset
        }
    }
    
}
