//
// MIT License
//
// Copyright (c) 2017-2020 MessageKit
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import MessageKit
import UIKit

class TSChatViewController: MessagesViewController, MessagesDataSource {

    var deleteBlock:(()->Void)?
    //数据
    var viewModel:TSAIChatVM = TSAIChatVM()
    lazy var messageList: [TSChatMessage] = []
    
    //导航栏
    lazy var vipBtn: UIButton = creatVipBtn
    lazy var navBarView: TSBaseNavContentBarView = creatNavBarView
    lazy var navBarContentView: UIView = creatNavBarContentView
    lazy var normalNavBarView: TSNormalNavigationBarView = creatNormalNavBarView

    //collectionView 布局
    lazy var textMessageSizeCalculator = TSTextLayoutSizeCalculator(layout:self.messagesCollectionView.messagesCollectionViewFlowLayout)
    
    //键盘
    lazy var inputBarVC: TSChatInputBarVC = creatInputBarVC
    lazy var inputBarBgView:UIView = creatInputBarBgView
    let inputBarTopView:UIView = UIView()
    lazy var scrollToBottomButton: UIButton = creatScrollToBottomButton

    // vip 相关
    lazy var freeText: UILabel = creatFreeText
    lazy var upgradeVipBg: UIView = creatUpgradeVipBg

    let formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        return formatter
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        configureNaviBarView()
        configureMessageCollectionView()
        configureMessageInputBar()
        loadFirstMessages()
        updateVipView()
        dealThings()
    }
    
    
    func dealThings(){
        
        vipBtn.isHidden = PurchaseManager.default.isVip
        NotificationCenter.default.addObserver(self, selector: #selector(vipInfoChanged), name: .kPurchaseDidChanged, object: nil)
        
        if viewModel.uiStyle == .chat {
            // 注册通知监听,App死的时候,保存本次聊天记录到本地
            NotificationCenter.default.addObserver(self, selector: #selector(saveChatList), name: .kApplicationWillTerminate, object: nil)
        }
    }
    
    @objc func vipInfoChanged() {
        kExecuteOnMainThread {
            self.vipBtn.isHidden = PurchaseManager.default.isVip
            self.updateVipView()
        }
    }

    @objc func saveChatList() {
        messageList.removeFirst()
        if messageList.count > 0 {
            //保存本次聊天记录
            viewModel.updateMessages(msgModels: messageList)
        }
    }
    
    func loadFirstMessages() {
        self.messageList = viewModel.getHistoryChatMessage()
        self.messagesCollectionView.reloadData()
        self.messagesCollectionView.scrollToLastItem(animated: false)
    }

    func configureMessageCollectionView() {
        clearAndResetConstraints()
        view.backgroundColor = .mainBg
        //设置自定义FlowLayout,itemsize等,都在这里控制
        let flowLayout = CustomMessagesFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)
    
        messagesCollectionView.collectionViewLayout = flowLayout
        messagesCollectionView.backgroundColor = .clear
        messagesCollectionView.register(TSTextMessageContentCell.self)
        messagesCollectionView.messagesLayoutDelegate = self
        messagesCollectionView.messagesDisplayDelegate = self
        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messageCellDelegate = self
        messagesCollectionView.clipsToBounds = true
        scrollsToLastItemOnKeyboardBeginsEditing = true // default false
        maintainPositionOnInputBarHeightChanged = true // default false
        showMessageTimestampOnSwipeLeft = false // default false
        //        messagesCollectionView.refreshControl = refreshControl
        messagesCollectionView.reloadData()
    }
    
    
    func clearAndResetConstraints() {
        // 筛选出与 messagesCollectionView 相关的约束
        let constraintsToRemove = view.constraints.filter { constraint in
            return (constraint.firstItem as? UIView == messagesCollectionView) || (constraint.secondItem as? UIView == messagesCollectionView)
        }
        // 停用并移除这些约束
        NSLayoutConstraint.deactivate(constraintsToRemove)
        for constraint in constraintsToRemove {
            view.removeConstraint(constraint)
        }

        messagesCollectionView.snp.remakeConstraints { make in
            make.leading.trailing.bottom.equalTo(0)
            make.top.equalTo(k_Nav_Height)
        }
    }


    // MARK: - Helpers
    var lastIndexPath:IndexPath{
        if messageList.count == 0 {
            return IndexPath(item: 0, section: 0)
        }
        return IndexPath(item: messageList.count - 1, section: 0)
    }
    
    func insertMessage(_ message: TSChatMessage) {
        messageList.append(message)
        messagesCollectionView.performBatchUpdates({
            messagesCollectionView.insertItems(at: [lastIndexPath])
            if messageList.count >= 2 {
                messagesCollectionView.reloadItems(at: [lastIndexPath])
            }
        }, completion: { [weak self] _ in
            if self?.isLastSectionVisible() == true {
                self?.messagesCollectionView.scrollToLastItem(animated: true)
            }
        })
    }
    
    
    func isLastSectionVisible() -> Bool {
        guard !messageList.isEmpty else { return false }
        return messagesCollectionView.indexPathsForVisibleItems.contains(lastIndexPath)
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        
    }

}


