//
//  KeyboardHelpView.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/23.
//

import UIKit

protocol KeyboardHelpViewDelegate: NSObjectProtocol {
    
    func helpCollectionViewDidSelectItem(characterId: String, content: String, complete: @escaping (() -> ()))
}

class KeyboardHelpView: KeyboardBaseView {
    
    struct UX {
        static let cellWidth = (KeyboardConst.kb_kScreenW - 96.0) / 3.0
        static let cellHeight = 46.0
    }
    
    weak var helpDelegate: KeyboardHelpViewDelegate?
    
    var characterList: [CharacterModel]? {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    lazy var collectionView: UICollectionView = {
            
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .vertical
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        
        collectionView.dataSource = self
        collectionView.delegate = self
        
        collectionView.showsVerticalScrollIndicator = false
        collectionView.bounces = false
        
        collectionView.register(KeyboardCharacterCell.self, forCellWithReuseIdentifier: KeyboardCharacterCell.reuseIdentifier())
        
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension KeyboardHelpView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (self.characterList?.count ?? 0) + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.row == self.characterList?.count {
            
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
            cell.titleLabel.isHidden = true
            cell.addBtn.isHidden = false
            return cell
        } else {
            
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
            if let model = self.characterList?[indexPath.row] {
                cell.config(model: model)
            }
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if self.dialogueLabel.text?.count == 0 {
            self.toast(text: "请先粘贴文字")
            return
        }
        
        let cell = collectionView.cellForItem(at: indexPath) as! KeyboardCharacterCell
        cell.setAnimate(play: true)
        if let model = self.characterList?[indexPath.row], let characterId = model.id {
            helpDelegate?.helpCollectionViewDidSelectItem(characterId: characterId, content: self.dialogueLabel.text ?? "") {
                cell.setAnimate(play: false)
                self.dialogueClearBtnAction()
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: UX.cellWidth, height: UX.cellHeight)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 6
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 6
    }
}

extension KeyboardHelpView {
    
    func initUI() {
        
        self.addSubview(self.collectionView)
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(deleteBtn.snp.left).offset(-6)
            make.top.equalTo(dialogueView.snp.bottom).offset(6)
            make.bottom.equalTo(self.sendBtn.snp.bottom).offset(3)
        }
    }
}
