/*
 * Copyright (c) 2019, Nordic Semiconductor
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this
 *    list of conditions and the following disclaimer in the documentation and/or
 *    other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

 /*
  * Created by codepgq.
  */

import Foundation

public struct LightCTLTemperatureSetUnacknowledged: GenericMessage, TransactionMessage, TransitionMessage {
    public static let opCode: UInt32 = 0x8265
    
    public var tid: UInt8!
    public var parameters: Data? {
        let data = Data() + temperature + deltaUV + tid
        if let transitionTime = transitionTime, let delay = delay {
            return data + transitionTime.rawValue + delay
        } else {
            return data
        }
    }
    
    /// The target value of the Light CTL Temperature state.
    ///
    /// Valid values are in range 0x0320-0x4320 (800-20000K).
    public let temperature: UInt16
    /// The 16-bit signed value representing the Delta UV of a tunable white
    /// light. A value of 0x0000 represents Delta UV = 0.
    public let deltaUV: Int16
    
    public var transitionTime: TransitionTime?
    public var delay: UInt8?
    
    public init(scaledTemperature temperature: Int) {
        self.init(
            temperature: 0x0320 + UInt16((Float(temperature) / 100) * (0x4E20 - 0x0320)),
            deltaUV: 0
        )
    }
    
    /// Creates the Light CTL Temperature Set Unacknowledged message.
    ///
    /// The temperature parameter is color temperature of white light in Kelvin.
    /// The vales for this state are defined in the following table:
    /// - 0x0320 - 0x4E20 - color temperature of white light in Kelvin
    /// - All other values are prohibited and will be rounded to the nearest
    ///   valid value.
    ///
    /// The Light CTL Delta UV state determines the distance from the Black Body
    /// curve. The color temperature of all on the black body locus (curve).
    /// This is a 16-bit signed integer representation of a -1 to +1 scale using
    /// the formula:
    ///
    /// Represented Delta UV = `deltaUV` / 32768
    ///
    /// - parameters:
    ///   - temperature: The target value of the Light CTL Temperature state.
    ///   - deltaUV: The target value of the Light CTL Delta UV state.
    public init(temperature: UInt16, deltaUV: Int16) {
        self.temperature = max(0x0320, min(temperature, 0x4E20))
        self.deltaUV = deltaUV
        self.transitionTime = nil
        self.delay = nil
    }
    
    /// Creates the Light CTL Temperature Set Unacknowledged message.
    ///
    /// The temperature parameter is color temperature of white light in Kelvin.
    /// The vales for this state are defined in the following table:
    /// - 0x0320 - 0x4E20 - color temperature of white light in Kelvin
    /// - All other values are prohibited and will be rounded to the nearest
    ///   valid value.
    ///
    /// The Light CTL Delta UV state determines the distance from the Black Body
    /// curve. The color temperature of all on the black body locus (curve).
    /// This is a 16-bit signed integer representation of a -1 to +1 scale using
    /// the formula:
    ///
    /// Represented Delta UV = `deltaUV` / 32768
    ///
    /// - parameters:
    ///   - temperature: The target value of the Light CTL Temperature state.
    ///   - deltaUV: The target value of the Light CTL Delta UV state.
    ///   - transitionTime: The time that an element will take to transition
    ///                     to the target state from the present state.
    ///   - delay: Message execution delay in 5 millisecond steps.
    public init(temperature: UInt16, deltaUV: Int16,
                transitionTime: TransitionTime, delay: UInt8) {
        self.temperature = max(0x0320, min(temperature, 0x4E20))
        self.deltaUV = deltaUV
        self.transitionTime = transitionTime
        self.delay = delay
    }
    
    public init?(parameters: Data) {
        guard parameters.count == 5 || parameters.count == 7 else { return nil }
        temperature = parameters.read()
        deltaUV = parameters.read(fromOffset: 2)
        tid = parameters[4]
        if parameters.count == 7 {
            transitionTime = TransitionTime(rawValue: parameters[5])
            delay = parameters[6]
        } else {
            transitionTime = nil
            delay = nil
        }
    }
    
}
