//
//  KeyboardPrologueView.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/28.
//

import UIKit
import JXSegmentedView

protocol KeyboardPrologueViewDelegate: NSObjectProtocol {
    
    func prologueCollectionViewDidSelectItem(name: String, complete: @escaping (([String]) -> ()))
    
    func prologueTableViewDidSelectItem(content: String)
}

class KeyboardPrologueView: KeyboardBaseView {
    
    struct UX {
        static let twoTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 4
        static let threeTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 3
        static let fourTitleBtnWidth = (KeyboardConst.kb_kScreenW - 20) / 4
        static let resultCellHeight = "单行文字".heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
    }
    
    weak var prologueDelegate: KeyboardPrologueViewDelegate?
    
    // 是否在加载
    var isResultLoading: Bool = true
    
    var selectName: String?
    
    var resultList: [String]? {
        didSet {
            self.tableView.reloadData()
        }
    }
    
    // 开场白列表
    var prologueList: [PrologueModel]? {
        didSet {
            updateUI()
        }
    }
    
    // 主题
    var selectTopics: [PrologueTopicModel]? {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    let titleDataSource: JXSegmentedTitleDataSource = {
        
        let titleDataSource = JXSegmentedTitleDataSource()
        titleDataSource.titles = []
        titleDataSource.itemWidth = UX.fourTitleBtnWidth
        titleDataSource.titleNormalColor = UIColor.hexStringColor(hexString: "#77849D")
        titleDataSource.titleSelectedColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
        titleDataSource.titleNormalFont = .systemFont(ofSize: 12)
        titleDataSource.titleSelectedFont = .boldSystemFont(ofSize: 12)
        titleDataSource.itemSpacing = 0
        
        return titleDataSource
    }()
    
    lazy var pagingTitleView: JXSegmentedView = {
        
        let totalItemWidth = KeyboardConst.kb_kScreenW - 20
        
        let indicator = JXSegmentedIndicatorBackgroundView()
        indicator.indicatorWidthIncrement = -4
//        indicator.indicatorWidth = totalItemWidth / 2
        indicator.indicatorHeight = 30
        indicator.indicatorColor = .hexStringColor(hexString: "#DDCFFD")
        indicator.indicatorCornerRadius = 15
        
        let titleView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: totalItemWidth, height: 34.0))
        titleView.backgroundColor = .white
        titleView.delegate = self
        titleView.dataSource = titleDataSource
        titleView.indicators = [indicator]
//        titleView.listContainer = containerView
        titleView.layer.cornerRadius = 17

        return titleView
    }()
    
    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
    }()
    
    lazy var resultView: UIView = {
       
        let view = UIView()
        view.isHidden = true
        return view
    }()
        
    lazy var backBtn: UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "keyboard_back_btn"), for: .normal)
        btn.layer.shadowOffset = CGSize(width: 0, height: 0)
        btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
        btn.layer.shadowRadius = 6.1
        btn.addTarget(self, action: #selector(backBtnClickAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var regenerateBtn: UIButton = {
        
        let btn = UIButton()
        btn.backgroundColor = .white
        btn.layer.cornerRadius = 12
        btn.layer.shadowOffset = CGSize(width: 0, height: 0)
        btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
        btn.layer.shadowRadius = 6.1
        btn.setTitle("重新生成", for: .normal)
        btn.setTitleColor(.hexStringColor(hexString: "#000000", alpha: 0.8), for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12)
        btn.addTarget(self, action: #selector(regenerateBtnClickAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var tableView: UITableView = {
        
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.delegate = self
        tableView.dataSource = self
       
        if #available(iOS 11, *) {
            tableView.estimatedRowHeight = 0
            tableView.estimatedSectionFooterHeight = 0
            tableView.estimatedSectionHeaderHeight = 0
            tableView.contentInsetAdjustmentBehavior = .never
        }
        
        tableView.isUserInteractionEnabled = true
        tableView.register(KeyboardTeachDialogueCell.self, forCellReuseIdentifier: KeyboardTeachDialogueCell.reuseIdentifier())
        
        return tableView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension KeyboardPrologueView {
    
    // 返回按钮点击
    @objc func backBtnClickAction() {
        
        self.isResultLoading = true
        self.collectionView.isHidden = false
        self.resultView.isHidden = true
        self.dialogueClearBtnAction()
    }
    
    // 重新生成按钮点击
    @objc func regenerateBtnClickAction() {
        
        self.isResultLoading = true
        self.tableView.reloadData()
        
        if let selectName = self.selectName {
            
            prologueDelegate?.prologueCollectionViewDidSelectItem(name: selectName) { list in
                self.isResultLoading = false
                self.resultList = list
            }
        }
    }
}

extension KeyboardPrologueView: JXSegmentedViewDelegate {
    
    //返回列表的数量
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return titleDataSource.titles.count
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
        if let prologueList = self.prologueList {
            let topics = prologueList[index].topics
            self.selectTopics = topics
        }
    }
}

extension KeyboardPrologueView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.selectTopics?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardCharacterCell.reuseIdentifier(), for: indexPath) as! KeyboardCharacterCell
        if let selectTopics = self.selectTopics, selectTopics.count > 0 {
            let name = selectTopics[indexPath.row].name
            cell.config(content: name ?? "")
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if let selectTopics = self.selectTopics, selectTopics.count > 0 {
            
            // 清空回答列表
            self.resultList?.removeAll()
            self.collectionView.isHidden = true
            self.resultView.isHidden = false
            self.tableView.reloadData()
            
            let name = selectTopics[indexPath.row].name
            prologueDelegate?.prologueCollectionViewDidSelectItem(name: name ?? "") { list in
                self.isResultLoading = false
                self.resultList = list
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width = (KeyboardConst.kb_kScreenW - 96) / 3
        return CGSize(width: width, height: 46)
    }
    
    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 KeyboardPrologueView: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return isResultLoading ? 4 : (resultList?.count ?? 0) + 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: KeyboardTeachDialogueCell.reuseIdentifier(), for: indexPath) as! KeyboardTeachDialogueCell
        cell.backgroundColor = .clear
        cell.selectionStyle = .none
        if indexPath.section == 0 {
            cell.isHidden = true
        } else {
            if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
                let content = resultList[indexPath.section - 1]
                cell.config(content: content)
            }
             
            cell.setLoading(play: isResultLoading)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if let resultList = self.resultList, resultList.count > 0 && !isResultLoading {
            
            let content = resultList[indexPath.section - 1]
            prologueDelegate?.prologueTableViewDidSelectItem(content: content)
            self.backBtnClickAction()
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if isResultLoading {
            return UX.resultCellHeight
        }
        
        if indexPath.section != 0 {
            if let resultList = self.resultList, resultList.count > 0 {
                let content = resultList[indexPath.section - 1]
                let height = content.heightAccording(width: KeyboardConst.kb_kScreenW - 105, font: .systemFont(ofSize: 12), lineSpacing: 5) + 16
                return height
            }
        }
        
        return UX.resultCellHeight
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 8
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: KeyboardConst.kb_kScreenW - 84, height: 8))
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
}

extension KeyboardPrologueView {
    
    func updateUI() {
        
        if let prologueList = self.prologueList {
            
            var titles = [String]()
            for prologue in prologueList {
                titles.append(prologue.title ?? "")
            }
            
            if prologueList.count == 2 {
                self.titleDataSource.itemWidth = UX.twoTitleBtnWidth
            } else if prologueList.count == 3 {
                self.titleDataSource.itemWidth = UX.threeTitleBtnWidth
            } else {
                self.titleDataSource.itemWidth = UX.fourTitleBtnWidth
            }
            
            self.titleDataSource.titles = titles
            self.pagingTitleView.dataSource = self.titleDataSource
            self.pagingTitleView.reloadData()
            
            self.selectTopics = prologueList.first?.topics
        }
    }
    
    func initUI() {
        
        self.dialogueView.removeFromSuperview()
        self.pasteBtn.removeFromSuperview()
        
        self.addSubview(pagingTitleView)
        pagingTitleView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(34)
            make.top.equalTo(15)
        }
        
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(deleteBtn.snp.left).offset(-6)
            make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
            make.bottom.equalTo(0)
        }
        
        deleteBtn.snp.remakeConstraints { make in
            make.size.equalTo(CGSize(width: 58, height: 46))
            make.right.equalTo(-10)
            make.top.equalTo(pagingTitleView.snp.bottom).offset(14)
        }
        
        self.addSubview(resultView)
        resultView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(deleteBtn.snp.left).offset(-6)
            make.top.equalTo(pagingTitleView.snp.bottom).offset(12)
            make.bottom.equalTo(0)
        }
        
        resultView.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.left.bottom.right.equalTo(0)
        }
        
        resultView.addSubview(backBtn)
        backBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 32, height: 32))
            make.left.equalTo(0)
            make.top.equalTo(0)
        }
        
        resultView.addSubview(regenerateBtn)
        regenerateBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 74, height: 32))
            make.top.right.equalTo(0)
        }
    }
}
