/**
 * Copyright (c) 2014 Digi International Inc.,
 * All rights not expressly granted are reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 * =======================================================================
 */
package com.digi.xbee.api.packet.raw;

import java.util.LinkedHashMap;

import com.digi.xbee.api.models.XBeeTransmitStatus;
import com.digi.xbee.api.packet.XBeeAPIPacket;
import com.digi.xbee.api.packet.APIFrameType;
import com.digi.xbee.api.utils.HexUtils;

/**
 * This class represents a TX (Transmit) Status packet. Packet is built using 
 * the parameters of the constructor or providing a valid API payload.
 * 
 * <p>When a TX Request is completed, the module sends a TX Status message. 
 * This message will indicate if the packet was transmitted successfully or if 
 * there was a failure.</p>
 * 
 * @see TX16Packet
 * @see TX64Packet
 * @see com.digi.xbee.api.packet.XBeeAPIPacket
 */
public class TXStatusPacket extends XBeeAPIPacket {

	// Constants.
	private static final int MIN_API_PAYLOAD_LENGTH = 3; // 1 (Frame type) + 1 (frame ID) + 1 (status)
	
	// Variables.
	private final XBeeTransmitStatus transmitStatus;
	
	/**
	 * Creates a new {@code TXStatusPacket} object from the given payload.
	 * 
	 * @param payload The API frame payload. It must start with the frame type 
	 *                corresponding to a TX Status packet ({@code 0x89}).
	 *                The byte array must be in {@code OperatingMode.API} mode.
	 * 
	 * @return Parsed TX status packet.
	 * 
	 * @throws IllegalArgumentException if {@code payload[0] != APIFrameType.TX_STATUS.getValue()} or
	 *                                  if {@code payload.length < }{@value #MIN_API_PAYLOAD_LENGTH} or
	 *                                  if {@code frameID < 0} or
	 *                                  if {@code frameID > 255}.
	 * @throws NullPointerException if {@code payload == null}.
	 */
	public static TXStatusPacket createPacket(byte[] payload) {
		if (payload == null)
			throw new NullPointerException("TX Status packet payload cannot be null.");
		
		// 1 (Frame type) + 1 (frame ID) + 1 (status)
		if (payload.length < MIN_API_PAYLOAD_LENGTH)
			throw new IllegalArgumentException("Incomplete TX Status packet.");
		
		if ((payload[0] & 0xFF) != APIFrameType.TX_STATUS.getValue())
			throw new IllegalArgumentException("Payload is not a TX Status packet.");
		
		// payload[0] is the frame type.
		int index = 1;
		
		// Frame ID byte.
		int frameID = payload[index] & 0xFF;
		index = index + 1;
		
		// Status byte.
		int status = payload[index] & 0xFF;
		
		// TODO if status is unknown????
		return new TXStatusPacket(frameID, XBeeTransmitStatus.get(status));
	}
	
	/**
	 * Class constructor. Instantiates a new {@code TXStatusPacket} object
	 * with the given parameters.
	 * 
	 * @param frameID Packet frame ID.
	 * @param transmitStatus Transmit status.
	 * 
	 * @throws IllegalArgumentException if {@code frameID < 0} or
	 *                                  if {@code frameID > 255}.
	 * @throws NullPointerException if {@code transmitStatus == null}.
	 * 
	 * @see com.digi.xbee.api.models.XBeeTransmitStatus
	 */
	public TXStatusPacket(int frameID, XBeeTransmitStatus transmitStatus) {
		super(APIFrameType.TX_STATUS);
		
		if (transmitStatus == null)
			throw new NullPointerException("Transmit status cannot be null.");
		if (frameID < 0 || frameID > 255)
			throw new IllegalArgumentException("Frame ID must be between 0 and 255.");
		
		this.frameID = frameID;
		this.transmitStatus = transmitStatus;
	}

	/*
	 * (non-Javadoc)
	 * @see com.digi.xbee.api.packet.XBeeAPIPacket#getAPIPacketSpecificData()
	 */
	@Override
	protected byte[] getAPIPacketSpecificData() {
		return new byte[] {(byte)transmitStatus.getId()};
	}

	/*
	 * (non-Javadoc)
	 * @see com.digi.xbee.api.packet.XBeeAPIPacket#needsAPIFrameID()
	 */
	@Override
	public boolean needsAPIFrameID() {
		return true;
	}
	
	/**
	 * Returns the transmit status.
	 * 
	 * @return Transmit status.
	 * 
	 * @see com.digi.xbee.api.models.XBeeTransmitStatus
	 */
	public XBeeTransmitStatus getTransmitStatus() {
		return transmitStatus;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.digi.xbee.api.packet.XBeeAPIPacket#getAPIPacketParameters()
	 */
	@Override
	public LinkedHashMap<String, String> getAPIPacketParameters() {
		LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
		parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getId(), 1)) + " (" + transmitStatus.getDescription() + ")");
		return parameters;
	}
}
