//
//  SystemCall.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/9/1.
//

import Foundation
import CoreTelephony
import RxSwift
import RxRelay
import DDDateKit_Private

public let SC = SystemCall.default

open class SystemCall {
    public static let `default` = SystemCall()
    
    private let callCenter : CTCallCenter
    
    public var callSuccess : Bool = false
    
    private var isDialing : Bool = false
    
    private var isConnected : Bool = false
    
    private var isDisconnected : Bool = false

    private var callTime : String?
    
    private var dialingTimeInterval : TimeInterval?

    private var connectTimeInterval : TimeInterval?
    
    private var disconnectTimeInterval : TimeInterval?
    
    private var duration : Int?
    
    public var userOrderId : Int?
    
    public var orderCode : String?
    
    public var taskOrderId : Int?
    
    public var uploadRelay = ReplayRelay<Any?>.create(bufferSize: 1)
    
    init() {
        callCenter = CTCallCenter()
    }
    
    func callEvent(successHandler: ((Bool,String?,Int?) -> Void)? = nil) {
        callCenter.callEventHandler = {[weak self] call in
            switch call.callState {
            case "CTCallStateDialing":
                self?.isDialing = true
                self?.isConnected = false
                self?.isDisconnected = false
                
                self?.callTime = Date().dd_toString(format: .yyyyMMddHHmmssDefault)
                self?.dialingTimeInterval = Date().timeIntervalSince1970
                break
            case "CTCallStateIncoming":
                self?.isDialing = false
                self?.isConnected = false
                self?.isDisconnected = false
                break
            case "CTCallStateConnected":
                self?.isConnected = true
                self?.connectTimeInterval = Date().timeIntervalSince1970
                break
            case "CTCallStateDisconnected":
                self?.isDisconnected = true
                self?.disconnectTimeInterval = Date().timeIntervalSince1970
                
                if self?.isDialing == true && self?.isConnected == true {
                    self?.callSuccess = true
                    if successHandler != nil {
                        var duration = 0
                        if let disconnectTimeInterval = self?.disconnectTimeInterval,let dialingTimeInterval = self?.dialingTimeInterval {
                            duration = Int(disconnectTimeInterval - dialingTimeInterval)
                            self?.duration = duration
                        }
                        successHandler!(true,self?.callTime,duration)
                    }
                }else{
                    self?.callSuccess = false
                    if successHandler != nil {
                        successHandler!(false,self?.callTime,0)
                    }
                }
                break
            default:
                break
            }
        }
    }
    
    func getParameters() -> Single<UploadCallRecordParameters> {
        return Single.create {[weak self] single in
            single(.success(UploadCallRecordParameters(userOrderId: self?.userOrderId ?? 0, orderCode: self?.orderCode ?? "", taskOrderId: self?.taskOrderId ?? 0, state: self?.callSuccess == true ? 1 : 2, callTime: self?.callTime ?? "", duration: self?.duration ?? 0)))
            return Disposables.create()
        }
    }
}
