//
//  RoleVoiceListController.swift

//
//  Created by Leon on 2025/7/20.
//

import JXPagingView
import UIKit
class RoleVoiceListController: CLBaseTableController {
    var selectVoiceId : Int?
    // var selectVoice: VoiceModel?

    var voices = [VoiceModel]()
    
    
    var selectVoiceAction: ((VoiceModel?) -> Void)?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupViews()
        setupDatas()
        setupEvents()
    }

    private func setupViews() {
        tableView.contentInset = .init(top: 20, left: 0, bottom: UIWindow.safeAreaBottom, right: 0)
        tableView.register(RoleVoiceListCell.self, forCellReuseIdentifier: "RoleVoiceListCell")
    }

    private func setupDatas() {
        select(voiceId: selectVoiceId)
    }

    private func setupEvents() {
    }

    override func loadData() {
        
    }

    public func select(voiceId: Int?) {
        guard let selectId = voiceId else{return}
        selectVoiceId = selectId
        
//        for per in voices {
//            let voice = per
//            if voice.voiceDict.id! == selectId {
//                selectVoice = voice
//            }
//        }
        if tableView != nil{
            tableView.reloadData()
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return voices.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RoleVoiceListCell", for: indexPath) as! RoleVoiceListCell
        cell.delegate = self
        let model = voices[indexPath.row]
        cell.config(model: model)
        
        if let selectId = selectVoiceId, selectId == model.voiceDict.id! {
            cell.setupSelectedState(selected: true)
        } else {
            cell.setupSelectedState(selected: false)
        }

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // None?
        let model = voices[indexPath.row]
        if SpeechManager.checkVoiceState(), let speechModel = model.speechModel{
            SpeechManager.shared.startPlay(with: speechModel)
        }
    }
}

extension RoleVoiceListController: RoleVoiceListCellDelegate {
    func roleVoiceListTapAdd(_ cell: RoleVoiceListCell, model: VoiceModel?) {
        guard let voice = model else {
            return
        }
        
        // 已经选中，点击x
        if let selectId = selectVoiceId, selectId == voice.voiceDict.id! {
            selectVoiceId  = nil
            selectVoiceAction?(nil)
            tableView.reloadData()
        }else{
            selectVoiceId = voice.voiceDict.id
            selectVoiceAction?(voice)
        }
        
        
    }

    func roleVoiceListTapPlay(_ cell: RoleVoiceListCell, model: VoiceModel?) {
        // see tableView didSelectRowAt
//        print("roleVoiceListTapPlay")
//
//        if SpeechManager.checkVoiceState(), let speechModel = model!.speechModel{
//            SpeechManager.shared.startPlay(with: speechModel)
//        }
    }
}

extension RoleVoiceListController: JXPagingViewListViewDelegate {
    func listView() -> UIView {
        return view
    }

    func listScrollView() -> UIScrollView {
        return tableView
    }

    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> Void) {
        listViewDidScrollCallback = callback
    }
}
