//
//  VehicleMonitorVideoController.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/16.
//

import Foundation
import DDControlsKit_Private
import DDCategoryKit_Private
import DDAutoUIKit_Private
import JXCategoryView
import SnapKit
import RxSwift
import RxRelay
import RxCocoa
import DDProgressHUDKit_Private

extension VehicleMonitorVideoController {
    func addActions() {
        reloadRelay
            .filter({[weak self] _ in
                return self?.vehicleId != nil
            })
            .observe(on: MainScheduler.instance)
            .do(onNext: {[weak self] _ in
                self?.view.dd_showHUD()
            })
            .flatMapLatest {[weak self] _ in
                return RQ.getRtspChannel(prameters: RtspChannelParameters(vehicleId: (self?.vehicleId)!))
            }
            .observe(on: MainScheduler.instance)
            .do(onNext: {[weak self] _ in
                self?.view.dd_hideHUD()
            })
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] response in
                if response?.success == true {
                    var channels : [String] = []
                    for index in 0..<(response?.data?.count ?? 0) {
                        channels.append("通道"+"\(index + 1)")
                    }
                    if let data = response?.data {
                        self?.videos.removeAll()
                        self?.videos.append(contentsOf: data)
                    }
                    self?.vehicleMonitorVideoView.categoryView.titles = channels
                    self?.vehicleMonitorVideoView.categoryView.reloadData()
                    self?.vehicleMonitorVideoView.categoryView.selectItem(at: 0)
                }else{
                    self?.view.dd_makeToast(response?.msg)
                }
                
            })
            .disposed(by: disposeBag)
        
        reloadRelay.accept(nil)
    }
}

extension VehicleMonitorVideoController : JXCategoryViewDelegate {
    public func categoryView(_ categoryView: JXCategoryBaseView!, didSelectedItemAt index: Int) {
        
        let vc = children.first as? VehicleMonitoringVideoDetailController
        vc?.playAssetURL(assetURL: URL(string: videos[index])!)
    }
}

open class VehicleMonitorVideoController : DDViewController {
    private let vehicleId : Int?
    private let deviceCode : String?
    private let vehicleMonitorVideoView : VehicleMonitorVideoView
    private let reloadRelay = ReplayRelay<Any?>.create(bufferSize: 1)
    private let disposeBag = DisposeBag()
    private var videos : [String] = []
    
    public init(vehicleId:Int?,deviceCode:String?) {
        self.vehicleId = vehicleId
        self.deviceCode = deviceCode
        self.vehicleMonitorVideoView = VehicleMonitorVideoView()
        super.init(nibName: nil, bundle: nil)
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        dd_navigationItemTitle = "视频监控"
        dd_navigationBarBackgroundColor = .hex("354683")
        dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.white(alpha: 0.7),.font:UIFont.mediumFont(17)]
        dd_backBarButtonItem?.tintColor = .hex("000000")
        
        vehicleMonitorVideoView.categoryView.delegate = self
        view.addSubview(vehicleMonitorVideoView)
        vehicleMonitorVideoView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(CGRectGetHeight(UIApplication.shared.dd_statusBarFrame)+CGRectGetHeight(navigationController?.navigationBar.frame ?? .zero))
            make.left.right.bottom.equalToSuperview()
        }
        
        let videoDetailVc = VehicleMonitoringVideoDetailController(assetURL: nil)
        videoDetailVc.dd_navigationBarBackgroundColor = .white
        videoDetailVc.dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.hex("000000"),.font:UIFont.mediumFont(17)]
        addChild(videoDetailVc)
        vehicleMonitorVideoView.addSubview(videoDetailVc.view)
        videoDetailVc.view.snp.makeConstraints { make in
            make.top.equalTo(vehicleMonitorVideoView.categoryView.snp.bottom).offset(auto(10))
            make.width.equalTo(auto(375))
            make.height.equalTo(auto(300))
            make.centerX.equalToSuperview()
        }
        
        addActions()
    }

    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
}

open class VehicleMonitorVideoView : DDView {
    public let categoryView : JXCategoryTitleView
    
    public override init(frame: CGRect) {
        categoryView = JXCategoryTitleView.init()
        super.init(frame: frame)
        categoryView.titleColor = .hex("737373").alpha(0.55)
        categoryView.titleSelectedColor = .hex("3678FF")
        categoryView.titleFont = .mediumFont(auto(14))
        categoryView.backgroundColor = .hex("FFFFFF")
        let indicator = JXCategoryIndicatorLineView()
        indicator.indicatorColor = .hex("3678FF")
        indicator.indicatorWidth = JXCategoryViewAutomaticDimension
        indicator.verticalMargin = 7
        categoryView.indicators = [indicator]
        addSubview(categoryView)
        categoryView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(0)
            make.left.equalTo(auto(20))
            make.right.equalTo(-auto(20))
            make.height.equalTo(auto(40))
        }
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

