using Cyber.Entities.SyncBases;
using Cyber.Networking.Clientside;
using Cyber.Networking.Messages;
namespace Cyber.Items {
    /// 
    /// Handles InventoryActions, building them on the client when needed and running them.
    /// 
    public class InventoryActionHandler {
        private Inventory Inventory;
        private Character Character;
        /// 
        /// Creates an 
        /// 
        /// 
        /// 
        public InventoryActionHandler(Inventory inventory, Character character) {
            Inventory = inventory;
            Character = character;
        }
        /// 
        /// Builds a  packet.
        /// 
        /// The equipped slot to use.
        /// 
        public InventoryActionPkt BuildUseItem(EquipSlot slot) {
            return new InventoryActionPkt(InventoryAction.Use, (int) slot);
        }
        /// 
        /// Builds a  packet.
        /// 
        /// The equipped slot to clear.
        /// 
        public InventoryActionPkt BuildClearSlot(EquipSlot slot) {
            return new InventoryActionPkt(InventoryAction.Unequip, (int) slot);
        }
        /// 
        /// Builds a  packet.
        /// 
        /// The item index to equip.
        /// 
        public InventoryActionPkt BuildEquipItem(int itemIdx) {
            return new InventoryActionPkt(InventoryAction.Equip, itemIdx);
        }
        /// 
        /// Handles an  to handle. Ran on server and client.
        /// 
        /// The  to run
        /// The item related to the action.
        /// Weather the action failed or not.
        public bool HandleAction(InventoryAction action, int relatedInt) {
            switch (action) {
            case InventoryAction.Equip:
                Inventory.Drive.EquipItem(relatedInt);
                return true;
            case InventoryAction.Unequip:
                EquipSlot Slot = (EquipSlot) relatedInt;
                Inventory.Drive.UnequipSlot(Slot);
                return true;
            case InventoryAction.Use:
                EquipSlot UseSlot = (EquipSlot) relatedInt;
                Item UseItem = Inventory.Drive.GetSlot(UseSlot);
                if (UseItem != null && UseItem.Action != null && Character != null &&
                        (!Client.IsRunning() || Client.GetConnectedPlayer().Character != Character)) {
                    // Item exists, it has an action, and the character 
                    // isn't controlled by the client (no double-actions).
                    UseItem.Action(Character);
                    return true;
                }
                return false;
            }
            return false;
        }
    }
}