//
//  PlayDetaiListViewContoller.swift
//  ColorfulWallpaper
//
//  Created by ni on 2024/9/19.
//

import Foundation
import UIKit

class PlayDetailListViewContoller: UIViewController, UITableViewDelegate {
    lazy var bgView: UIView = .simpleView(color: .hexColor("#222222"))

    lazy var tapArea: UIControl = {
        let area = UIControl()
        area.addTarget(self, action: #selector(dismissCurrentVc), for: .touchUpInside)
        return area
    }()

    lazy var lineView: UIView = {
        let line: UIView = .simpleView(color: .white.withAlphaComponent(0.2))
        line.customCornerRadius = 2
        return line
    }()

    lazy var titleLabel: UILabel = .simpleLabel(text: "Up Next".localized(),font: .font(size: 18), color: .white)

    var viewModel: PlayDetailListViewModel = .init()

    lazy var listView: UITableView = {
        let tabView = UITableView()
        tabView.dataSource = viewModel
        tabView.delegate = self
        tabView.separatorStyle = .none
        tabView.register(PlayDetailListViewCell
            .self, forCellReuseIdentifier: "PlayDetailListViewCell")
        tabView.backgroundColor = .clear
        return tabView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildren()
        makeConstraints()
        scrollToCurrentVideo()
    }

    func addChildren() {
        view.backgroundColor = .clear
        view.addSubview(tapArea)
        view.addSubview(bgView)
        bgView.addSubview(lineView)
        bgView.addSubview(titleLabel)
        bgView.addSubview(listView)
    }

    func scrollToCurrentVideo() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            if let indexPath = self.viewModel.indexOfCurrentVideo {
                self.listView.scrollToRow(at: indexPath, at: .middle, animated: false)
            }
        }
    }


    @objc func dismissCurrentVc() {
        dismiss(animated: true)
    }

   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let count = viewModel.needRecommend ? viewModel.recommendDatas.count : viewModel.videos.count
        if indexPath.row < count {
            if viewModel.needRecommend {
                let data = viewModel.recommendDatas[indexPath.row]
                PlayerManager.shared.playVideo(onlineVideo: data, recommendDatas: [], scene: .recommend)
            } else {
                let video = viewModel.videos[indexPath.row]
                PlayerManager.shared.playVideo(video: video,
                                                    list: viewModel.videos,
                                                    scene: .local,
                                                    onceAdKey: "")
            }

            dismissCurrentVc()
        }
    }

    func makeConstraints() {
        tapArea.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.bottom.equalTo(bgView.snp.top)
        }
        bgView.snp.makeConstraints { make in
            make.bottom.leading.trailing.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(0.7)
        }
        lineView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(32)
            make.height.equalTo(4)
            make.top.equalToSuperview().offset(8)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(20)
            make.leading.equalToSuperview().offset(16)
        }
        
        listView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.leading.trailing.bottom.equalToSuperview()
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bgView.setRectCorner(corner: [.topLeft, .topRight], radii: CGSize(width: 16, height: 16))
    }
}


