//
//  PushNotiCommonTool.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/9/5.
//

import Foundation
import RxSwift
import RxCocoa

public let PUSHNOTICOMMONTOOL = PushNotiCommonTool.default

open class PushNotiCommonTool : NSObject {
    public static let `default` = PushNotiCommonTool()
    private let disposeBag = DisposeBag()
    
    func open(userInfo:[AnyHashable:Any]?) {
        let commonView = PushNotiCommonView()
        var userOrderId : Int?
        var taskOrderId : Int?
        var taskCode : String?
        if let dict = GROUP.parserAlertToDictionary(userInfo: userInfo) {
            if let title = dict[pushNoti_title_key] {
                commonView.titleLabel.text = title
            }
            if let body = dict[pushNoti_body_key] {
                commonView.contentLabel.text = body
            }
        }
        if let _userOrderId = (userInfo?[pushNoti_userOrderId_key] as? String) {
            userOrderId = Int(_userOrderId)
        }
        if let _taskOrderId = (userInfo?[pushNoti_taskOrderId_key] as? String) {
            taskOrderId = Int(_taskOrderId)
        }
        if let _taskCode = (userInfo?[pushNoti_taskCode_key] as? String) {
            taskCode = _taskCode
        }
        if let cancel = (userInfo?[pushNoti_cancelButtonText_key] as? String) {
            commonView.cancelButton.setTitle(cancel, for: .normal)
        }
        if let sure = (userInfo?[pushNoti_confirmButtonText_key] as? String) {
            commonView.sureButton.setTitle(sure, for: .normal)
        }
        
        commonView.cancelButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {
                ENTRY.dismiss(name: pushNotiCommonViewEntry) {
                    // 停止声音播放
                    TOOL.stopVoice()
                }
            })
            .disposed(by: disposeBag)
        
        commonView.sureButton.rx.tap
            .flatMapLatest({ _ in
                return (GROUP.isCancelOrderPushNotification(userInfo: userInfo) == true) ? RQ.giveUpUserOrder(parameters: GiveUpUserOrderParameters(userOrderId: userOrderId,taskOrderId: taskOrderId)) : Single.just(nil)
            })
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: { _ in
                ENTRY.dismiss(name: pushNotiCommonViewEntry) {
                    // 停止声音播放
                    TOOL.stopVoice()
                    
                    // 报备跳转webview
                    if GROUP.isReportIndexPushNotification(userInfo: userInfo) == true {
                        if let supplierId = USER.supplierId,let taskCode,let userOrderId {
                            WEBTOOL.open(name: .reportIndex,appending: "&userOrderId=\(userOrderId)&type=1&userOrderCode=\(taskCode)&supplierId=\(supplierId)")
                        }
                    }
                }
            })
            .disposed(by: disposeBag)
        
        ENTRY.showPushNotiCommonEntry(view: commonView,name: pushNotiCommonViewEntry)
    }
    
    func open(model:AppPushRecordListDataModel?,continueHandler: (() -> Void)? = nil) {
        let commonView = PushNotiCommonView()
        var userOrderId : Int?
        var taskOrderId : Int?
        if let title = model?.pushPayloadObj?.title {
            commonView.titleLabel.text = title
        }
        if let body = model?.pushPayloadObj?.content {
            commonView.contentLabel.text = body
        }
        if let orderId = model?.pushPayloadObj?.userOrderId {
            userOrderId = Int(orderId)
        }
        if let taskId = model?.pushPayloadObj?.taskOrderId {
            taskOrderId = Int(taskId)
        }
        if let cancel = model?.pushPayloadObj?.cancelButtonText {
            commonView.cancelButton.setTitle(cancel, for: .normal)
        }
        if let sure = model?.pushPayloadObj?.confirmButtonText {
            commonView.sureButton.setTitle(sure, for: .normal)
        }
        
        commonView.cancelButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {
                ENTRY.dismiss(name: pushNotiCommonViewEntry) {
                    // 停止声音播放
                    TOOL.stopVoice()
                }
            })
            .disposed(by: disposeBag)
        
        commonView.sureButton.rx.tap
            .flatMapLatest({ _ in
                return RQ.giveUpUserOrder(parameters: GiveUpUserOrderParameters(userOrderId: userOrderId,taskOrderId: taskOrderId))
            })
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: { _ in
                ENTRY.dismiss(name: pushNotiCommonViewEntry) {
                    // 停止声音播放
                    TOOL.stopVoice()
                    
                    continueHandler?()
                }
            })
            .disposed(by: disposeBag)
        
        ENTRY.showPushNotiCommonEntry(view: commonView,name: pushNotiCommonViewEntry)
    }
}
