 //
 
 import Foundation
-import CoreBluetooth
 
 // MARK: ServiceDiscoveringRequest
 
-/// 扫描服务请求模型
+/// request of discovering services of specific peripheral
 internal class ServiceDiscoveringRequest: OperationRequest {
 
 	// MARK: Stored Properties
 
-	/// 待扫描的服务UUID数组
-	var serviceUUIDs: [CBUUID]?
+	/// UUID array of service to be discovered
+	internal var serviceUUIDs: [UUID]?
 
 	// MARK: Initializer
 

	}

	/**
	发现特征

	- parameter characteristicUUIDs: 特征UUID数组, 传nil则扫描所有特征;
	- parameter service:             服务;
	- parameter peripheral:          从设备;
	- parameter success:             发现特征成功的回调;
	- parameter failure:             发现特征连接失败的回调;
	*/
	public func discover(characteristicUUIDs: [CBUUID]?, ofService service: CBService, inPeripheral peripheral: CBPeripheral, success: ((Response?) -> Void)?, failure: ((NSError?) -> Void)?) {
		dispatch_async(self.mainQ) { () -> Void in
			let req = CharacteristicDiscoveringRequest(characteristicUUIDs: characteristicUUIDs, service: service, peripheral: peripheral, success: success, failure: failure)
			self.characteristicDiscoveringRequests.insert(req)

	}

	/**
	Convenient initializer

	- parameter serviceUUIDs: UUID array of service to be discovered, all services will be discovered if passed nil
	- parameter peripheral:   a CBPeripheral instance of which the services to be discovered
	- parameter success:      a closure called when discovering succeed
	- parameter failure:      a closure called when discovering failed

	- returns: a ServiceDiscoveringRequest instance
	*/
	internal convenience init(serviceUUIDs: [UUID]?, peripheral: Peripheral, success: ((Response?) -> Void)?, failure: ((NSError?) -> Void)?) {
		self.init()
        self.serviceUUIDs = serviceUUIDs
        self.peripheral   = peripheral
        self.success      = success
        self.failure      = failure
	}

	override internal var hash: Int {
		return self.peripheral.hash
	}

	override internal func isEqual(object: AnyObject?) -> Bool {
		if let other = object as? ServiceDiscoveringRequest {
			return self.hash == other.hash
		}
		return false
	}
}

// MARK: CharacteristicDiscoveringRequest

/// request of discovering characteristics of specific service
internal class CharacteristicDiscoveringRequest: OperationRequest {

	// MARK: Stored Properties

	/// UUID array of characteristic to be discovered
	internal var characteristicUUIDs: [UUID]?

	/// a CBService object of which the characteristics to be discovered
	internal var service: Service!

	// MARK: Initializer

	private override init() {
		super.init()
	}

	/**
	Convenient initializer

	- parameter characteristicUUIDs: UUID array of characteristic to be discovered
	- parameter service:             a CBService object of which the characteristics to be discovered
	- parameter peripheral:          a CBPeripheral instance of which the characteristics to be discovered
	- parameter success:             a closure called when discovering succeed
	- parameter failure:             a closure called when discovering failed

	- returns: a CharacteristicDiscoveringRequest instance
	*/
	internal convenience init(characteristicUUIDs: [UUID]?, service: Service, peripheral: Peripheral, success: ((Response?) -> Void)?, failure: ((NSError?) -> Void)?) {
		self.init()
        self.characteristicUUIDs = characteristicUUIDs
        self.service             = service
        self.peripheral          = peripheral
        self.success             = success
        self.failure             = failure
	}

	override internal var hash: Int {
		let string = self.peripheral.identifier.UUIDString + self.service.UUID.UUIDString
		return string.hashValue
	}

	override internal func isEqual(object: AnyObject?) -> Bool {
		if let other = object as? CharacteristicDiscoveringRequest {
			return self.hash == other.hash
		}
		return false
	}
}

// MARK: Discover

extension Cusp {

	/**
	Discover specific or all services of a peripheral.
	发现从设备的部分或全部服务.

	- parameter serviceUUIDs: an UUID array of services to be discovered, all services will be discovered if passed nil. 服务UUID数组, 传nil则扫描所有服务.
	- parameter peripheral:   a CBPeripheral object of which the services to be discovered. 待发现服务的从设备对象.
	- parameter success:      a closure called when discovering succeed. 发现服务成功的闭包.
	- parameter failure:      a closure called when discovering failed. 发现服务失败的闭包.
	*/
	public func discover(serviceUUIDs: [UUID]?, inPeripheral peripheral: Peripheral, success: ((Response?) -> Void)?, failure: ((NSError?) -> Void)?) {
		dispatch_async(self.mainQ) { () -> Void in
			let req = ServiceDiscoveringRequest(serviceUUIDs: serviceUUIDs, peripheral: peripheral, success: success, failure: failure)
			self.serviceDiscoveringRequests.insert(req)
