//
//  NewTraining.swift
//  OrderScheduling
//
//  Created by 中道 on 2024/5/9.
//

import Foundation
import RxSwift
import RxRelay
import PopupDialog

class NewTraining : NSObject {
    static let `default` = NewTraining()
    
    var newTrainingRelay = ReplayRelay<Any?>.create(bufferSize: 1)
    
    var disposeBag = DisposeBag()
    
    override init() {
        super.init()
        
        newTrainingRelay
            .flatMapLatest({ _ in
                return RQ.jumpNewTrain()
            })
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] response in
                if response?.success == true {
                    if response?.data?.jump == true {
                        let tabBarController = UIApplication.shared.dd_keyWindow.rootViewController as? UITabBarController
                        let navController = tabBarController?.selectedViewController as? UINavigationController
                        let vc = navController?.topViewController
                        if vc?.isKind(of: WebViewController.self) == true, (vc as? WebViewController)?.vcTitle == WebViewTool.WebViewNameEnum.newTrainingList.rawValue {
                        }else{
                            let popup = PopupDialog(title: nil, message: response?.data?.content,buttonAlignment: .horizontal,transitionStyle: .zoomIn,tapGestureDismissal: false,
                                                    panGestureDismissal: false,
                                                    hideStatusBar: false){

                            }
                            let vc = popup.viewController as! PopupDialogDefaultViewController
                            vc.titleColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
                            vc.messageColor = UIColor(red: 0.13, green: 0.19, blue: 0.33, alpha: 1)
                            vc.titleFont = UIFont.boldSystemFont(ofSize: 16)
                            vc.messageFont = UIFont.systemFont(ofSize: 16)
                            
                            let buttonOne = CancelButton(title: "取消") {
                            }
                            buttonOne.titleFont = UIFont.boldSystemFont(ofSize: 16)
                            buttonOne.titleColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.7)
                            
                            let buttonTwo = DefaultButton(title: "去查看", dismissOnTap: true) {[weak self] in
                                DispatchQueue.main.async {[weak self] in
                                    if let linkUrl = response?.data?.url {
                                        let vc = WebViewController.init(showNavBar: true,title: response?.data?.title,url: linkUrl+"?token=\((USER.token)!)")
                                        vc.disappearHandler = {[weak self] in
                                            self?.newTrainingRelay.accept(nil)
                                        }
                                        let tabVc2 = UIApplication.shared.dd_keyWindow.rootViewController as? MainTabBarController
                                        let nav2 = tabVc2?.selectedViewController as? UINavigationController
                                        nav2?.pushViewController(vc, animated: true)
                                    }
                                }
                            }
                            buttonTwo.titleFont = UIFont.boldSystemFont(ofSize: 16)
                            buttonTwo.titleColor = UIColor(red: 29/255.0, green: 100/255.0, blue: 210/255.0, alpha: 0.7)

                            popup.addButtons([buttonOne, buttonTwo])
                            let tabVc1 = UIApplication.shared.dd_keyWindow.rootViewController as? MainTabBarController
                            let nav1 = tabVc1?.selectedViewController as? UINavigationController
                            nav1?.present(popup, animated: true)
                        }
                    }
                }else{
                    UIApplication.shared.dd_keyWindow.rootViewController?.view.dd_makeToast(response?.msg)
                }
            })
            .disposed(by: disposeBag)
    }
}
