//
//  TSBaseOperation.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/20.
//

import Foundation

class TSBaseOperation: Operation , @unchecked Sendable{
    typealias TaskCompletionHandler = (Error?) -> Void
    typealias TaskBlock = (@escaping TaskCompletionHandler) -> Void
    
    private let task: TaskBlock
    private var _executing = false
    private var _finished = false
    private var _cancelled = false
    private var _error: Error?
    
    init(task: @escaping TaskBlock) {
        self.task = task
        super.init()
    }
    
    // MARK: - State Management
    
    override var isExecuting: Bool {
        return _executing
    }
    
    override var isFinished: Bool {
        return _finished
    }
    
    override var isCancelled: Bool {
        return _cancelled
    }
    
    override var isAsynchronous: Bool {
        return true
    }
    
    override func start() {
        if isCancelled {
            completeOperation()
            return
        }
        
        willChangeValue(forKey: "isExecuting")
        _executing = true
        didChangeValue(forKey: "isExecuting")
        
        task { [weak self] error in
            guard let self = self else { return }
            self._error = error
            self.completeOperation()
        }
    }
    
    override func cancel() {
        willChangeValue(forKey: "isCancelled")
        _cancelled = true
        didChangeValue(forKey: "isCancelled")
        
        if isExecuting {
            completeOperation()
        }
    }
    
    private func completeOperation() {
        if _executing {
            willChangeValue(forKey: "isExecuting")
            _executing = false
            didChangeValue(forKey: "isExecuting")
        }
        
        if !_finished {
            willChangeValue(forKey: "isFinished")
            _finished = true
            didChangeValue(forKey: "isFinished")
        }
    }
    
    // MARK: - Error Handling
    
    var error: Error? {
        return _error
    }
    
    // MARK: - Convenience Methods
    
    override func waitUntilFinished() {
        while !isFinished {
            RunLoop.current.run(mode: .default, before: .distantFuture)
        }
    }
    
    func addDependency(_ operation: TSBaseOperation) {
        super.addDependency(operation)
    }
    
    func removeDependency(_ operation: TSBaseOperation) {
        super.removeDependency(operation)
    }
}





//import Foundation
//
//final class TaskOperation: Operation, @unchecked Sendable {
//    typealias TaskCompletionHandler = (Error?) -> Void
//    typealias TaskBlock = (@escaping TaskCompletionHandler) -> Void
//    
//    private let task: TaskBlock
//    private var _executing = false
//    private var _finished = false
//    private var _cancelled = false
//    private var _error: Error?
//    
//    private let stateLock = NSLock() // 用于线程安全的状态管理
//    
//    init(task: @escaping TaskBlock) {
//        self.task = task
//        super.init()
//    }
//    
//    // MARK: - State Management
//    
//    override var isExecuting: Bool {
//        stateLock.lock()
//        defer { stateLock.unlock() }
//        return _executing
//    }
//    
//    override var isFinished: Bool {
//        stateLock.lock()
//        defer { stateLock.unlock() }
//        return _finished
//    }
//    
//    override var isCancelled: Bool {
//        stateLock.lock()
//        defer { stateLock.unlock() }
//        return _cancelled
//    }
//    
//    override var isAsynchronous: Bool {
//        return true
//    }
//    
//    override func start() {
//        stateLock.lock()
//        if isCancelled {
//            stateLock.unlock()
//            completeOperation()
//            return
//        }
//        
//        willChangeValue(forKey: "isExecuting")
//        _executing = true
//        didChangeValue(forKey: "isExecuting")
//        stateLock.unlock()
//        
//        task { [weak self] error in
//            guard let self = self else { return }
//            self.stateLock.lock()
//            self._error = error
//            self.stateLock.unlock()
//            self.completeOperation()
//        }
//    }
//    
//    override func cancel() {
//        stateLock.lock()
//        willChangeValue(forKey: "isCancelled")
//        _cancelled = true
//        didChangeValue(forKey: "isCancelled")
//        stateLock.unlock()
//        
//        if isExecuting {
//            completeOperation()
//        }
//    }
//    
//    private func completeOperation() {
//        stateLock.lock()
//        if _executing {
//            willChangeValue(forKey: "isExecuting")
//            _executing = false
//            didChangeValue(forKey: "isExecuting")
//        }
//        
//        if !_finished {
//            willChangeValue(forKey: "isFinished")
//            _finished = true
//            didChangeValue(forKey: "isFinished")
//        }
//        stateLock.unlock()
//    }
//    
//    // MARK: - Error Handling
//    
//    var error: Error? {
//        stateLock.lock()
//        defer { stateLock.unlock() }
//        return _error
//    }
//    
//    // MARK: - Convenience Methods
//    
//    func waitUntilFinished() {
//        while !isFinished {
//            RunLoop.current.run(mode: .default, before: .distantFuture)
//        }
//    }
//    
//    func addDependency(_ operation: TaskOperation) {
//        super.addDependency(operation)
//    }
//    
//    func removeDependency(_ operation: TaskOperation) {
//        super.removeDependency(operation)
//    }
//}
