//
//  TSChatViewController+SendMsg.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/17.
//

extension TSChatViewController {
    
    func inputSendMsg(_ data: [Any]) {
        
        //判断 vip
        if kJudgeVip(externalBool: kPurchaseDefault.freeNumAvailable(type: .aichat) == false, vc: self, closePageBlock: {[weak self] in
            guard let self = self else { return }
        }){ return }
        
        inputBarVC.emptyInput()
        
        sendMessages(data)
        scrollToBottom()
        view.endEditing(true)
    }
    
    
    private func sendMessages(_ data: [Any]) {
        let user = viewModel.kUserSender
        for component in data {
            if let str = component as? String {
                let message = TSChatMessage(text: str, user: user, messageId: UUID().uuidString, date: Date())
                insertMessage(message)
                //保存这条消息到本地数据库
                //发送消息后,进行AI 对话生成
                generativeAIChat(message: message)
            }
        }
    }
    
    func generativeAIChat(message:TSChatMessage) {
        var messageString = ""
        switch message.kind {
        case .text(let message):
            messageString = message
        default:
            break
        }
        
        if messageString.count == 0 {
            return
        }
        
        //先插入回答的消息,转圈加载
        let message = TSChatMessage(attributedText: kMDAttributedString(text: ""), user: viewModel.kAIUser, messageId: UUID().uuidString, date: Date())
        message.sendState = .start
        insertMessage(message)
        NotificationCenter.default.post(name: .kAIAnsweringNotification, object: nil, userInfo: [kIsAIAnswering: true])

        //每次全部输出
        viewModel.sendChatMessage(message: messageString) {[weak self] string in
            guard let self = self else { return }
            debugPrint("viewModel.AiMDString=\(viewModel.AiMDString)")
            message.kind = .attributedText(kMDAttributedString(text: viewModel.AiMDString))
            message.sendState = .progress(0.5)

            if self.scrollToBottomButton.isHidden == true {
                updataAIChatCellUI()
                self.messagesCollectionView.scrollToLastItem(animated: false)
            }
        }
        
//        //逐字输出
//        var previousAiMDString = ""
//        viewModel.sendChatMessage(message: messageString) {[weak self] string in
//            guard let self = self else { return }
//            debugPrint("viewModel.AiMDString=\(viewModel.AiMDString)")
//            message.sendState = .progress(0.5)
//            delayedOutputAnimation(message: message, previousStr: previousAiMDString, newAddStr: string)
//            previousAiMDString = viewModel.AiMDString
//        }
        
        completion: {[weak self] data, error in
            guard let self = self else { return }
            if let _ = data {
                message.sendState = .success("netData")
                kPurchaseDefault.useOnceForFree(type: .aichat)//消耗一次 AI 次数
                message.kind = .attributedText(kMDAttributedString(text: viewModel.AiMDString))
            }else {
                message.kind = .attributedText(kMDAttributedString(text: kAIErrorString))
                message.sendState = .failed(kAIErrorString)
            }
            updataAIChatCellUI()
            
            kExecuteOnMainThread {
                NotificationCenter.default.post(name: .kAIAnsweringNotification, object: nil, userInfo: [kIsAIAnswering: false])
                
                //更新 Vip
                if kPurchaseDefault.isVip == false{
                    self.updateVipView()
                }
                
                if self.scrollToBottomButton.isHidden == true {
                    self.scrollToBottom()
                }
            }
        }
    }
    
    func updataAIChatCellUI(){
        kExecuteOnMainThread {
            if self.messageList.count >= 2 {
                UIView.performWithoutAnimation {
                    self.messagesCollectionView.reloadItems(at: [self.lastIndexPath])
                }
            }else{
                self.messagesCollectionView.reloadData()
            }
        }
    }
    
    // 判断是否显示最后一个单元格
    func isLastItemVisible() -> Bool {
        let visibleIndexPaths = messagesCollectionView.indexPathsForVisibleItems
        return visibleIndexPaths.contains(lastIndexPath)
    }

    func scrollToBottom() {
        self.messagesCollectionView.scrollToLastItem(animated: false)
    }
}


extension  TSChatViewController{
    
    func delayedOutputAnimation(message:TSChatMessage,previousStr:String,newAddStr:String, delay: TimeInterval = 0.07)  {
        var showUIString = previousStr
        let characters = Array(newAddStr)// 将 newText 转换为字符数组
        
        // 使用递归逐字显示
        func typeCharacter(at index: Int) {
            guard index < characters.count else { return } // 递归终止条件
            showUIString.append(String(characters[index]))
            // 设置延迟，逐字显示
            DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
                if self.scrollToBottomButton.isHidden == true {
                    message.kind = .attributedText(kMDAttributedString(text: showUIString))
                    self.updataAIChatCellUI()
                    self.scrollToBottom()
                }
                typeCharacter(at: index + 1) // 递归调用
            }
        }
        
        // 开始逐字显示
        typeCharacter(at: 0)
    }
    
    
    func typeTextAnimation(label: UILabel, originalText: String, newText: String, delay: TimeInterval = 0.05) {
        // 确保 label 当前显示的文本是 originalText
        label.text = originalText

        // 将 newText 转换为字符数组
        let characters = Array(newText)

        // 使用递归逐字显示
        func typeCharacter(at index: Int) {
            guard index < characters.count else { return } // 递归终止条件

            // 更新 label 的文本
            let text =  String(characters[0...index])
            label.text = text
            label.attributedText = kMDAttributedString(text: text)
            
            // 设置延迟，逐字显示
            DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
                typeCharacter(at: index + 1) // 递归调用
            }
        }

        // 开始逐字显示
        typeCharacter(at: originalText.count)
    }
}

let kIsAIAnswering = "isAIAnswering"
public extension Notification.Name {
    //AI 回答中通知
    static let kAIAnsweringNotification = Self.init("kAIAnsweringNotification")
}


