//
//  PQStuckPointMusicSearchController.swift
//  PQSpeed
//
//  Created by SanW on 2021/5/6.
//  Copyright © 2021 BytesFlow. All rights reserved.
//

import BFAnalyzeKit
import BFCommonKit
import BFMediaKit
import BFUIKit
import UIKit

class PQStuckPointMusicSearchController: PQStuckPointMusicContentController {
    // 选中的总时长
    var selectedTotalDuration: Float64 = 0
    // 选择的总数
    var selectedDataCount: Int = 0
    // 选择的图片总数
    var selectedImageDataCount: Int = 0
    // 当前搜索页数
    var pageNum: Int = 1
    // 当前搜索关键字
    var searchWord: String?
    // 当前选中的index
    var lastSelectedIndex: IndexPath?
    // 热搜数据
    var hotList: [Any] = Array<Any>.init()

    override func viewDidLoad() {
        super.viewDidLoad()
//        emptyData?.title = "没有搜索到相关音乐"
//        emptyData?.emptyImageName = "pic_search_empty"
//        emptyRemindView.remindLab.font = UIFont.systemFont(ofSize: 14)
//        emptyRemindView.remindLab.textColor = UIColor.hexColor(hexadecimal: "#999999")
//        emptyRemindView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 200)
//        emptyRemindView.emptyData = emptyData
//        refreshHandle = { [weak self] _, _ in
//            self?.loadRequestData(isRefresh: false)
//        }
    }

    /// 开始搜索
    /// - Parameter keyword: 搜索文字
    /// - Returns: <#description#>
    func loadSearchData(keyword: String?) {
        if keyword == nil || (keyword?.count ?? 0) <= 0 {
            cShowHUB(superView: nil, msg: "请先输入搜索内容")
            return
        }
        BFLog(message: "背景音乐--开始搜索背景音乐")
        if keyword != searchWord || (keyword == searchWord && itemList.count <= 0) {
            showEmptyView()
            itemList.removeAll()
            collectionView.reloadData()
            pageNum = 0
            searchWord = keyword
//            SWNetRequest.cancelTask(url: PQENVUtil.shared.materialSearchApi + searchBGMMaterialUrl)
            loadRequestData()
        }
    }

    // 发起搜索请求
    @objc func loadRequestData(isRefresh: Bool = true) {
        pageNum = pageNum + 1
        PQBaseViewModel.searchBGMListData(searchWord, pageNum, 20, videoCount: selectedDataCount - selectedImageDataCount, imageCount: selectedImageDataCount, totalDuration: selectedTotalDuration) { [weak self] bgmList, msg in
            // 处理请求数据
            BFLog(message: "背景音乐--搜索背景音乐成功")
            if bgmList.count <= 0 {
                self?.pageNum = (self?.pageNum ?? 0) - 1
            }

            if bgmList.count <= 0, (self?.itemList.count ?? 0) <= 0, (self?.hotList.count ?? 0) > 0 {
                if !(self?.hotList.first is BFEmptyModel) {
                    self?.hotList.insert(BFEmptyModel(), at: 0)
                }

                self?.configMusicListData(isRefresh: true, musicListData: self?.hotList ?? [])
            } else {
                self?.configMusicListData(isRefresh: isRefresh, musicListData: bgmList)
            }
            // 点击上报：用户在搜索框输入文字然后按回车-返回结果后上报
            BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_searchSyncedUpMusic, pageSource: .sp_stuck_searchSyncedUpMusic, extParams: ["searchText": self?.searchWord ?? "", "searchResultNumber": bgmList.count, "isSuccess": msg == nil], commonParams: commonParams())
        }
    }

    /// 清空搜索数据
    /// - Returns: <#description#>
    func clearData() {
        BFLog(message: "背景音乐--清空背景音乐搜索数据")
        hotList.forEach { item in
            if item is PQVoiceModel {
                (item as? PQVoiceModel)?.isSelected = false
                (item as? PQVoiceModel)?.isPlaying = false
            }
        }
        itemList.removeAll()
        searchWord = nil
        pageNum = 0
        lastSelectedIndex = nil
        collectionView.reloadData()
    }
}
