//
//  UserPermission.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/9/6.
//

import Foundation
import RxSwift
import RxRelay

public let USERP = UserPermission.default

open class UserPermission {
    public static let `default` = UserPermission()
    public let relay = ReplayRelay<Any?>.create(bufferSize: 1)
    public let disposeBag = DisposeBag()
    public var userPermissionRelay = ReplayRelay<Array<String>?>.create(bufferSize: 1)
    
    public var canRejectDispatchHandle : Bool = false
    public var canAcceptDispatchHandle : Bool = false
    public var canWaitdispatchBtn : Bool = false
    public var canWaitModifyDispatchBtn : Bool = false
    public var canDealWith : Bool = false
    public var canSupplierRemarkBtn : Bool = false
    public var canSupplierAuditEditCostBtn : Bool = false
    public var canSupplierAuditUploadPhotoBtn : Bool = false

    private let maxRetryCount = 120
    
    init() {
        Observable.combineLatest(relay, USER.refreshTokenSub)
            .filter({ (_,_) in
                return USER.isLogin == true
            })
            .flatMapLatest { (_,_) in
                return RQ.userOperationPermissions()
            }
            .retry(when: { (rxError: Observable<Error>) -> Observable<Int> in
                return rxError.enumerated().flatMap({[weak self] (index,error) -> Observable<Int> in
                    guard index < (self?.maxRetryCount ?? 0) else {
                        return Observable.error(error)
                    }
                    return Observable.timer(RxTimeInterval.seconds(5), scheduler: MainScheduler.instance)

                })
            })
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] response in
                if response?.success == true {
                    if response?.data?.contains(where: { value in
                        value == UserOperationPermissionsDataModel.DataEnum.rejectDispatchHandle.rawValue
                    }) == true {
                        self?.canRejectDispatchHandle = true
                    }else{
                        self?.canRejectDispatchHandle = false
                    }
                    
                    if response?.data?.contains(where: { value in
                        value == UserOperationPermissionsDataModel.DataEnum.acceptDispatchHandle.rawValue
                    }) == true {
                        self?.canAcceptDispatchHandle = true
                    }else{
                        self?.canAcceptDispatchHandle = false
                    }
                    
                    if response?.data?.contains(where: { value in
                        value == UserOperationPermissionsDataModel.DataEnum.waitdispatchBtn.rawValue
                    }) == true {
                        self?.canWaitdispatchBtn = true
                    }else{
                        self?.canWaitdispatchBtn = false
                    }
                    
                    if response?.data?.contains(where: { value in
                        value == UserOperationPermissionsDataModel.DataEnum.waitModifyDispatchBtn.rawValue
                    }) == true {
                        self?.canWaitModifyDispatchBtn = true
                    }else{
                        self?.canWaitModifyDispatchBtn = false
                    }
                    
                    if response?.data?.contains(where: { value in
                        value == UserOperationPermissionsDataModel.DataEnum.dealWith.rawValue
                    }) == true {
                        self?.canDealWith = true
                    }else{
                        self?.canDealWith = false
                    }
                    
                    if response?.data?.contains(where: { value in
                        value == UserOperationPermissionsDataModel.DataEnum.supplierRemarkBtn.rawValue
                    }) == true {
                        self?.canSupplierRemarkBtn = true
                    }else{
                        self?.canSupplierRemarkBtn = false
                    }
                    
                    if response?.data?.contains(where: { value in
                        value == UserOperationPermissionsDataModel.DataEnum.supplierAuditEditCostBtn.rawValue
                    }) == true {
                        self?.canSupplierAuditEditCostBtn = true
                    }else{
                        self?.canSupplierAuditEditCostBtn = false
                    }
                    
                    if response?.data?.contains(where: { value in
                        value == UserOperationPermissionsDataModel.DataEnum.supplierAuditUploadPhotoBtn.rawValue
                    }) == true {
                        self?.canSupplierAuditUploadPhotoBtn = true
                    }else{
                        self?.canSupplierAuditUploadPhotoBtn = false
                    }
                    
                    self?.userPermissionRelay.accept(response?.data)
                }
            },onError: { error in

            })
            .disposed(by: disposeBag)
    }
}
