//
//  MusicEmptyView.swift
//  PhysicalWallPaper
//
//  Created by nkl on 2024/11/13.
//

//import ADManager
import Foundation
import TSVideoKit
import ADManager

extension SongListViewController {
   
    // 长按手势处理
    @objc func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
        let location = gesture.location(in: listView)

        if gesture.state == .began {
            if let indexPath = listView.indexPathForRow(at: location), indexPath.section == 0 {
                let ac = UIAlertController(title: nil,
                                           message: "Are you sure to delete".localized(), preferredStyle: .alert)
                ac.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel))
                ac.addAction(UIAlertAction(title: "Delete".localized(), style: .destructive, handler: { _ in
                    if indexPath.row < self.viewModel.unFinishedVideo.count {
                        let viewModel = self.viewModel.unFinishedVideo[indexPath.row]
                        if let video = viewModel.dataVideo, let videoId = video.videoId {
                            TSVideoOperator.shared.downloadManager.cancelTask(id: videoId) {
                                self.viewModel.requestData()
                            }
                        }
                    }
                }))
                present(ac, animated: true, completion: nil)
            }
        }
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            guard let cell = cell as? SongDownloadCell, indexPath.row < viewModel.unFinishedVideo.count else {
                return
            }

            let viewModel = viewModel.unFinishedVideo[indexPath.row]

            cell.bindViewModel(viewModel: viewModel, isOnline: false)

            cell.downloadStateChangeBlock = { [weak self] state in
                guard let self = self else {
                    return
                }
                switch state {
                case .idle(isAnimate: false), .idle(isAnimate: true):
//                    if PurchaseManager.default.isVip {
                    viewModel.startDownload()
//                    if !PurchaseManager.default.isVip {
//                        ADManager.shared.showAd(scene: ADScene.downloadInsert, from: self)
//                    }
//                    } else {
//                        let loading = ADLoadingViewController()
//                        loading.adScene = .downloadReward
//                        loading.finishedHandler = { finished in
//                            if finished {
//                                viewModel.startDownload()
//                            }
//                        }
//                        present(loading, animated: false)
//                    }
                case .pause:
                    viewModel.resumeDownload()
                    break
                case .done:
                    if let videoModel = viewModel.dataVideo {
//                        self.viewModel.showOperationViewController(video: videoModel)
                    }
                    break
                case .downloading:
                    viewModel.pauseDownload()
                    break
                case .retry:
                    /// todo.kailen
                    viewModel.retryDownload()
                    break
                case .loading:
                    break
                }
            }
        }
    }
}
